Issue 52191. It is not an error to use a void typed expression in SwitchExpressionCase.

Bug: https://github.com/dart-lang/sdk/issues/52191
Change-Id: I2cea3260ad63a0c59399f02dadb99bb09a944623
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/299200
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-04-27 21:22:29 +00:00 committed by Commit Queue
parent a0218e916c
commit d838d1a08a
3 changed files with 93 additions and 30 deletions

View file

@ -1258,12 +1258,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
super.visitSwitchExpression(node);
}
@override
void visitSwitchExpressionCase(SwitchExpressionCase node) {
checkForUseOfVoidResult(node.expression);
super.visitSwitchExpressionCase(node);
}
@override
void visitSwitchPatternCase(SwitchPatternCase node) {
_withHiddenElements(node.statements, () {

View file

@ -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) {};
@ -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;

View file

@ -213,30 +213,6 @@ void main() {
]);
}
test_switchExpression_expression() async {
await assertErrorsInCode('''
void f(void x) {
(switch(x) {
_ => 0,
});
}
''', [
error(CompileTimeErrorCode.USE_OF_VOID_RESULT, 27, 1),
]);
}
test_switchExpressionCase_expression() async {
await assertErrorsInCode('''
void f(Object? x) {
(switch(x) {
_ => print(x),
});
}
''', [
error(CompileTimeErrorCode.USE_OF_VOID_RESULT, 44, 5),
]);
}
test_switchStatement_expression() async {
await assertErrorsInCode('''
void f(void x) {