Skip to content

Commit 6cb0966

Browse files
committed
Allowing e of any type in void f(...) => e in the analyzer.
Re-land, now including adjustment of two tests in pkg/analyzer/test. [email protected] Review-Url: https://codereview.chromium.org/2870363003 .
1 parent 8bce36d commit 6cb0966

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
660660
function is PropertyAccessorElement &&
661661
function.isSetter;
662662
if (!isSetterWithImplicitReturn) {
663-
_checkForReturnOfInvalidType(node.expression, expectedReturnType);
663+
_checkForReturnOfInvalidType(node.expression, expectedReturnType,
664+
isArrowFunction: true);
664665
}
665666
return super.visitExpressionFunctionBody(node);
666667
} finally {
@@ -5598,7 +5599,8 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
55985599
* See [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE].
55995600
*/
56005601
void _checkForReturnOfInvalidType(
5601-
Expression returnExpression, DartType expectedReturnType) {
5602+
Expression returnExpression, DartType expectedReturnType,
5603+
{bool isArrowFunction = false}) {
56025604
if (_enclosingFunction == null) {
56035605
return;
56045606
}
@@ -5610,6 +5612,10 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
56105612
}
56115613
DartType staticReturnType = _computeReturnTypeForMethod(returnExpression);
56125614
if (expectedReturnType.isVoid) {
5615+
if (isArrowFunction) {
5616+
// "void f(..) => e" admits all types for "e".
5617+
return;
5618+
}
56135619
if (staticReturnType.isVoid ||
56145620
staticReturnType.isDynamic ||
56155621
staticReturnType.isBottom ||

pkg/analyzer/test/generated/static_type_warning_code_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,9 +1121,8 @@ class A {
11211121
[StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]);
11221122
}
11231123

1124-
test_returnOfInvalidType_expressionFunctionBody_void() async {
1125-
await assertErrorsInCode(
1126-
"void f() => 42;", [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]);
1124+
test_returnOfInvalidType_not_issued_for_expressionFunctionBody_void() async {
1125+
await assertNoErrorsInCode("void f() => 42;");
11271126
}
11281127

11291128
test_returnOfInvalidType_function() async {

pkg/analyzer/test/src/task/strong/checker_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3536,7 +3536,7 @@ void voidFn() => null;
35363536
class A {
35373537
set a(y) => 4;
35383538
set b(y) => voidFn();
3539-
void set c(y) => /*error:RETURN_OF_INVALID_TYPE*/4;
3539+
void set c(y) => 4;
35403540
void set d(y) => voidFn();
35413541
/*warning:NON_VOID_RETURN_FOR_SETTER*/int set e(y) => 4;
35423542
/*warning:NON_VOID_RETURN_FOR_SETTER*/int set f(y) =>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Testing that a void arrow function is allowed to return any type of value.
6+
7+
void foo() => 42;
8+
9+
main() {
10+
foo();
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Testing that a void block function is not allowed to `return e`
6+
// where `e` is non-void.
7+
8+
void foo() {
9+
return 42; //# 00: static type warning
10+
}
11+
12+
main() {
13+
foo();
14+
}

0 commit comments

Comments
 (0)