Skip to content

Commit 6d06476

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Analyzer: Report generators with void return types.
Fixes dart-lang/sdk#32192 This has been illegal for some time. This change updates the analyzer to match the behavior of CFE. Also move the relevant tests to diagnostics/ Change-Id: I4539aeb65708e2594c52288a6b4584ba7e5455e4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153066 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 0e25306 commit 6d06476

File tree

4 files changed

+166
-114
lines changed

4 files changed

+166
-114
lines changed

pkg/analyzer/test/generated/static_type_warning_code_test.dart

-114
Original file line numberDiff line numberDiff line change
@@ -332,120 +332,6 @@ f() {
332332
]);
333333
}
334334

335-
test_illegalAsyncGeneratorReturnType_function_nonStream() async {
336-
await assertErrorsInCode('''
337-
int f() async* {}
338-
''', [
339-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
340-
]);
341-
}
342-
343-
test_illegalAsyncGeneratorReturnType_function_subtypeOfStream() async {
344-
await assertErrorsInCode('''
345-
import 'dart:async';
346-
abstract class SubStream<T> implements Stream<T> {}
347-
SubStream<int> f() async* {}
348-
''', [
349-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 73, 14),
350-
]);
351-
}
352-
353-
test_illegalAsyncGeneratorReturnType_function_void() async {
354-
await assertErrorsInCode('''
355-
void f() async* {}
356-
''', [
357-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
358-
]);
359-
}
360-
361-
test_illegalAsyncGeneratorReturnType_method_nonStream() async {
362-
await assertErrorsInCode('''
363-
class C {
364-
int f() async* {}
365-
}
366-
''', [
367-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
368-
]);
369-
}
370-
371-
test_illegalAsyncGeneratorReturnType_method_subtypeOfStream() async {
372-
await assertErrorsInCode('''
373-
import 'dart:async';
374-
abstract class SubStream<T> implements Stream<T> {}
375-
class C {
376-
SubStream<int> f() async* {}
377-
}
378-
''', [
379-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
380-
]);
381-
}
382-
383-
test_illegalAsyncGeneratorReturnType_method_void() async {
384-
await assertErrorsInCode('''
385-
class C {
386-
void f() async* {}
387-
}
388-
''', [
389-
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
390-
]);
391-
}
392-
393-
test_illegalSyncGeneratorReturnType_function_nonIterator() async {
394-
await assertErrorsInCode('''
395-
int f() sync* {}
396-
''', [
397-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
398-
]);
399-
}
400-
401-
test_illegalSyncGeneratorReturnType_function_subclassOfIterator() async {
402-
await assertErrorsInCode('''
403-
abstract class SubIterator<T> implements Iterator<T> {}
404-
SubIterator<int> f() sync* {}
405-
''', [
406-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
407-
]);
408-
}
409-
410-
test_illegalSyncGeneratorReturnType_function_void() async {
411-
await assertErrorsInCode('''
412-
void f() sync* {}
413-
''', [
414-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
415-
]);
416-
}
417-
418-
test_illegalSyncGeneratorReturnType_method_nonIterator() async {
419-
await assertErrorsInCode('''
420-
class C {
421-
int f() sync* {}
422-
}
423-
''', [
424-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
425-
]);
426-
}
427-
428-
test_illegalSyncGeneratorReturnType_method_subclassOfIterator() async {
429-
await assertErrorsInCode('''
430-
abstract class SubIterator<T> implements Iterator<T> {}
431-
class C {
432-
SubIterator<int> f() sync* {}
433-
}
434-
''', [
435-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
436-
]);
437-
}
438-
439-
test_illegalSyncGeneratorReturnType_method_void() async {
440-
await assertErrorsInCode('''
441-
class C {
442-
void f() sync* {}
443-
}
444-
''', [
445-
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
446-
]);
447-
}
448-
449335
// TODO(srawlins) Figure out what to do with the rest of these tests.
450336
// The names do not correspond to diagnostic codes, so it isn't clear what
451337
// they're testing.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) 2020, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(IllegalAsyncGeneratorReturnTypeTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class IllegalAsyncGeneratorReturnTypeTest extends DriverResolutionTest {
18+
test_function_nonStream() async {
19+
await assertErrorsInCode('''
20+
int f() async* {}
21+
''', [
22+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
23+
]);
24+
}
25+
26+
test_function_stream() async {
27+
await assertNoErrorsInCode('''
28+
Stream<void> f() async* {}
29+
''');
30+
}
31+
32+
test_function_subtypeOfStream() async {
33+
await assertErrorsInCode('''
34+
import 'dart:async';
35+
abstract class SubStream<T> implements Stream<T> {}
36+
SubStream<int> f() async* {}
37+
''', [
38+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 73, 14),
39+
]);
40+
}
41+
42+
test_function_void() async {
43+
await assertErrorsInCode('''
44+
void f() async* {}
45+
''', [
46+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 4),
47+
]);
48+
}
49+
50+
test_method_nonStream() async {
51+
await assertErrorsInCode('''
52+
class C {
53+
int f() async* {}
54+
}
55+
''', [
56+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
57+
]);
58+
}
59+
60+
test_method_subtypeOfStream() async {
61+
await assertErrorsInCode('''
62+
import 'dart:async';
63+
abstract class SubStream<T> implements Stream<T> {}
64+
class C {
65+
SubStream<int> f() async* {}
66+
}
67+
''', [
68+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
69+
]);
70+
}
71+
72+
test_method_void() async {
73+
await assertErrorsInCode('''
74+
class C {
75+
void f() async* {}
76+
}
77+
''', [
78+
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
79+
]);
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2020, 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+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(IllegalSyncGeneratorReturnTypeTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class IllegalSyncGeneratorReturnTypeTest extends DriverResolutionTest {
18+
test_function_iterator() async {
19+
await assertNoErrorsInCode('''
20+
Iterable<void> f() sync* {}
21+
''');
22+
}
23+
24+
test_function_nonIterator() async {
25+
await assertErrorsInCode('''
26+
int f() sync* {}
27+
''', [
28+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
29+
]);
30+
}
31+
32+
test_function_subclassOfIterator() async {
33+
await assertErrorsInCode('''
34+
abstract class SubIterator<T> implements Iterator<T> {}
35+
SubIterator<int> f() sync* {}
36+
''', [
37+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
38+
]);
39+
}
40+
41+
test_function_void() async {
42+
await assertErrorsInCode('''
43+
void f() sync* {}
44+
''', [
45+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 4),
46+
]);
47+
}
48+
49+
test_method_nonIterator() async {
50+
await assertErrorsInCode('''
51+
class C {
52+
int f() sync* {}
53+
}
54+
''', [
55+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
56+
]);
57+
}
58+
59+
test_method_subclassOfIterator() async {
60+
await assertErrorsInCode('''
61+
abstract class SubIterator<T> implements Iterator<T> {}
62+
class C {
63+
SubIterator<int> f() sync* {}
64+
}
65+
''', [
66+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
67+
]);
68+
}
69+
70+
test_method_void() async {
71+
await assertErrorsInCode('''
72+
class C {
73+
void f() sync* {}
74+
}
75+
''', [
76+
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
77+
]);
78+
}
79+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ import 'getter_not_subtype_setter_types_test.dart'
174174
as getter_not_subtype_setter_types;
175175
import 'if_element_condition_from_deferred_library_test.dart'
176176
as if_element_condition_from_deferred_library;
177+
import 'illegal_async_generator_return_type_test.dart'
178+
as illegal_async_generator_return_type;
177179
import 'illegal_async_return_type_test.dart' as illegal_async_return_type;
180+
import 'illegal_sync_generator_return_type_test.dart'
181+
as illegal_sync_generator_return_type;
178182
import 'implements_deferred_class_test.dart' as implements_deferred_class;
179183
import 'implements_disallowed_class_test.dart' as implements_disallowed_class;
180184
import 'implements_non_class_test.dart' as implements_non_class;
@@ -664,7 +668,9 @@ main() {
664668
getter_not_assignable_setter_types.main();
665669
getter_not_subtype_setter_types.main();
666670
if_element_condition_from_deferred_library.main();
671+
illegal_async_generator_return_type.main();
667672
illegal_async_return_type.main();
673+
illegal_sync_generator_return_type.main();
668674
implements_deferred_class.main();
669675
implements_disallowed_class.main();
670676
implements_non_class.main();

0 commit comments

Comments
 (0)