[analyzer] Remove all checks for set-literal and extensions enablement

And using Future from dart:core, which was not enabled until Dart
2.1.0.

Dart 3's minimum language version is 2.12. google3's is 2.9.

Change-Id: Id31d92007e685447ff217bdccf8161c8fd6ce6e7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316863
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins 2023-07-30 00:12:59 +00:00 committed by Commit Queue
parent 823f1583bf
commit 415ff8ece8
15 changed files with 39 additions and 790 deletions

View file

@ -3630,8 +3630,6 @@ WarningCode.RETURN_OF_INVALID_TYPE_FROM_CATCH_ERROR:
status: noFix
WarningCode.RETURN_TYPE_INVALID_FOR_CATCH_ERROR:
status: noFix
WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE:
status: hasFix
WarningCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT:
status: hasFix
WarningCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT:
@ -3641,8 +3639,6 @@ WarningCode.SDK_VERSION_CONSTRUCTOR_TEAROFFS:
since: 2.15
WarningCode.SDK_VERSION_EQ_EQ_OPERATOR_IN_CONST_CONTEXT:
status: hasFix
WarningCode.SDK_VERSION_EXTENSION_METHODS:
status: hasFix
WarningCode.SDK_VERSION_GT_GT_GT_OPERATOR:
status: hasFix
WarningCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT:
@ -3650,8 +3646,6 @@ WarningCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT:
WarningCode.SDK_VERSION_NEVER:
status: noFix
notes: Deprecated
WarningCode.SDK_VERSION_SET_LITERAL:
status: hasFix
WarningCode.SDK_VERSION_SINCE:
status: needsFix
notes: |-

View file

@ -979,9 +979,6 @@ class FixProcessor extends BaseProcessor {
WarningCode.OVERRIDE_ON_NON_OVERRIDING_METHOD: [
DataDriven.new,
],
WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: [
ImportLibrary.dartAsync,
],
};
/// A map from error codes to a list of the generators that are used to create
@ -1627,27 +1624,18 @@ class FixProcessor extends BaseProcessor {
WarningCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT: [
UpdateSdkConstraints.version_2_2_2,
],
WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE: [
UpdateSdkConstraints.version_2_1_0,
],
WarningCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT: [
UpdateSdkConstraints.version_2_2_2,
],
WarningCode.SDK_VERSION_EQ_EQ_OPERATOR_IN_CONST_CONTEXT: [
UpdateSdkConstraints.version_2_2_2,
],
WarningCode.SDK_VERSION_EXTENSION_METHODS: [
UpdateSdkConstraints.version_2_6_0,
],
WarningCode.SDK_VERSION_GT_GT_GT_OPERATOR: [
UpdateSdkConstraints.version_2_14_0,
],
WarningCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT: [
UpdateSdkConstraints.version_2_2_2,
],
WarningCode.SDK_VERSION_SET_LITERAL: [
UpdateSdkConstraints.version_2_2_0,
],
WarningCode.TEXT_DIRECTION_CODE_POINT_IN_COMMENT: [
RemoveCharacter.new,
ReplaceWithUnicodeEscape.new,

View file

@ -1,51 +0,0 @@
// Copyright (c) 2018, 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:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ImportAsyncTest);
});
}
@reflectiveTest
class ImportAsyncTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.IMPORT_ASYNC;
Future<void> test_future() async {
updateTestPubspecFile('''
environment:
sdk: ^2.0.0
''');
await resolveTestCode('''
Future<int> zero() async => 0;
''');
await assertHasFix('''
import 'dart:async';
Future<int> zero() async => 0;
''');
}
Future<void> test_stream() async {
updateTestPubspecFile('''
environment:
sdk: ^2.0.0
''');
await resolveTestCode('''
Stream<int> zero() => throw '';
''');
await assertHasFix('''
import 'dart:async';
Stream<int> zero() => throw '';
''');
}
}

View file

