1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-08 12:06:26 +00:00

Extension types. Report an error for super formal parameters.

Change-Id: I0e577dbe9d27913ce633951c68b2891f48699ca6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319560
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-08-09 18:45:51 +00:00
parent 00e30a89ff
commit 0540ac84e1
8 changed files with 123 additions and 0 deletions

View File

@ -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: |-

View File

@ -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(

View File

@ -191,6 +191,7 @@ const List<ErrorCode> 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,

View File

@ -1297,6 +1297,15 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
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)) {

View File

@ -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();

View File

@ -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.

View File

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

View File

@ -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();