Skip to content

Commit

Permalink
Analyzer: Improve generator return error messages
Browse files Browse the repository at this point in the history
Fixes dart-lang/sdk#27468

In addition convert both ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE and
ILLEGAL_SYNC_GENERATOR_RETURN_TYPE to CompileTimeErrorCodes.

Change-Id: I5f09f3d77dad52b4ba524c86b3e07ac5fac905fd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/154824
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
  • Loading branch information
srawlins authored and commit-bot@chromium.org committed Jul 18, 2020
1 parent c5a6ead commit bbe6bb9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 49 deletions.
4 changes: 2 additions & 2 deletions pkg/analyzer/lib/error/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_BOUND,
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_TYPE_ARGUMENT,
CompileTimeErrorCode.IF_ELEMENT_CONDITION_FROM_DEFERRED_LIBRARY,
CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS,
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
CompileTimeErrorCode.IMPLEMENTS_NON_CLASS,
Expand Down Expand Up @@ -699,9 +701,7 @@ const List<ErrorCode> errorCodeValues = [
StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS,
StaticTypeWarningCode.FOR_IN_OF_INVALID_ELEMENT_TYPE,
StaticTypeWarningCode.FOR_IN_OF_INVALID_TYPE,
StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
StaticTypeWarningCode.ILLEGAL_ASYNC_RETURN_TYPE,
StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
StaticTypeWarningCode.INSTANCE_ACCESS_TO_STATIC_MEMBER,
StaticTypeWarningCode.INVALID_ASSIGNMENT,
StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION,
Expand Down
48 changes: 24 additions & 24 deletions pkg/analyzer/lib/src/error/codes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,30 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
"an if condition inside a const collection literal.",
correction: "Try making the deferred import non-deferred.");

/**
* It is a compile-time error if the declared return type of a function marked
* 'async*' is not a supertype of 'Stream<T>' for some type 'T'.
*/
static const CompileTimeErrorCode ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE =
CompileTimeErrorCode(
'ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE',
"Functions marked 'async*' must have a return type that is a "
"supertype of 'Stream<T>' for some type 'T'.",
correction: "Try fixing the return type of the function, or "
"removing the modifier 'async*' from the function body.");

/**
* It is a compile-time error if the declared return type of a function marked
* 'sync*' is not a supertype of 'Iterable<T>' for some type 'T'.
*/
static const CompileTimeErrorCode ILLEGAL_SYNC_GENERATOR_RETURN_TYPE =
CompileTimeErrorCode(
'ILLEGAL_SYNC_GENERATOR_RETURN_TYPE',
"Functions marked 'sync*' must have a return type that is a "
"supertype of 'Iterable<T>' for some type 'T'.",
correction: "Try fixing the return type of the function, or "
"removing the modifier 'sync*' from the function body.");

/**
* 7.10 Superinterfaces: It is a compile-time error if the implements clause
* of a class <i>C</i> specifies a malformed type or deferred type as a
Expand Down Expand Up @@ -7170,18 +7194,6 @@ class StaticTypeWarningCode extends AnalyzerErrorCode {
"but {0} found.",
correction: "Try adjusting the number of type arguments.");

/**
* 9 Functions: It is a static warning if the declared return type of a
* function marked async* may not be assigned to Stream.
*/
static const StaticTypeWarningCode ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE =
StaticTypeWarningCode(
'ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE',
"Functions marked 'async*' must have a return type assignable to "
"'Stream'.",
correction: "Try fixing the return type of the function, or "
"removing the modifier 'async*' from the function body.");

/**
* No parameters.
*/
Expand Down Expand Up @@ -7230,18 +7242,6 @@ class StaticTypeWarningCode extends AnalyzerErrorCode {
"removing the modifier 'async' from the function body.",
hasPublishedDocs: true);