@ -119,7 +119,6 @@ import 'fix_in_file_test.dart' as fix_in_file;
import 'fix_processor_map_test.dart' as fix_processor_map;
import 'fix_test.dart' as fix;
import 'ignore_diagnostic_test.dart' as ignore_error;
import 'import_async_test.dart' as import_async;
import 'import_library_prefix_test.dart' as import_library_prefix;
import 'import_library_project_test.dart' as import_library_project;
import 'import_library_sdk_test.dart' as import_library_sdk;
@ -378,7 +377,6 @@ void main() {
fix_in_file.main();
fix_processor_map.main();
ignore_error.main();
import_async.main();
import_library_prefix.main();
import_library_project.main();
import_library_sdk.main();

View file

@ -20,33 +20,62 @@ class UpdateSdkConstraintsTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.UPDATE_SDK_CONSTRAINTS;
/// Asserts that a library with [content] can be updated from the [from]
/// constraints to the [to] constraints.
Future<void> assertUpdate(
{required String content,
String from = '^2.0.0',
required String to}) async {
updateTestPubspecFile('''
environment:
sdk: $from
''');
await resolveTestCode(content);
await assertHasFix('''
environment:
sdk: $to
''', target: testPubspecPath);
}
/// Asserts that a library with `>>>` can be updated from the [from]
/// constraints to the [to] constraints.
Future<void> assertUpdateWithGtGtGt(
{required String from, required String to}) async {
await assertUpdate(content: r'''
class C {
C operator >>>(C other) => this;
}
''', from: from, to: to);
}
Future<void> test_any() async {
await testUpdate(from: 'any', to: '^2.1.0');
await assertUpdateWithGtGtGt(from: 'any', to: '^2.14.0');
}
Future<void> test_asInConstContext() async {
await testUpdate(content: '''
await assertUpdate(content: '''
const dynamic a = 2;
const c = a as int;
''', to: '^2.2.2');
}
Future<void> test_boolOperator() async {
await testUpdate(content: '''
await assertUpdate(content: '''
const c = true & false;
''', to: '^2.2.2');
}
Future<void> test_caret() async {
await testUpdate(from: '^2.0.0', to: '^2.1.0');
await assertUpdateWithGtGtGt(from: '^2.12.0', to: '^2.14.0');
}
Future<void> test_compound() async {
await testUpdate(from: "'>=2.0.0 <3.0.0'", to: "'>=2.1.0 <3.0.0'");
await assertUpdateWithGtGtGt(
from: "'>=2.12.0 <3.0.0'", to: "'>=2.14.0 <3.0.0'");
}
Future<void> test_eqEqOperatorInConstContext() async {
await testUpdate(content: '''
await assertUpdate(content: '''
class A {
const A();
}
@ -56,17 +85,17 @@ const c = a == null;
}
Future<void> test_gt() async {
await testUpdate(from: "'>2.0.0'", to: "'>=2.1.0'");
await assertUpdateWithGtGtGt(from: "'>2.12.0'", to: "'>=2.14.0'");
}
Future<void> test_gte() async {
await testUpdate(from: "'>=2.0.0'", to: "'>=2.1.0'");
await assertUpdateWithGtGtGt(from: "'>=2.12.0'", to: "'>=2.14.0'");
}
Future<void> test_gtGtGtOperator() async {
writeTestPackageConfig(languageVersion: latestLanguageVersion);
createAnalysisOptionsFile(experiments: [EnableString.triple_shift]);
await testUpdate(content: '''
await assertUpdate(content: '''
class C {
C operator >>>(C other) => this;
}
@ -74,31 +103,9 @@ class C {
}
Future<void> test_isInConstContext() async {
await testUpdate(content: '''
await assertUpdate(content: '''
const num a = 0;
const c = a is int;
''', to: '^2.2.2');
}
Future<void> test_setLiteral() async {
await testUpdate(content: '''
var s = <int>{};
''', to: '^2.2.0');
}
Future<void> testUpdate(
{String? content, String from = '^2.0.0', required String to}) async {
updateTestPubspecFile('''
environment:
sdk: $from
''');
await resolveTestCode(content ??
'''
Future<int> zero() async => 0;
''');
await assertHasFix('''
environment:
sdk: $to
''', target: testPubspecPath);
}
}

View file

@ -6852,17 +6852,6 @@ class WarningCode extends AnalyzerErrorCode {
uniqueName: 'RETURN_TYPE_INVALID_FOR_CATCH_ERROR',
);
/// Parameters:
/// 0: the name of the class
static const WarningCode SDK_VERSION_ASYNC_EXPORTED_FROM_CORE = WarningCode(
'SDK_VERSION_ASYNC_EXPORTED_FROM_CORE',
"The class '{0}' wasn't exported from 'dart:core' until version 2.1, but "
"this code is required to be able to run on earlier versions.",
correctionMessage:
"Try either importing 'dart:async' or updating the SDK constraints.",
hasPublishedDocs: true,
);
/// No parameters.
static const WarningCode SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT =
WarningCode(
@ -6913,15 +6902,6 @@ class WarningCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);
/// No parameters.
static const WarningCode SDK_VERSION_EXTENSION_METHODS = WarningCode(
'SDK_VERSION_EXTENSION_METHODS',
"Extension methods weren't supported until version 2.6.0, but this code is "
"required to be able to run on earlier versions.",
correctionMessage: "Try updating the SDK constraints.",
hasPublishedDocs: true,
);
/// No parameters.
static const WarningCode SDK_VERSION_GT_GT_GT_OPERATOR = WarningCode(
'SDK_VERSION_GT_GT_GT_OPERATOR',
@ -6951,15 +6931,6 @@ class WarningCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);
/// No parameters.
static const WarningCode SDK_VERSION_SET_LITERAL = WarningCode(
'SDK_VERSION_SET_LITERAL',
"Set literals weren't supported until version 2.2, but this code is "
"required to be able to run on earlier versions.",
correctionMessage: "Try updating the SDK constraints.",
hasPublishedDocs: true,
);
/// Parameters:
/// 0: the version specified in the `@Since()` annotation
/// 1: the SDK version constraints

View file

@ -1005,16 +1005,13 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.RETURN_OF_DO_NOT_STORE,
WarningCode.RETURN_OF_INVALID_TYPE_FROM_CATCH_ERROR,
WarningCode.RETURN_TYPE_INVALID_FOR_CATCH_ERROR,
WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE,
WarningCode.SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT,
WarningCode.SDK_VERSION_BOOL_OPERATOR_IN_CONST_CONTEXT,
WarningCode.SDK_VERSION_CONSTRUCTOR_TEAROFFS,
WarningCode.SDK_VERSION_EQ_EQ_OPERATOR_IN_CONST_CONTEXT,
WarningCode.SDK_VERSION_EXTENSION_METHODS,
WarningCode.SDK_VERSION_GT_GT_GT_OPERATOR,
WarningCode.SDK_VERSION_IS_EXPRESSION_IN_CONST_CONTEXT,
WarningCode.SDK_VERSION_NEVER,
WarningCode.SDK_VERSION_SET_LITERAL,
WarningCode.SDK_VERSION_SINCE,
WarningCode.STRICT_RAW_TYPE,
WarningCode.SUBTYPE_OF_SEALED_CLASS,

View file

@ -38,10 +38,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
/// need to be checked. Use [checkTripleShift] to access this field.
bool? _checkTripleShift;
/// A cached flag indicating whether uses of extension method features need to
/// be checked. Use [checkExtensionMethods] to access this field.
bool? _checkExtensionMethods;
/// A cached flag indicating whether references to Future and Stream need to
/// be checked. Use [checkFutureAndStream] to access this field.
bool? _checkFutureAndStream;
@ -50,10 +46,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
/// be checked. Use [checkSetLiterals] to access this field.
bool? _checkSetLiterals;
/// A flag indicating whether we are visiting code inside a set literal. Used
/// to prevent over-reporting uses of set literals.
bool _inSetLiteral = false;
/// Initialize a newly created verifier to use the given [_errorReporter] to
/// report errors.
SdkConstraintVerifier(this._errorReporter, this._containingLibrary,
@ -88,11 +80,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
bool get checkConstantUpdate2018 => _checkConstantUpdate2018 ??=
!before_2_5_0.intersect(_versionConstraint).isEmpty;
/// Return `true` if references to the extension method features need to
/// be checked.
bool get checkExtensionMethods => _checkExtensionMethods ??=
!before_2_6_0.intersect(_versionConstraint).isEmpty;
/// Return `true` if references to Future and Stream need to be checked.
bool get checkFutureAndStream => _checkFutureAndStream ??=
!before_2_1_0.intersect(_versionConstraint).isEmpty;
@ -185,27 +172,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
super.visitConstructorName(node);
}
@override
void visitExtensionDeclaration(ExtensionDeclaration node) {
if (checkExtensionMethods) {
_errorReporter.reportErrorForToken(
WarningCode.SDK_VERSION_EXTENSION_METHODS, node.extensionKeyword);
}
super.visitExtensionDeclaration(node);
}
@override
void visitExtensionOverride(ExtensionOverride node) {
if (checkExtensionMethods) {
_errorReporter.reportErrorForToken(
WarningCode.SDK_VERSION_EXTENSION_METHODS,
node.name,
);
}
_checkSinceSdkVersion(node.element, node);
super.visitExtensionOverride(node);
}
@override
void visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
_checkSinceSdkVersion(node.staticElement, node);
@ -249,11 +215,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
@override
void visitNamedType(NamedType node) {
_checkAsyncExportedFromCode(
name: node.name2,
element: node.element,
);
if (checkNnbd && node.element == _typeProvider.neverType.element) {
_errorReporter.reportErrorForNode(WarningCode.SDK_VERSION_NEVER, node);
}
@ -274,18 +235,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
super.visitPropertyAccess(node);
}
@override
void visitSetOrMapLiteral(SetOrMapLiteral node) {
if (node.isSet && checkSetLiterals && !_inSetLiteral) {
_errorReporter.reportErrorForNode(
WarningCode.SDK_VERSION_SET_LITERAL, node);
}
bool wasInSetLiteral = _inSetLiteral;
_inSetLiteral = true;
super.visitSetOrMapLiteral(node);
_inSetLiteral = wasInSetLiteral;
}
@override
void visitShowCombinator(ShowCombinator node) {
// Don't flag references to either `Future` or `Stream` within a combinator.
@ -299,31 +248,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
_checkSinceSdkVersion(node.staticElement, node);
}
void _checkAsyncExportedFromCode({
required Token name,
required Element? element,
}) {
if (checkFutureAndStream &&
element is InterfaceElement &&
(element == _typeProvider.futureElement ||
element == _typeProvider.streamElement)) {
for (LibraryElement importedLibrary
in _containingLibrary.importedLibraries) {
if (!importedLibrary.isDartCore) {
var namespace = importedLibrary.exportNamespace;
if (namespace.get(element.name) != null) {
return;
}
}
}
_errorReporter.reportErrorForToken(
WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE,
name,
[element.name],
);
}
}
void _checkSinceSdkVersion(
Element? element,
AstNode target, {

View file

@ -24097,58 +24097,6 @@ WarningCode:
int g() => 0;
```
SDK_VERSION_ASYNC_EXPORTED_FROM_CORE:
problemMessage: "The class '{0}' wasn't exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions."
correctionMessage: "Try either importing 'dart:async' or updating the SDK constraints."
hasPublishedDocs: true
comment: |-
Parameters:
0: the name of the class
documentation: |-
#### Description
The analyzer produces this diagnostic when either the class `Future` or
`Stream` is referenced in a library that doesn't import `dart:async` in
code that has an SDK constraint whose lower bound is less than 2.1.0. In
earlier versions, these classes weren't defined in `dart:core`, so the
import was necessary.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.1.0:
```yaml
%uri="pubspec.yaml"
environment:
sdk: '>=2.0.0 <2.4.0'
```
In the package that has that pubspec, code like the following produces this
diagnostic:
```dart
void f([!Future!] f) {}
```
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the classes to be referenced:
```yaml
environment:
sdk: '>=2.1.0 <2.4.0'
```
If you need to support older versions of the SDK, then import the
`dart:async` library.
```dart
import 'dart:async';
void f(Future f) {}
```
SDK_VERSION_AS_EXPRESSION_IN_CONST_CONTEXT:
problemMessage: "The use of an as expression in a constant expression wasn't supported until version 2.3.2, but this code is required to be able to run on earlier versions."
correctionMessage: Try updating the SDK constraints.
@ -24368,62 +24316,6 @@ WarningCode:
const C b = null;
bool same = a == b;
```
SDK_VERSION_EXTENSION_METHODS:
problemMessage: "Extension methods weren't supported until version 2.6.0, but this code is required to be able to run on earlier versions."
correctionMessage: Try updating the SDK constraints.
hasPublishedDocs: true
comment: No parameters.
documentation: |-
#### Description
The analyzer produces this diagnostic when an extension declaration or an
extension override is found in code that has an SDK constraint whose lower
bound is less than 2.6.0. Using extensions wasn't supported in earlier
versions, so this code won't be able to run against earlier versions of the
SDK.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.6.0:
```yaml
%uri="pubspec.yaml"
environment:
sdk: '>=2.4.0 <2.7.0'
```
In the package that has that pubspec, code like the following produces
this diagnostic:
```dart
[!extension!] E on String {
void sayHello() {
print('Hello $this');
}
}
```
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the syntax to be used:
```yaml
environment:
sdk: '>=2.6.0 <2.7.0'
```
If you need to support older versions of the SDK, then rewrite the code to
not make use of extensions. The most common way to do this is to rewrite
the members of the extension as top-level functions (or methods) that take
the value that would have been bound to `this` as a parameter:
```dart
void sayHello(String s) {
print('Hello $s');
}
```
SDK_VERSION_GT_GT_GT_OPERATOR:
problemMessage: "The operator '>>>' wasn't supported until version 2.14.0, but this code is required to be able to run on earlier versions."
correctionMessage: Try updating the SDK constraints.
@ -24579,53 +24471,6 @@ WarningCode:
```dart
dynamic x;
```
SDK_VERSION_SET_LITERAL:
problemMessage: "Set literals weren't supported until version 2.2, but this code is required to be able to run on earlier versions."
correctionMessage: Try updating the SDK constraints.
hasPublishedDocs: true
comment: No parameters.
documentation: |-
#### Description
The analyzer produces this diagnostic when a set literal is found in code
that has an SDK constraint whose lower bound is less than 2.2.0. Set
literals weren't supported in earlier versions, so this code won't be able
to run against earlier versions of the SDK.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.2.0:
```yaml
%uri="pubspec.yaml"
environment:
sdk: '>=2.1.0 <2.4.0'
```
In the package that has that pubspec, code like the following produces this
diagnostic:
```dart
var s = [!<int>{}!];
```
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the syntax to be used:
```yaml
environment:
sdk: '>=2.2.0 <2.4.0'
```
If you do need to support older versions of the SDK, then replace the set
literal with code that creates the set without the use of a literal:
```dart
var s = new Set<int>();
```
SDK_VERSION_SINCE:
problemMessage: "This API is available since SDK {0}, but constraints '{1}' don't guarantee it."
correctionMessage: Try updating the SDK constraints.

View file

@ -1,172 +0,0 @@
// Copyright (c) 2018, 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/dart/analysis/results.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
import 'sdk_constraint_verifier_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(SdkVersionAsyncExportedFromCoreTest);
});
}
@reflectiveTest
class SdkVersionAsyncExportedFromCoreTest extends SdkConstraintVerifierTest {
test_equals_explicitImportOfAsync() async {
await verifyVersion('>=2.1.0', '''
import 'dart:async';
Future<int> zero() async => 0;
''');
}
test_equals_explicitImportOfCore() async {
await verifyVersion('>=2.1.0', '''
import 'dart:core';
Future<int> zero() async => 0;
''');
}
test_equals_explicitImportOfExportingLibrary() async {
newFile('$testPackageLibPath/exporter.dart', '''
export 'dart:async';
''');
await verifyVersion('>=2.1.0', '''
import 'exporter.dart';
Future<int> zero() async => 0;
''');
}
test_equals_implicitImportOfCore() async {
await verifyVersion('>=2.1.0', '''
Future<int> zero() async => 0;
''');
}
test_equals_implicitImportOfCore_inPart() async {
writeTestPackagePubspecYamlFile(
PubspecYamlFileConfig(
sdkVersion: '>=2.1.0',
),
);
final lib = newFile('$testPackageLibPath/lib.dart', '''
library lib;
part 'a.dart';
''');
final a = newFile('$testPackageLibPath/a.dart', r'''
part of lib;
Future<int> zero() async => 0;
''');
final analysisSession = contextFor(lib).currentSession;
final resolvedLibrary = await analysisSession.getResolvedLibrary(lib.path);
resolvedLibrary as ResolvedLibraryResult;
final resolvedPart = resolvedLibrary.units.last;
expect(resolvedPart.path, a.path);
assertErrorsInList(resolvedPart.errors, []);
}
test_lessThan_explicitImportOfAsync() async {
await verifyVersion('>=2.0.0', '''
import 'dart:async';
Future<int> zero() async => 0;
''');
}
test_lessThan_explicitImportOfCore() async {
await verifyVersion('>=2.0.0', '''
import 'dart:core' show Future, int;
Future<int> zero() async => 0;
''', expectedErrors: [
error(WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE, 38, 6),
]);
}
test_lessThan_explicitImportOfExportingLibrary() async {
newFile('$testPackageLibPath/exporter.dart', '''
export 'dart:async';
''');
await verifyVersion('>=2.0.0', '''
import 'exporter.dart';
Future<int> zero() async => 0;
''');
}
test_lessThan_implicitImportOfCore() async {
await verifyVersion('>=2.0.0', '''
Future<int> zero() async => 0;
''', expectedErrors: [
error(WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE, 0, 6),
]);
}
test_lessThan_implicitImportOfCore_inPart() async {
writeTestPackagePubspecYamlFile(
PubspecYamlFileConfig(
sdkVersion: '>=2.0.0',
),
);
final lib = newFile('$testPackageLibPath/lib.dart', '''
library lib;
part 'a.dart';
''');
final a = newFile('$testPackageLibPath/a.dart', r'''
part of lib;
Future<int> zero() async => 0;
''');
final analysisSession = contextFor(lib).currentSession;
final resolvedLibrary = await analysisSession.getResolvedLibrary(lib.path);
resolvedLibrary as ResolvedLibraryResult;
final resolvedPart = resolvedLibrary.units.last;
expect(resolvedPart.path, a.path);
assertErrorsInList(resolvedPart.errors, [
error(WarningCode.SDK_VERSION_ASYNC_EXPORTED_FROM_CORE, 14, 6),
]);
}
test_lessThan_onlyReferencedInExport_hide() async {
await verifyVersion('>=2.0.0', '''
export 'dart:async' hide Future;
''');
}
test_lessThan_onlyReferencedInExport_show() async {
await verifyVersion('>=2.0.0', '''
export 'dart:async' show Future;
''');
}
test_lessThan_onlyReferencedInImport_hide() async {
await verifyVersion('>=2.0.0', '''
import 'dart:core' hide Future;
''');
}
test_lessThan_onlyReferencedInImport_show() async {
await verifyVersion('>=2.0.0', '''
import 'dart:core' show Future;
''');
}
}

View file

@ -1,56 +0,0 @@
// Copyright (c) 2019, 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 'sdk_constraint_verifier_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(SdkVersionExtensionMethodsTest);
});
}
@reflectiveTest
class SdkVersionExtensionMethodsTest extends SdkConstraintVerifierTest {
test_extension_equals() async {
await verifyVersion('>=2.6.0', '''
extension E on int {}
''');
}
test_extension_lessThan() async {
await verifyVersion('>=2.2.0', '''
extension E on int {}
''', expectedErrors: [
error(WarningCode.SDK_VERSION_EXTENSION_METHODS, 0, 9),
]);
}
test_extensionOverride_equals() async {
await verifyVersion('>=2.6.0', '''
extension E on int {
int get a => 0;
}
void f() {
E(0).a;
}
''');
}
test_extensionOverride_lessThan() async {
await verifyVersion('>=2.2.0', '''
extension E on int {
int get a => 0;
}
void f() {
E(0).a;
}
''', expectedErrors: [
error(WarningCode.SDK_VERSION_EXTENSION_METHODS, 0, 9),
error(WarningCode.SDK_VERSION_EXTENSION_METHODS, 54, 1),
]);
}
}

View file

@ -1,37 +0,0 @@
// Copyright (c) 2018, 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 'sdk_constraint_verifier_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(SdkVersionSetLiteralTest);
});
}
@reflectiveTest
class SdkVersionSetLiteralTest extends SdkConstraintVerifierTest {
test_equals() async {
await verifyVersion('>=2.2.0', '''
Set<int> zero() => <int>{0};
''');
}
test_greaterThan() async {
await verifyVersion('>=2.3.0', '''
Set<int> zero() => <int>{0};
''');
}
test_lessThan() async {
await verifyVersion('>=2.1.0', '''
Set<int> zero() => <int>{0};
''', expectedErrors: [
error(WarningCode.SDK_VERSION_SET_LITERAL, 19, 8),
]);
}
}

View file

@ -806,7 +806,6 @@ void f() {
E(0).foo();
}
''', expectedErrors: [
error(WarningCode.SDK_VERSION_SINCE, 33, 1),
error(WarningCode.SDK_VERSION_SINCE, 38, 3),
]);
}

View file

@ -722,19 +722,14 @@ import 'return_type_invalid_for_catch_error_test.dart'
import 'return_without_value_test.dart' as return_without_value;
import 'sdk_version_as_expression_in_const_context_test.dart'
as sdk_version_as_expression_in_const_context;
import 'sdk_version_async_exported_from_core_test.dart'
as sdk_version_async_exported_from_core;
import 'sdk_version_bool_operator_in_const_context_test.dart'
as sdk_version_bool_operator_in_const_context;
import 'sdk_version_eq_eq_operator_test.dart' as sdk_version_eq_eq_operator;
import 'sdk_version_extension_methods_test.dart'
as sdk_version_extension_methods;
import 'sdk_version_gt_gt_gt_operator_test.dart'
as sdk_version_gt_gt_gt_operator;
import 'sdk_version_is_expression_in_const_context_test.dart'
as sdk_version_is_expression_in_const_context;
import 'sdk_version_never_test.dart' as sdk_version_never;
import 'sdk_version_set_literal_test.dart' as sdk_version_set_literal;
import 'sdk_version_since_test.dart' as sdk_version_since;
import 'sealed_class_subtype_outside_of_library_test.dart'
as sealed_class_subtype_outside_of_library;
@ -1353,14 +1348,11 @@ main() {
return_without_value.main();
set_element_from_deferred_library.main();
sdk_version_as_expression_in_const_context.main();
sdk_version_async_exported_from_core.main();
sdk_version_bool_operator_in_const_context.main();
sdk_version_eq_eq_operator.main();
sdk_version_extension_methods.main();
sdk_version_gt_gt_gt_operator.main();
sdk_version_is_expression_in_const_context.main();
sdk_version_never.main();
sdk_version_set_literal.main();
sdk_version_since.main();
sealed_class_subtype_outside_of_library.main();
set_element_type_not_assignable.main();

View file

@ -17847,55 +17847,6 @@ int f() {
}
{% endprettify %}
### sdk_version_async_exported_from_core
_The class '{0}' wasn't exported from 'dart:core' until version 2.1, but this
code is required to be able to run on earlier versions._
#### Description
The analyzer produces this diagnostic when either the class `Future` or
`Stream` is referenced in a library that doesn't import `dart:async` in
code that has an SDK constraint whose lower bound is less than 2.1.0. In
earlier versions, these classes weren't defined in `dart:core`, so the
import was necessary.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.1.0:
```yaml
environment:
sdk: '>=2.0.0 <2.4.0'
```
In the package that has that pubspec, code like the following produces this
diagnostic:
{% prettify dart tag=pre+code %}
void f([!Future!] f) {}
{% endprettify %}
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the classes to be referenced:
```yaml
environment:
sdk: '>=2.1.0 <2.4.0'
```
If you need to support older versions of the SDK, then import the
`dart:async` library.
{% prettify dart tag=pre+code %}
import 'dart:async';
void f(Future f) {}
{% endprettify %}
### sdk_version_as_expression_in_const_context
_The use of an as expression in a constant expression wasn't supported until
@ -18099,61 +18050,6 @@ const C b = null;
bool same = a == b;
{% endprettify %}
### sdk_version_extension_methods
_Extension methods weren't supported until version 2.6.0, but this code is
required to be able to run on earlier versions._
#### Description
The analyzer produces this diagnostic when an extension declaration or an
extension override is found in code that has an SDK constraint whose lower
bound is less than 2.6.0. Using extensions wasn't supported in earlier
versions, so this code won't be able to run against earlier versions of the
SDK.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.6.0:
```yaml
environment:
sdk: '>=2.4.0 <2.7.0'
```
In the package that has that pubspec, code like the following produces
this diagnostic:
{% prettify dart tag=pre+code %}
[!extension!] E on String {
void sayHello() {
print('Hello $this');
}
}
{% endprettify %}
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the syntax to be used:
```yaml
environment:
sdk: '>=2.6.0 <2.7.0'
```
If you need to support older versions of the SDK, then rewrite the code to
not make use of extensions. The most common way to do this is to rewrite
the members of the extension as top-level functions (or methods) that take
the value that would have been bound to `this` as a parameter:
{% prettify dart tag=pre+code %}
void sayHello(String s) {
print('Hello $s');
}
{% endprettify %}
### sdk_version_gt_gt_gt_operator
_The operator '>>>' wasn't supported until version 2.14.0, but this code is
@ -18305,52 +18201,6 @@ not reference this class:
dynamic x;
{% endprettify %}
### sdk_version_set_literal
_Set literals weren't supported until version 2.2, but this code is required to
be able to run on earlier versions._
#### Description
The analyzer produces this diagnostic when a set literal is found in code
that has an SDK constraint whose lower bound is less than 2.2.0. Set
literals weren't supported in earlier versions, so this code won't be able
to run against earlier versions of the SDK.
#### Example
Here's an example of a pubspec that defines an SDK constraint with a lower
bound of less than 2.2.0:
```yaml
environment:
sdk: '>=2.1.0 <2.4.0'
```
In the package that has that pubspec, code like the following produces this
diagnostic:
{% prettify dart tag=pre+code %}
var s = [!<int>{}!];
{% endprettify %}
#### Common fixes
If you don't need to support older versions of the SDK, then you can
increase the SDK constraint to allow the syntax to be used:
```yaml
environment:
sdk: '>=2.2.0 <2.4.0'
```
If you do need to support older versions of the SDK, then replace the set
literal with code that creates the set without the use of a literal:
{% prettify dart tag=pre+code %}
var s = new Set<int>();
{% endprettify %}
### set_element_type_not_assignable
_The element type '{0}' can't be assigned to the set type '{1}'._