Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bbe6bb9

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Analyzer: Improve generator return error messages
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 <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent c5a6ead commit bbe6bb9

File tree

8 files changed

+50
-49
lines changed

8 files changed

+50
-49
lines changed

pkg/analyzer/lib/error/error.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ const List<ErrorCode> errorCodeValues = [
169169
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_BOUND,
170170
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_TYPE_ARGUMENT,
171171
CompileTimeErrorCode.IF_ELEMENT_CONDITION_FROM_DEFERRED_LIBRARY,
172+
CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
173+
CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
172174
CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS,
173175
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
174176
CompileTimeErrorCode.IMPLEMENTS_NON_CLASS,
@@ -699,9 +701,7 @@ const List<ErrorCode> errorCodeValues = [
699701
StaticTypeWarningCode.EXPECTED_TWO_MAP_TYPE_ARGUMENTS,
700702
StaticTypeWarningCode.FOR_IN_OF_INVALID_ELEMENT_TYPE,
701703
StaticTypeWarningCode.FOR_IN_OF_INVALID_TYPE,
702-
StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
703704
StaticTypeWarningCode.ILLEGAL_ASYNC_RETURN_TYPE,
704-
StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
705705
StaticTypeWarningCode.INSTANCE_ACCESS_TO_STATIC_MEMBER,
706706
StaticTypeWarningCode.INVALID_ASSIGNMENT,
707707
StaticTypeWarningCode.INVOCATION_OF_NON_FUNCTION,

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,30 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
27342734
"an if condition inside a const collection literal.",
27352735
correction: "Try making the deferred import non-deferred.");
27362736

2737+
/**
2738+
* It is a compile-time error if the declared return type of a function marked
2739+
* 'async*' is not a supertype of 'Stream<T>' for some type 'T'.
2740+
*/
2741+
static const CompileTimeErrorCode ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE =
2742+
CompileTimeErrorCode(
2743+
'ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE',
2744+
"Functions marked 'async*' must have a return type that is a "
2745+
"supertype of 'Stream<T>' for some type 'T'.",
2746+
correction: "Try fixing the return type of the function, or "
2747+
"removing the modifier 'async*' from the function body.");
2748+
2749+
/**
2750+
* It is a compile-time error if the declared return type of a function marked
2751+
* 'sync*' is not a supertype of 'Iterable<T>' for some type 'T'.
2752+
*/
2753+
static const CompileTimeErrorCode ILLEGAL_SYNC_GENERATOR_RETURN_TYPE =
2754+
CompileTimeErrorCode(
2755+
'ILLEGAL_SYNC_GENERATOR_RETURN_TYPE',
2756+
"Functions marked 'sync*' must have a return type that is a "
2757+
"supertype of 'Iterable<T>' for some type 'T'.",
2758+
correction: "Try fixing the return type of the function, or "
2759+
"removing the modifier 'sync*' from the function body.");
2760+
27372761
/**
27382762
* 7.10 Superinterfaces: It is a compile-time error if the implements clause
27392763
* of a class <i>C</i> specifies a malformed type or deferred type as a
@@ -7170,18 +7194,6 @@ class StaticTypeWarningCode extends AnalyzerErrorCode {
71707194
"but {0} found.",
71717195
correction: "Try adjusting the number of type arguments.");
71727196

7173-
/**
7174-
* 9 Functions: It is a static warning if the declared return type of a
7175-
* function marked async* may not be assigned to Stream.
7176-
*/
7177-
static const StaticTypeWarningCode ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE =
7178-
StaticTypeWarningCode(
7179-
'ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE',
7180-
"Functions marked 'async*' must have a return type assignable to "
7181-
"'Stream'.",
7182-
correction: "Try fixing the return type of the function, or "
7183-
"removing the modifier 'async*' from the function body.");
7184-
71857197
/**
71867198
* No parameters.
71877199
*/
@@ -7230,18 +7242,6 @@ class StaticTypeWarningCode extends AnalyzerErrorCode {
72307242
"removing the modifier 'async' from the function body.",
72317243
hasPublishedDocs: true);
72327244

7233-
/**
7234-
* 9 Functions: It is a static warning if the declared return type of a
7235-
* function marked sync* may not be assigned to Iterable.
7236-
*/
7237-
static const StaticTypeWarningCode ILLEGAL_SYNC_GENERATOR_RETURN_TYPE =
7238-
StaticTypeWarningCode(
7239-
'ILLEGAL_SYNC_GENERATOR_RETURN_TYPE',
7240-
"Functions marked 'sync*' must have a return type assignable to "
7241-
"'Iterable'.",
7242-
correction: "Try fixing the return type of the function, or "
7243-
"removing the modifier 'sync*' from the function body.");
7244-
72457245
/**
72467246
* Parameters:
72477247
* 0: the name of the static member

pkg/analyzer/lib/src/error/return_type_verifier.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/error/listener.dart';
1010
import 'package:analyzer/src/dart/element/type.dart';
1111
import 'package:analyzer/src/dart/element/type_provider.dart';
1212
import 'package:analyzer/src/dart/element/type_system.dart';
13+
import 'package:analyzer/src/error/analyzer_error_code.dart';
1314
import 'package:analyzer/src/error/codes.dart';
1415
import 'package:analyzer/src/generated/error_verifier.dart';
1516
import 'package:meta/meta.dart';
@@ -87,7 +88,7 @@ class ReturnTypeVerifier {
8788

8889
void checkElement(
8990
ClassElement expectedElement,
90-
StaticTypeWarningCode errorCode,
91+
AnalyzerErrorCode errorCode,
9192
) {
9293
void reportError() {
9394
enclosingExecutable.hasLegalReturnType = false;
@@ -113,7 +114,7 @@ class ReturnTypeVerifier {
113114
if (enclosingExecutable.isGenerator) {
114115
checkElement(
115116
_typeProvider.streamElement,
116-
StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
117+
CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE,
117118
);
118119
} else {
119120
checkElement(
@@ -124,7 +125,7 @@ class ReturnTypeVerifier {
124125
} else if (enclosingExecutable.isGenerator) {
125126
checkElement(
126127
_typeProvider.iterableElement,
127-
StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
128+
CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE,
128129
);
129130
}
130131
}

pkg/analyzer/test/src/diagnostics/illegal_async_generator_return_type_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IllegalAsyncGeneratorReturnTypeTest extends DriverResolutionTest {
1919
await assertErrorsInCode('''
2020
int f() async* {}
2121
''', [
22-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
22+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
2323
]);
2424
}
2525

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

4242
test_function_void() async {
4343
await assertErrorsInCode('''
4444
void f() async* {}
4545
''', [
46-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
46+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
4747
]);
4848
}
4949

@@ -53,7 +53,7 @@ class C {
5353
int f() async* {}
5454
}
5555
''', [
56-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
56+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
5757
]);
5858
}
5959

@@ -65,7 +65,7 @@ class C {
6565
SubStream<int> f() async* {}
6666
}
6767
''', [
68-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
68+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
6969
]);
7070
}
7171

@@ -75,7 +75,7 @@ class C {
7575
void f() async* {}
7676
}
7777
''', [
78-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
78+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
7979
]);
8080
}
8181
}

pkg/analyzer/test/src/diagnostics/illegal_sync_generator_return_type_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Iterable<void> f() sync* {}
2525
await assertErrorsInCode('''
2626
int f() sync* {}
2727
''', [
28-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
28+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
2929
]);
3030
}
3131

@@ -34,15 +34,15 @@ int f() sync* {}
3434
abstract class SubIterator<T> implements Iterator<T> {}
3535
SubIterator<int> f() sync* {}
3636
''', [
37-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
37+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
3838
]);
3939
}
4040

4141
test_function_void() async {
4242
await assertErrorsInCode('''
4343
void f() sync* {}
4444
''', [
45-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
45+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
4646
]);
4747
}
4848

@@ -52,7 +52,7 @@ class C {
5252
int f() sync* {}
5353
}
5454
''', [
55-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
55+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
5656
]);
5757
}
5858

@@ -63,7 +63,7 @@ class C {
6363
SubIterator<int> f() sync* {}
6464
}
6565
''', [
66-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
66+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
6767
]);
6868
}
6969

@@ -73,7 +73,7 @@ class C {
7373
void f() sync* {}
7474
}
7575
''', [
76-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
76+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
7777
]);
7878
}
7979
}

pkg/analyzer/test/src/diagnostics/yield_of_invalid_type_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int f() async* {
3939
yield 0;
4040
}
4141
''', [
42-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
42+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
4343
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 25, 1),
4444
]);
4545
}
@@ -58,7 +58,7 @@ Iterable<int> f() async* {
5858
yield 0;
5959
}
6060
''', [
61-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 13),
61+
error(CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 13),
6262
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 35, 1),
6363
]);
6464
}
@@ -122,7 +122,7 @@ int f() sync* {
122122
yield 0;
123123
}
124124
''', [
125-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
125+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
126126
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 24, 1),
127127
]);
128128
}
@@ -169,7 +169,7 @@ Stream<int> f() sync* {
169169
yield 0;
170170
}
171171
''', [
172-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 22, 11),
172+
error(CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 22, 11),
173173
error(StaticTypeWarningCode.YIELD_OF_INVALID_TYPE, 54, 1),
174174
]);
175175
}

tests/language/async/or_generator_return_type_stacktrace_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ int badReturnTypeAsync() async => 0;
1111
// [cfe] Functions marked 'async' must have a return type assignable to 'Future'.
1212
int badReturnTypeAsyncStar() async* {}
1313
// [error line 12, column 1, length 3]
14-
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
14+
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
1515
// ^
1616
// [cfe] Functions marked 'async*' must have a return type assignable to 'Stream'.
1717
int badReturnTypeSyncStar() sync* {}
1818
// [error line 17, column 1, length 3]
19-
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
19+
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
2020
// ^
2121
// [cfe] Functions marked 'sync*' must have a return type assignable to 'Iterable'.
2222

tests/language_2/async/or_generator_return_type_stacktrace_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ int badReturnTypeAsync() async {}
1111
// [cfe] Functions marked 'async' must have a return type assignable to 'Future'.
1212
int badReturnTypeAsyncStar() async* {}
1313
// [error line 12, column 1, length 3]
14-
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
14+
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE
1515
// ^
1616
// [cfe] Functions marked 'async*' must have a return type assignable to 'Stream'.
1717
int badReturnTypeSyncStar() sync* {}
1818
// [error line 17, column 1, length 3]
19-
// [analyzer] STATIC_TYPE_WARNING.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
19+
// [analyzer] COMPILE_TIME_ERROR.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE
2020
// ^
2121
// [cfe] Functions marked 'sync*' must have a return type assignable to 'Iterable'.
2222

0 commit comments

Comments
 (0)