/**
* 9 Functions: It is a static warning if the declared return type of a
* function marked sync* may not be assigned to Iterable.
*/
static const StaticTypeWarningCode ILLEGAL_SYNC_GENERATOR_RETURN_TYPE =
StaticTypeWarningCode(
'ILLEGAL_SYNC_GENERATOR_RETURN_TYPE',
"Functions marked 'sync*' must have a return type assignable to "
"'Iterable'.",
correction: "Try fixing the return type of the function, or "
"removing the modifier 'sync*' from the function body.");

/**
* Parameters:
* 0: the name of the static member
Expand Down
7 changes: 4 additions & 3 deletions pkg/analyzer/lib/src/error/return_type_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/error/analyzer_error_code.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/error_verifier.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -87,7 +88,7 @@ class ReturnTypeVerifier {

void checkElement(
ClassElement expectedElement,
StaticTypeWarningCode errorCode,
AnalyzerErrorCode errorCode,
) {
void reportError() {
enclosingExecutable.hasLegalReturnType = false;
Expand All @@ -113,7 +114,7 @@ class ReturnTypeVerifier {
if (enclosingExecutable.isGenerator) {
checkElement(
_typeProvider.streamElement,
StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
);
} else {
checkElement(
Expand All @@ -124,7 +125,7 @@ class ReturnTypeVerifier {
} else if (enclosingExecutable.isGenerator) {
checkElement(
_typeProvider.iterableElement,
StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IllegalAsyncGeneratorReturnTypeTest extends DriverResolutionTest {
await assertErrorsInCode('''
int f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

Expand All @@ -35,15 +35,15 @@ import 'dart:async';
abstract class SubStream<T> implements Stream<T> {}
SubStream<int> f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 73, 14),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 73, 14),
]);
}

test_function_void() async {
await assertErrorsInCode('''
void f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
]);
}

Expand All @@ -53,7 +53,7 @@ class C {
int f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

Expand All @@ -65,7 +65,7 @@ class C {
SubStream<int> f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
]);
}

Expand All @@ -75,7 +75,7 @@ class C {
void f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Iterable<void> f() sync* {}
await assertErrorsInCode('''
int f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

Expand All @@ -34,15 +34,15 @@ int f() sync* {}
abstract class SubIterator<T> implements Iterator<T> {}
SubIterator<int> f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
]);
}

test_function_void() async {
await assertErrorsInCode('''
void f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
]);
}

Expand All @@ -52,7 +52,7 @@ class C {
int f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

Expand All @@ -63,7 +63,7 @@ class C {
SubIterator<int> f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
]);
}

Expand All @@ -73,7 +73,7 @@ class C {
void f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int f() async* {
yield 0;
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 25, 1),
]);
}
Expand All @@ -58,7 +58,7 @@ Iterable<int> f() async* {
yield 0;
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 13),
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 13),
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 35, 1),
]);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ int f() sync* {
yield 0;
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 24, 1),
]);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ Stream<int> f() sync* {
yield 0;
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 22, 11),
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 22, 11),
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 54, 1),
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ int badReturnTypeAsync() async => 0;
// [cfe] Functions marked 'async' must have a return type assignable to 'Future'.
int badReturnTypeAsyncStar() async* {}
// [error line 12, column 1, length 3]
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
// ^
// [cfe] Functions marked 'async*' must have a return type assignable to 'Stream'.
int badReturnTypeSyncStar() sync* {}
// [error line 17, column 1, length 3]
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
// ^
// [cfe] Functions marked 'sync*' must have a return type assignable to 'Iterable'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ int badReturnTypeAsync() async {}
// [cfe] Functions marked 'async' must have a return type assignable to 'Future'.
int badReturnTypeAsyncStar() async* {}
// [error line 12, column 1, length 3]
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
// ^
// [cfe] Functions marked 'async*' must have a return type assignable to 'Stream'.
int badReturnTypeSyncStar() sync* {}
// [error line 17, column 1, length 3]
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
// ^
// [cfe] Functions marked 'sync*' must have a return type assignable to 'Iterable'.

Expand Down

0 comments on commit bbe6bb9

Please sign in to comment.