From 202b07570d025e710034f47d55e142e68e254bbf Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 15 May 2023 20:50:06 +0000 Subject: [PATCH] [stable] 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 Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/299200 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302880 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov Reviewed-by: Samuel Rawlins --- .../lib/src/generated/error_verifier.dart | 6 ++ .../resolution/switch_expression_test.dart | 93 +++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 98ef3cb18735..3eaf49c865a0 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -1252,6 +1252,12 @@ class ErrorVerifier extends RecursiveAstVisitor }); } + @override + void visitSwitchExpression(SwitchExpression node) { + checkForUseOfVoidResult(node.expression); + super.visitSwitchExpression(node); + } + @override void visitSwitchPatternCase(SwitchPatternCase node) { _withHiddenElements(node.statements, () { diff --git a/pkg/analyzer/test/src/dart/resolution/switch_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/switch_expression_test.dart index c09eeb11752f..43a603905d37 100644 --- a/pkg/analyzer/test/src/dart/resolution/switch_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/switch_expression_test.dart @@ -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;