Skip to content

Commit

Permalink
Analyzer: Report generators with void return types.
Browse files Browse the repository at this point in the history
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 <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
srawlins authored and commit-bot@chromium.org committed Jul 11, 2020
1 parent 0e25306 commit 6d06476
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 114 deletions.
114 changes: 0 additions & 114 deletions pkg/analyzer/test/generated/static_type_warning_code_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,120 +332,6 @@ f() {
]);
}

test_illegalAsyncGeneratorReturnType_function_nonStream() async {
await assertErrorsInCode('''
int f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

test_illegalAsyncGeneratorReturnType_function_subtypeOfStream() async {
await assertErrorsInCode('''
import 'dart:async';
abstract class SubStream<T> implements Stream<T> {}
SubStream<int> f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 73, 14),
]);
}

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

test_illegalAsyncGeneratorReturnType_method_nonStream() async {
await assertErrorsInCode('''
class C {
int f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

test_illegalAsyncGeneratorReturnType_method_subtypeOfStream() async {
await assertErrorsInCode('''
import 'dart:async';
abstract class SubStream<T> implements Stream<T> {}
class C {
SubStream<int> f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
]);
}

test_illegalAsyncGeneratorReturnType_method_void() async {
await assertErrorsInCode('''
class C {
void f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}

test_illegalSyncGeneratorReturnType_function_nonIterator() async {
await assertErrorsInCode('''
int f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

test_illegalSyncGeneratorReturnType_function_subclassOfIterator() async {
await assertErrorsInCode('''
abstract class SubIterator<T> implements Iterator<T> {}
SubIterator<int> f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 56, 16),
]);
}

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

test_illegalSyncGeneratorReturnType_method_nonIterator() async {
await assertErrorsInCode('''
class C {
int f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

test_illegalSyncGeneratorReturnType_method_subclassOfIterator() async {
await assertErrorsInCode('''
abstract class SubIterator<T> implements Iterator<T> {}
class C {
SubIterator<int> f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
]);
}

test_illegalSyncGeneratorReturnType_method_void() async {
await assertErrorsInCode('''
class C {
void f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}

// TODO(srawlins) Figure out what to do with the rest of these tests.
// The names do not correspond to diagnostic codes, so it isn't clear what
// they're testing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/driver_resolution.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(IllegalAsyncGeneratorReturnTypeTest);
});
}

@reflectiveTest
class IllegalAsyncGeneratorReturnTypeTest extends DriverResolutionTest {
test_function_nonStream() async {
await assertErrorsInCode('''
int f() async* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

test_function_stream() async {
await assertNoErrorsInCode('''
Stream<void> f() async* {}
''');
}

test_function_subtypeOfStream() async {
await assertErrorsInCode('''
import 'dart:async';
abstract class SubStream<T> implements Stream<T> {}
SubStream<int> f() async* {}
''', [
error(StaticTypeWarningCode.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),
]);
}

test_method_nonStream() async {
await assertErrorsInCode('''
class C {
int f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

test_method_subtypeOfStream() async {
await assertErrorsInCode('''
import 'dart:async';
abstract class SubStream<T> implements Stream<T> {}
class C {
SubStream<int> f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 85, 14),
]);
}

test_method_void() async {
await assertErrorsInCode('''
class C {
void f() async* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/driver_resolution.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(IllegalSyncGeneratorReturnTypeTest);
});
}

@reflectiveTest
class IllegalSyncGeneratorReturnTypeTest extends DriverResolutionTest {
test_function_iterator() async {
await assertNoErrorsInCode('''
Iterable<void> f() sync* {}
''');
}

test_function_nonIterator() async {
await assertErrorsInCode('''
int f() sync* {}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 0, 3),
]);
}

test_function_subclassOfIterator() async {
await assertErrorsInCode('''
abstract class SubIterator<T> implements Iterator<T> {}
SubIterator<int> f() sync* {}
''', [
error(StaticTypeWarningCode.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),
]);
}

test_method_nonIterator() async {
await assertErrorsInCode('''
class C {
int f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 3),
]);
}

test_method_subclassOfIterator() async {
await assertErrorsInCode('''
abstract class SubIterator<T> implements Iterator<T> {}
class C {
SubIterator<int> f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 68, 16),
]);
}

test_method_void() async {
await assertErrorsInCode('''
class C {
void f() sync* {}
}
''', [
error(StaticTypeWarningCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE, 12, 4),
]);
}
}
6 changes: 6 additions & 0 deletions pkg/analyzer/test/src/diagnostics/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ import 'getter_not_subtype_setter_types_test.dart'
as getter_not_subtype_setter_types;
import 'if_element_condition_from_deferred_library_test.dart'
as if_element_condition_from_deferred_library;
import 'illegal_async_generator_return_type_test.dart'
as illegal_async_generator_return_type;
import 'illegal_async_return_type_test.dart' as illegal_async_return_type;
import 'illegal_sync_generator_return_type_test.dart'
as illegal_sync_generator_return_type;
import 'implements_deferred_class_test.dart' as implements_deferred_class;
import 'implements_disallowed_class_test.dart' as implements_disallowed_class;
import 'implements_non_class_test.dart' as implements_non_class;
Expand Down Expand Up @@ -664,7 +668,9 @@ main() {
getter_not_assignable_setter_types.main();
getter_not_subtype_setter_types.main();
if_element_condition_from_deferred_library.main();
illegal_async_generator_return_type.main();
illegal_async_return_type.main();
illegal_sync_generator_return_type.main();
implements_deferred_class.main();
implements_disallowed_class.main();
implements_non_class.main();
Expand Down

0 comments on commit 6d06476

Please sign in to comment.