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 b1d30088cf9..e06a1458a9b 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_FORMAL_PARAMETER: + status: needsFix + notes: |- + Remove it. CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION: status: needsFix notes: |- diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index 30d1e6e8454..7515bea6813 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -1598,6 +1598,15 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { hasPublishedDocs: true, ); + /// No parameters. + static const CompileTimeErrorCode + EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_FORMAL_PARAMETER = + CompileTimeErrorCode( + 'EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_FORMAL_PARAMETER', + "Extension type constructors can't declare super formal parameters.", + correctionMessage: "Try removing the super formal parameter declaration.", + ); + /// No parameters. static const CompileTimeErrorCode EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION = 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 59a0305eec1..6b2f1df8d74 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_FORMAL_PARAMETER, CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT, diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 6d26aa83422..104a89bbf6d 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -1297,6 +1297,15 @@ class ErrorVerifier extends RecursiveAstVisitor void visitSuperFormalParameter(SuperFormalParameter node) { super.visitSuperFormalParameter(node); + if (_enclosingClass is ExtensionTypeElement) { + errorReporter.reportErrorForToken( + CompileTimeErrorCode + .EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_FORMAL_PARAMETER, + node.superKeyword, + ); + return; + } + var constructor = node.parentFormalParameterList.parent; if (!(constructor is ConstructorDeclaration && constructor.isNonRedirectingGenerative)) { diff --git a/pkg/analyzer/lib/src/test_utilities/find_node.dart b/pkg/analyzer/lib/src/test_utilities/find_node.dart index 2f6ed82c805..4e76bddc7da 100644 --- a/pkg/analyzer/lib/src/test_utilities/find_node.dart +++ b/pkg/analyzer/lib/src/test_utilities/find_node.dart @@ -48,6 +48,8 @@ class FindNode { ConditionalExpression get singleConditionalExpression => _single(); + ConstructorDeclaration get singleConstructorDeclaration => _single(); + ConstructorFieldInitializer get singleConstructorFieldInitializer => _single(); @@ -142,6 +144,8 @@ class FindNode { SuperConstructorInvocation get singleSuperConstructorInvocation => _single(); + SuperFormalParameter get singleSuperFormalParameter => _single(); + SwitchCase get singleSwitchCase => _single(); SwitchExpression get singleSwitchExpression => _single(); diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index 1deea8654e5..6ea40772c7f 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_FORMAL_PARAMETER: + problemMessage: "Extension type constructors can't declare super formal parameters." + correctionMessage: Try removing the super formal parameter declaration. + comment: No parameters. EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION: problemMessage: "Extension type constructors can't include superinitializers." correctionMessage: Try removing the superconstructor invocation. diff --git a/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_formal_parameter_test.dart b/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_formal_parameter_test.dart new file mode 100644 index 00000000000..55c4f241768 --- /dev/null +++ b/pkg/analyzer/test/src/diagnostics/extension_type_constructor_with_super_formal_parameter_test.dart @@ -0,0 +1,89 @@ +// 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(ExtensionTypeConstructorWithSuperFormalParameterTest); + }); +} + +@reflectiveTest +class ExtensionTypeConstructorWithSuperFormalParameterTest + extends PubPackageResolutionTest { + test_named() async { + await assertErrorsInCode(''' +extension type E(int it) { + E.named(this.it, {super.foo}); +} +''', [ + error( + CompileTimeErrorCode + .EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_FORMAL_PARAMETER, + 47, + 5), + ]); + + final node = findNode.singleFormalParameterList; + assertResolvedNodeText(node, r''' +FormalParameterList + leftParenthesis: ( + parameter: FieldFormalParameter + thisKeyword: this + period: . + name: it + declaredElement: self::@extensionType::E::@constructor::named::@parameter::it + type: int + leftDelimiter: { + parameter: DefaultFormalParameter + parameter: SuperFormalParameter + superKeyword: super + period: . + name: foo + declaredElement: self::@extensionType::E::@constructor::named::@parameter::foo + type: dynamic + declaredElement: self::@extensionType::E::@constructor::named::@parameter::foo + type: dynamic + rightDelimiter: } + rightParenthesis: ) +'''); + } + + test_positional() async { + await assertErrorsInCode(''' +extension type E(int it) { + E.named(this.it, super.foo); +} +''', [ + error( + CompileTimeErrorCode + .EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_FORMAL_PARAMETER, + 46, + 5), + ]); + + final node = findNode.singleFormalParameterList; + assertResolvedNodeText(node, r''' +FormalParameterList + leftParenthesis: ( + parameter: FieldFormalParameter + thisKeyword: this + period: . + name: it + declaredElement: self::@extensionType::E::@constructor::named::@parameter::it + type: int + parameter: SuperFormalParameter + superKeyword: super + period: . + name: foo + declaredElement: self::@extensionType::E::@constructor::named::@parameter::foo + type: dynamic + rightParenthesis: ) +'''); + } +} diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart index 0f992dc24ea..7facc11368e 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_formal_parameter_test.dart' + as extension_type_constructor_with_super_formal_parameter; import 'extension_type_constructor_with_super_invocation_test.dart' as extension_type_constructor_with_super_invocation; import 'extension_type_declares_instance_field_test.dart' @@ -1048,6 +1050,7 @@ main() { extension_override_argument_not_assignable.main(); extension_override_with_cascade.main(); extension_override_without_access.main(); + extension_type_constructor_with_super_formal_parameter.main(); extension_type_constructor_with_super_invocation.main(); extension_type_declares_instance_field.main(); extension_type_declares_member_of_object.main();