diff --git a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart index b0e6908369f1..a55d6644f295 100644 --- a/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart +++ b/pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart @@ -24,7 +24,6 @@ import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; import 'package:analyzer/src/dart/element/type_provider.dart'; import 'package:analyzer/src/dart/element/type_system.dart'; import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart'; -import 'package:analyzer/src/dart/resolver/legacy_type_asserter.dart'; import 'package:analyzer/src/dart/resolver/resolution_visitor.dart'; import 'package:analyzer/src/error/best_practices_verifier.dart'; import 'package:analyzer/src/error/codes.dart'; @@ -317,8 +316,6 @@ class LibraryAnalyzer { } } - assert(units.values.every(LegacyTypeAsserter.assertLegacyTypes)); - _checkForInconsistentLanguageVersionOverride(units); // This must happen after all other diagnostics have been computed but diff --git a/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart b/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart deleted file mode 100644 index 4f0d2e760447..000000000000 --- a/pkg/analyzer/lib/src/dart/resolver/legacy_type_asserter.dart +++ /dev/null @@ -1,177 +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/dart/analysis/features.dart'; -import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/dart/ast/visitor.dart'; -import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/dart/element/nullability_suffix.dart'; -import 'package:analyzer/dart/element/type.dart'; - -/// A visitor to assert that legacy libraries deal with legacy types. -/// -/// Intended to be used via the static method -/// [LegacyTypeAsserter.assertLegacyTypes], inside an `assert()` node. -/// -/// Has a defense against being accidentally run outside of an assert statement, -/// but that can be overridden if needed. -/// -/// Checks that the static type of every node, as well as the elements of many -/// nodes, have legacy types, and asserts that the legacy types are deep legacy -/// types. -class LegacyTypeAsserter extends GeneralizingAstVisitor { - final Set _visitedTypes = {}; - - LegacyTypeAsserter({bool requireIsDebug = true}) { - if (requireIsDebug) { - bool isDebug = false; - - assert(() { - isDebug = true; - return true; - }()); - - if (!isDebug) { - throw UnsupportedError( - 'Legacy type asserter is being run outside of a debug environment'); - } - } - } - - @override - void visitAssignmentExpression(AssignmentExpression node) { - _assertLegacyElement(node.readElement); - _assertLegacyElement(node.writeElement); - _assertLegacyType(node.readType); - _assertLegacyType(node.writeType); - super.visitAssignmentExpression(node); - } - - @override - void visitClassMember(ClassMember node) { - final element = node.declaredElement; - if (element is ExecutableElement) { - _assertLegacyType(element.type); - } - super.visitClassMember(node); - } - - @override - void visitCompilationUnit(CompilationUnit node) { - if (!node.featureSet.isEnabled(Feature.non_nullable)) { - super.visitCompilationUnit(node); - } - } - - @override - void visitDeclaredIdentifier(DeclaredIdentifier node) { - _assertLegacyType(node.declaredElement?.type); - super.visitDeclaredIdentifier(node); - } - - @override - void visitExpression(Expression node) { - _assertLegacyType(node.staticType); - _assertLegacyType(node.staticParameterElement?.type); - super.visitExpression(node); - } - - @override - void visitFormalParameter(FormalParameter node) { - _assertLegacyType(node.declaredElement?.type); - super.visitFormalParameter(node); - } - - @override - void visitFunctionDeclaration(FunctionDeclaration node) { - _assertLegacyType(node.declaredElement?.type); - super.visitFunctionDeclaration(node); - } - - @override - void visitInvocationExpression(InvocationExpression node) { - _assertLegacyType(node.staticInvokeType); - node.typeArgumentTypes?.forEach(_assertLegacyType); - return super.visitInvocationExpression(node); - } - - @override - void visitNamedType(NamedType node) { - _assertLegacyType(node.type); - super.visitNamedType(node); - } - - @override - void visitTypeAnnotation(TypeAnnotation node) { - _assertLegacyType(node.type); - super.visitTypeAnnotation(node); - } - - @override - void visitVariableDeclaration(VariableDeclaration node) { - _assertLegacyType(node.declaredElement?.type); - super.visitVariableDeclaration(node); - } - - void _assertLegacyElement(Element? element) { - if (element is ExecutableElement) { - _assertLegacyType(element.type); - } else if (element is VariableElement) { - _assertLegacyType(element.type); - } - } - - void _assertLegacyType(DartType? type) { - if (type == null) { - return; - } - - if (type is DynamicType || type is InvalidType || type is VoidType) { - return; - } - - if (type is NeverType && type.isDartCoreNull) { - // Never?, which is ok. - // - // Note: we could allow Null? and Null, but we really should be able to - // guarantee that we are only working with Null*, so that's what this - // currently does. - return; - } - - if (!_visitedTypes.add(type)) { - return; - } - - type.alias?.typeArguments.forEach(_assertLegacyType); - - if (type is TypeParameterType) { - _assertLegacyType(type.bound); - } else if (type is InterfaceType) { - type.typeArguments.forEach(_assertLegacyType); - } else if (type is FunctionType) { - _assertLegacyType(type.returnType); - type.parameters.map((param) => param.type).forEach(_assertLegacyType); - type.typeFormals.map((param) => param.bound).forEach(_assertLegacyType); - } - - if (type.nullabilitySuffix == NullabilitySuffix.star) { - return; - } - - throw StateError('Expected all legacy types, but got ' - '${type.getDisplayString(withNullability: true)} ' - '(${type.runtimeType})'); - } - - static bool assertLegacyTypes(CompilationUnit compilationUnit) { - if (compilationUnit.languageVersionToken != null && - compilationUnit.directives.whereType().isNotEmpty) { - return true; - } - - LegacyTypeAsserter().visitCompilationUnit(compilationUnit); - return true; - } -} diff --git a/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart b/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart deleted file mode 100644 index 149fd911aec0..000000000000 --- a/pkg/analyzer/test/src/dart/resolver/legacy_type_asserter_test.dart +++ /dev/null @@ -1,135 +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/dart/element/nullability_suffix.dart'; -import 'package:analyzer/dart/element/type.dart'; -import 'package:analyzer/src/dart/ast/ast.dart'; -import 'package:analyzer/src/dart/element/type.dart'; -import 'package:analyzer/src/dart/resolver/legacy_type_asserter.dart'; -import 'package:test/test.dart'; -import 'package:test_reflective_loader/test_reflective_loader.dart'; - -import '../resolution/context_collection_resolution.dart'; - -main() { - defineReflectiveSuite(() { - defineReflectiveTests(LegacyTypeAsserterTest); - }); -} - -@reflectiveTest -class LegacyTypeAsserterTest extends PubPackageResolutionTest { - InterfaceType get _intNone { - return typeProvider.intElement.instantiate( - typeArguments: const [], - nullabilitySuffix: NullabilitySuffix.none, - ); - } - - InterfaceType get _intQuestion { - return typeProvider.intElement.instantiate( - typeArguments: const [], - nullabilitySuffix: NullabilitySuffix.question, - ); - } - - InterfaceType get _intStar { - return typeProvider.intElement.instantiate( - typeArguments: const [], - nullabilitySuffix: NullabilitySuffix.star, - ); - } - - test_nullableUnit_expressionStaticType_bottom() async { - await _buildUnit(() => NeverTypeImpl.instance); - expect(() { - LegacyTypeAsserter.assertLegacyTypes(result.unit); - }, throwsStateError); - } - - test_nullableUnit_expressionStaticType_bottomQuestion() async { - await _buildUnit(() => NeverTypeImpl.instanceNullable); - LegacyTypeAsserter.assertLegacyTypes(result.unit); - } - - test_nullableUnit_expressionStaticType_dynamic() async { - await _buildUnit(() => typeProvider.dynamicType); - LegacyTypeAsserter.assertLegacyTypes(result.unit); - } - - test_nullableUnit_expressionStaticType_nonNull() async { - await _buildUnit(() => _intNone); - expect(() { - LegacyTypeAsserter.assertLegacyTypes(result.unit); - }, throwsStateError); - } - - test_nullableUnit_expressionStaticType_nonNullTypeArgument() async { - await _buildUnit( - () => typeProvider.listElement.instantiate( - typeArguments: [_intNone], - nullabilitySuffix: NullabilitySuffix.star, - ), - ); - expect(() { - LegacyTypeAsserter.assertLegacyTypes(result.unit); - }, throwsStateError); - } - - test_nullableUnit_expressionStaticType_nonNullTypeParameter() async { - await _buildUnit( - () => typeProvider.listElement.instantiate( - typeArguments: [ - findElement.typeParameter('T').instantiate( - nullabilitySuffix: NullabilitySuffix.none, - ), - ], - nullabilitySuffix: NullabilitySuffix.star, - ), - ); - expect(() { - LegacyTypeAsserter.assertLegacyTypes(result.unit); - }, throwsStateError); - } - - test_nullableUnit_expressionStaticType_null() async { - await _buildUnit( - () => typeProvider.nullElement.instantiate( - typeArguments: [], - nullabilitySuffix: NullabilitySuffix.star, - ), - ); - LegacyTypeAsserter.assertLegacyTypes(result.unit); - } - - test_nullableUnit_expressionStaticType_question() async { - await _buildUnit(() => _intQuestion); - expect(() { - LegacyTypeAsserter.assertLegacyTypes(result.unit); - }, throwsStateError); - } - - test_nullableUnit_expressionStaticType_star() async { - await _buildUnit(() => _intStar); - LegacyTypeAsserter.assertLegacyTypes(result.unit); - } - - test_nullableUnit_expressionStaticType_void() async { - await _buildUnit(() => typeProvider.voidType); - LegacyTypeAsserter.assertLegacyTypes(result.unit); - } - - Future _buildUnit(DartType Function() getType) async { - await resolveTestCode(r''' -// @dart = 2.9 -void f(Object? foo) { - foo; -} -'''); - - final foo = findNode.simple('foo;'); - foo as SimpleIdentifierImpl; - foo.staticType = getType(); - } -} diff --git a/pkg/analyzer/test/src/dart/resolver/test_all.dart b/pkg/analyzer/test/src/dart/resolver/test_all.dart index f100f90e9ed5..e6430bd4d01d 100644 --- a/pkg/analyzer/test/src/dart/resolver/test_all.dart +++ b/pkg/analyzer/test/src/dart/resolver/test_all.dart @@ -5,11 +5,9 @@ import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'exit_detector_test.dart' as exit_detector; -import 'legacy_type_asserter_test.dart' as legacy_type_asserter; main() { defineReflectiveSuite(() { exit_detector.main(); - legacy_type_asserter.main(); }, name: 'resolver'); }