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>
This commit is contained in:
Konstantin Shcheglov 2023-08-09 16:50:56 +00:00 committed by Commit Queue
parent 3564f5c163
commit ffcb38d702
7 changed files with 101 additions and 0 deletions

View file

@ -636,6 +636,10 @@ CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE:
Replace the `..` with `.`. Replace the `..` with `.`.
CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS: CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS:
status: noFix status: noFix
CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION:
status: needsFix
notes: |-
Remove it.
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD: CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD:
status: noFix status: noFix
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT: CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT:

View file

@ -1598,6 +1598,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
hasPublishedDocs: true, 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. /// No parameters.
static const CompileTimeErrorCode EXTENSION_TYPE_DECLARES_INSTANCE_FIELD = static const CompileTimeErrorCode EXTENSION_TYPE_DECLARES_INSTANCE_FIELD =
CompileTimeErrorCode( CompileTimeErrorCode(

View file

@ -191,6 +191,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.EXTENSION_OVERRIDE_ARGUMENT_NOT_ASSIGNABLE, CompileTimeErrorCode.EXTENSION_OVERRIDE_ARGUMENT_NOT_ASSIGNABLE,
CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS, CompileTimeErrorCode.EXTENSION_OVERRIDE_WITHOUT_ACCESS,
CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE, CompileTimeErrorCode.EXTENSION_OVERRIDE_WITH_CASCADE,
CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION,
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD,
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT, CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT,
CompileTimeErrorCode.EXTENSION_TYPE_IMPLEMENTS_DISALLOWED_TYPE, CompileTimeErrorCode.EXTENSION_TYPE_IMPLEMENTS_DISALLOWED_TYPE,

View file

@ -1286,6 +1286,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
); );
_isInConstructorInitializer = true; _isInConstructorInitializer = true;
try { try {
_checkForExtensionTypeConstructorWithSuperInvocation(node);
super.visitSuperConstructorInvocation(node); super.visitSuperConstructorInvocation(node);
} finally { } finally {
_isInConstructorInitializer = false; _isInConstructorInitializer = false;
@ -2877,6 +2878,17 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
} }
} }
void _checkForExtensionTypeConstructorWithSuperInvocation(
SuperConstructorInvocation node,
) {
if (_enclosingClass is ExtensionTypeElement) {
errorReporter.reportErrorForToken(
CompileTimeErrorCode.EXTENSION_TYPE_CONSTRUCTOR_WITH_SUPER_INVOCATION,
node.superKeyword,
);
}
}
void _checkForExtensionTypeDeclaresInstanceField(FieldDeclaration node) { void _checkForExtensionTypeDeclaresInstanceField(FieldDeclaration node) {
if (_enclosingClass is! ExtensionTypeElement) { if (_enclosingClass is! ExtensionTypeElement) {
return; return;

View file

@ -5056,6 +5056,10 @@ CompileTimeErrorCode:
If there are multiple cascaded accesses, you'll need to duplicate the If there are multiple cascaded accesses, you'll need to duplicate the
extension override for each one. 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: EXTENSION_TYPE_DECLARES_INSTANCE_FIELD:
problemMessage: "Extension types can't declare instance fields." problemMessage: "Extension types can't declare instance fields."
correctionMessage: Try replacing the field with a getter. correctionMessage: Try replacing the field with a getter.

View file

@ -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>
''');
}
}

View file

@ -238,6 +238,8 @@ import 'extension_override_with_cascade_test.dart'
as extension_override_with_cascade; as extension_override_with_cascade;
import 'extension_override_without_access_test.dart' import 'extension_override_without_access_test.dart'
as extension_override_without_access; 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' import 'extension_type_declares_instance_field_test.dart'
as extension_type_declares_instance_field; as extension_type_declares_instance_field;
import 'extension_type_declares_member_of_object_test.dart' import 'extension_type_declares_member_of_object_test.dart'
@ -1046,6 +1048,7 @@ main() {
extension_override_argument_not_assignable.main(); extension_override_argument_not_assignable.main();
extension_override_with_cascade.main(); extension_override_with_cascade.main();
extension_override_without_access.main(); extension_override_without_access.main();
extension_type_constructor_with_super_invocation.main();
extension_type_declares_instance_field.main(); extension_type_declares_instance_field.main();
extension_type_declares_member_of_object.main(); extension_type_declares_member_of_object.main();
extension_type_implements_disallowed_type.main(); extension_type_implements_disallowed_type.main();