Extension types. Report super constructor invocations.
Change-Id: Ib3da4c7f19864bbbb6d9f53769375fe9f1cf7daa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319300
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
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 6258af9..b1d3008 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 @@
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 603aa60..30d1e6e 100644
--- a/pkg/analyzer/lib/src/error/codes.g.dart
+++ b/pkg/analyzer/lib/src/error/codes.g.dart
@@ -1599,6 +1599,14 @@
);
/// 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(
'EXTENSION_TYPE_DECLARES_INSTANCE_FIELD',
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 79f9b8b..59a0305 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 @@
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 dbf1cab..6d26aa8 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -1286,6 +1286,7 @@
);
_isInConstructorInitializer = true;
try {
+ _checkForExtensionTypeConstructorWithSuperInvocation(node);
super.visitSuperConstructorInvocation(node);
} finally {
_isInConstructorInitializer = false;
@@ -2877,6 +2878,17 @@
}
}
+ 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 6dcc99e..f13399e 100644
--- a/pkg/analyzer/messages.yaml
+++ b/pkg/analyzer/messages.yaml
@@ -5056,6 +5056,10 @@
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 0000000..48a32f9
--- /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: <null>
+ staticType: null
+ argumentList: ArgumentList
+ leftParenthesis: (
+ rightParenthesis: )
+ staticElement: <null>
+''');
+ }
+
+ 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: <null>
+''');
+ }
+}
diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart
index ccf6ab2..0f992dc 100644
--- a/pkg/analyzer/test/src/diagnostics/test_all.dart
+++ b/pkg/analyzer/test/src/diagnostics/test_all.dart
@@ -238,6 +238,8 @@
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 @@
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();