Skip to content

Commit

Permalink
[stable] It is not an error to use a void typed expression in SwitchE…
Browse files Browse the repository at this point in the history
…xpressionCase.

Bug: #52191
Change-Id: I2cea3260ad63a0c59399f02dadb99bb09a944623
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/299200
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302880
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed May 15, 2023
1 parent 66fe70a commit 202b075
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,12 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
});
}

@override
void visitSwitchExpression(SwitchExpression node) {
checkForUseOfVoidResult(node.expression);
super.visitSwitchExpression(node);
}

@override
void visitSwitchPatternCase(SwitchPatternCase node) {
_withHiddenElements(node.statements, () {
Expand Down
93 changes: 93 additions & 0 deletions pkg/analyzer/test/src/dart/resolution/switch_expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,62 @@ main() {

@reflectiveTest
class SwitchExpressionResolutionTest extends PubPackageResolutionTest {
test_case_expression_void() async {
await assertNoErrorsInCode(r'''
void f(Object? x) {
(switch(x) {
0 => 0,
_ => g(),
});
}
void g() {}
''');

final node = findNode.singleSwitchExpression;
assertResolvedNodeText(node, r'''
SwitchExpression
switchKeyword: switch
leftParenthesis: (
expression: SimpleIdentifier
token: x
staticElement: self::@function::f::@parameter::x
staticType: Object?
rightParenthesis: )
leftBracket: {
cases
SwitchExpressionCase
guardedPattern: GuardedPattern
pattern: ConstantPattern
expression: IntegerLiteral
literal: 0
staticType: int
matchedValueType: Object?
arrow: =>
expression: IntegerLiteral
literal: 0
staticType: int
SwitchExpressionCase
guardedPattern: GuardedPattern
pattern: WildcardPattern
name: _
matchedValueType: Object?
arrow: =>
expression: MethodInvocation
methodName: SimpleIdentifier
token: g
staticElement: self::@function::g
staticType: void Function()
argumentList: ArgumentList
leftParenthesis: (
rightParenthesis: )
staticInvokeType: void Function()
staticType: void
rightBracket: }
staticType: void
''');
}

test_cases_empty() async {
await assertErrorsInCode(r'''
final a = switch (0) {};
Expand Down Expand Up @@ -85,6 +141,43 @@ SwitchExpression
''');
}

test_expression_void() async {
await assertErrorsInCode('''
void f(void x) {
(switch(x) {
_ => 0,
});
}
''', [
error(CompileTimeErrorCode.USE_OF_VOID_RESULT, 27, 1),
]);

final node = findNode.singleSwitchExpression;
assertResolvedNodeText(node, r'''
SwitchExpression
switchKeyword: switch
leftParenthesis: (
expression: SimpleIdentifier
token: x
staticElement: self::@function::f::@parameter::x
staticType: void
rightParenthesis: )
leftBracket: {
cases
SwitchExpressionCase
guardedPattern: GuardedPattern
pattern: WildcardPattern
name: _
matchedValueType: void
arrow: =>
expression: IntegerLiteral
literal: 0
staticType: int
rightBracket: }
staticType: int
''');
}

test_location_topLevel() async {
await assertNoErrorsInCode(r'''
num a = 0;
Expand Down

0 comments on commit 202b075

Please sign in to comment.