diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index 6258af99a73..b1d30088cf9 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -636,6 +636,10 @@ CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE: Replace the `..` with `.`. CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS: status: noFix +CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION: + status: needsFix + notes: |- + Remove it. CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD: status: noFix CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT: diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index 603aa60e3ec..30d1e6e8454 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -1598,6 +1598,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { hasPublishedDocs: true, ); + /// No parameters. + static const CompileTimeErrorCode + EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION = CompileTimeErrorCode( + 'EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION', + "Extension type constructors can't include superinitializers.", + correctionMessage: "Try removing the superconstructor invocation.", + ); + /// No parameters. static const CompileTimeErrorCode EXTENSION_TYPE_DECLARES_INSTANCE_FIELD = CompileTimeErrorCode( diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart index 79f9b8bc1e8..59a0305eec1 100644 --- a/pkg/analyzer/lib/src/error/error_code_values.g.dart +++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart @@ -191,6 +191,7 @@ const List errorCodeValues = [ CompileTimeErrorCode.EXTENSION_OVERRIDE_ARGUMENT_NOT_ASSIGNABLE, CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS, CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE, + CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT, CompileTimeErrorCode.EXTENSION_TYPE_IMPLEMENTS_DISALLOWED_TYPE, diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index dbf1caba96e..6d26aa83422 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -1286,6 +1286,7 @@ class ErrorVerifier extends RecursiveAstVisitor ); _isInConstructorInitializer = true; try { + _checkForExtensionTypeConstructorWithSuperInvocation(node); super.visitSuperConstructorInvocation(node); } finally { _isInConstructorInitializer = false; @@ -2877,6 +2878,17 @@ class ErrorVerifier extends RecursiveAstVisitor } } + void _checkForExtensionTypeConstructorWithSuperInvocation( + SuperConstructorInvocation node, + ) { + if (_enclosingClass is ExtensionTypeElement) { + errorReporter.reportErrorForToken( + CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION, + node.superKeyword, + ); + } + } + void _checkForExtensionTypeDeclaresInstanceField(FieldDeclaration node) { if (_enclosingClass is! ExtensionTypeElement) { return; diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index 6dcc99ef935..f13399e35a4 100644 --- a/pkg/analyzer/messages.yaml +++ b/pkg/analyzer/messages.yaml @@ -5056,6 +5056,10 @@ CompileTimeErrorCode: If there are multiple cascaded accesses, you'll need to duplicate the extension override for each one. + EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION: + problemMessage: "Extension type constructors can't include superinitializers." + correctionMessage: Try removing the superconstructor invocation. + comment: No parameters. EXTENSION_TYPE_DECLARES_INSTANCE_FIELD: problemMessage: "Extension types can't declare instance fields." correctionMessage: Try replacing the field with a getter. diff --git a/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_invocation_test.dart b/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_invocation_test.dart new file mode 100644 index 00000000000..48a32f9952d --- /dev/null +++ b/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_invocation_test.dart @@ -0,0 +1,69 @@ +// Copyright (c) 2023, 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/context_collection_resolution.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(ExtensionTypeConstructorWithSuperInvocationTest); + }); +} + +@reflectiveTest +class ExtensionTypeConstructorWithSuperInvocationTest + extends PubPackageResolutionTest { + test_named() async { + await assertErrorsInCode(''' +extension type E(int it) { + E.named() : it = 0, super.named(); +} +''', [ + error( + CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION, + 49, + 5), + ]); + + final node = findNode.singleSuperConstructorInvocation; + assertResolvedNodeText(node, r''' +SuperConstructorInvocation + superKeyword: super + period: . + constructorName: SimpleIdentifier + token: named + staticElement: + staticType: null + argumentList: ArgumentList + leftParenthesis: ( + rightParenthesis: ) + staticElement: +'''); + } + + test_unnamed() async { + await assertErrorsInCode(''' +extension type E(int it) { + E.named() : it = 0, super(); +} +''', [ + error( + CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION, + 49, + 5), + ]); + + final node = findNode.singleSuperConstructorInvocation; + assertResolvedNodeText(node, r''' +SuperConstructorInvocation + superKeyword: super + argumentList: ArgumentList + leftParenthesis: ( + rightParenthesis: ) + staticElement: +'''); + } +} diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart index ccf6ab2a5a3..0f992dc24ea 100644 --- a/pkg/analyzer/test/src/diagnostics/test_all.dart +++ b/pkg/analyzer/test/src/diagnostics/test_all.dart @@ -238,6 +238,8 @@ import 'extension_override_with_cascade_test.dart' as extension_override_with_cascade; import 'extension_override_without_access_test.dart' as extension_override_without_access; +import 'extension_type_constructor_with_super_invocation_test.dart' + as extension_type_constructor_with_super_invocation; import 'extension_type_declares_instance_field_test.dart' as extension_type_declares_instance_field; import 'extension_type_declares_member_of_object_test.dart' @@ -1046,6 +1048,7 @@ main() { extension_override_argument_not_assignable.main(); extension_override_with_cascade.main(); extension_override_without_access.main(); + extension_type_constructor_with_super_invocation.main(); extension_type_declares_instance_field.main(); extension_type_declares_member_of_object.main(); extension_type_implements_disallowed_type.main();