Skip to content

Commit e52a8be

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Report error for generators with return type void.
Bug: #32192 Change-Id: Ib31205207def253dc89802898a899b3555ecc28d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153953 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 1561cc3 commit e52a8be

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,24 @@ class ReturnTypeVerifier {
8989
ClassElement expectedElement,
9090
StaticTypeWarningCode errorCode,
9191
) {
92-
if (!_isLegalReturnType(expectedElement)) {
92+
void reportError() {
9393
enclosingExecutable.hasLegalReturnType = false;
9494
_errorReporter.reportErrorForNode(errorCode, returnType);
9595
}
96+
97+
// It is a compile-time error if the declared return type of
98+
// a function marked `sync*` or `async*` is `void`.
99+
if (enclosingExecutable.isGenerator) {
100+
if (enclosingExecutable.returnType.isVoid) {
101+
return reportError();
102+
}
103+
}
104+
105+
// It is a compile-time error if the declared return type of
106+
// a function marked `...` is not a supertype of `...`.
107+
if (!_isLegalReturnType(expectedElement)) {
108+
return reportError();
109+
}
96110
}
97111

98112
if (enclosingExecutable.isAsynchronous) {

pkg/analyzer/test/generated/static_type_warning_code_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ SubStream<int> f() async* {}
380380
]);
381381
}
382382

383+
test_illegalAsyncGeneratorReturnType_function_void() async {
384+
await assertErrorsInCode('''
385+
void f() async* {}
386+
''', [
387+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
388+
]);
389+
}
390+
383391
test_illegalAsyncGeneratorReturnType_method_nonStream() async {
384392
await assertErrorsInCode('''
385393
class C {
@@ -402,6 +410,16 @@ class C {
402410
]);
403411
}
404412

413+
test_illegalAsyncGeneratorReturnType_method_void() async {
414+
await assertErrorsInCode('''
415+
class C {
416+
void f() async* {}
417+
}
418+
''', [
419+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
420+
]);
421+
}
422+
405423
test_illegalSyncGeneratorReturnType_function_nonIterator() async {
406424
await assertErrorsInCode('''
407425
int f() sync* {}
@@ -419,6 +437,14 @@ SubIterator<int> f() sync* {}
419437
]);
420438
}
421439

440+
test_illegalSyncGeneratorReturnType_function_void() async {
441+
await assertErrorsInCode('''
442+
void f() sync* {}
443+
''', [
444+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
445+
]);
446+
}
447+
422448
test_illegalSyncGeneratorReturnType_method_nonIterator() async {
423449
await assertErrorsInCode('''
424450
class C {
@@ -440,6 +466,16 @@ class C {
440466
]);
441467
}
442468

469+
test_illegalSyncGeneratorReturnType_method_void() async {
470+
await assertErrorsInCode('''
471+
class C {
472+
void f() sync* {}
473+
}
474+
''', [
475+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
476+
]);
477+
}
478+
443479
test_instanceAccessToStaticMember_method_reference() async {
444480
await assertErrorsInCode(r'''
445481
class A {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ int f() async {}
2323
]);
2424
}
2525

26+
test_function_nonFuture_void() async {
27+
await assertNoErrorsInCode('''
28+
void f() async {}
29+
''');
30+
}
31+
2632
test_function_nonFuture_withReturn() async {
2733
await assertErrorsInCode('''
2834
int f() async {
@@ -55,6 +61,14 @@ class C {
5561
]);
5662
}
5763

64+
test_method_nonFuture_void() async {
65+
await assertNoErrorsInCode('''
66+
class C {
67+
void m() async {}
68+
}
69+
''');
70+
}
71+
5872
test_method_subtypeOfFuture() async {
5973
await assertErrorsInCode('''
6074
import 'dart:async';

0 commit comments

Comments
 (0)