Extension types. Report when has representation self reference.

Change-Id: I729515f92c4c31530b61c7f9d71507fbf922787d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319180
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-08-08 20:39:40 +00:00 committed by Commit Queue
parent 5930cd6d55
commit 36c578f75c
7 changed files with 108 additions and 1 deletions

View file

@ -644,6 +644,8 @@ CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT:
Offer to delete the invalid member.
CompileTimeErrorCode.EXTENSION_TYPE_IMPLEMENTS_DISALLOWED_TYPE:
status: noFix
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF:
status: noFix
CompileTimeErrorCode.EXTERNAL_FIELD_CONSTRUCTOR_INITIALIZER:
status: needsFix
notes: |-

View file

@ -1623,6 +1623,14 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
"Try specifying a different type, or remove the type from the list.",
);
/// No parameters.
static const CompileTimeErrorCode
EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF = CompileTimeErrorCode(
'EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF',
"The extension type representation can't depend on itself.",
correctionMessage: "Try specifying a different type.",
);
static const CompileTimeErrorCode EXTERNAL_FIELD_CONSTRUCTOR_INITIALIZER =
CompileTimeErrorCode(
'EXTERNAL_WITH_INITIALIZER',

View file

@ -194,6 +194,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_INSTANCE_FIELD,
CompileTimeErrorCode.EXTENSION_TYPE_DECLARES_MEMBER_OF_OBJECT,
CompileTimeErrorCode.EXTENSION_TYPE_IMPLEMENTS_DISALLOWED_TYPE,
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
CompileTimeErrorCode.EXTERNAL_FIELD_CONSTRUCTOR_INITIALIZER,
CompileTimeErrorCode.EXTERNAL_FIELD_INITIALIZER,
CompileTimeErrorCode.EXTERNAL_VARIABLE_INITIALIZER,

View file

@ -688,7 +688,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
}
@override
void visitExtensionTypeDeclaration(ExtensionTypeDeclaration node) {
void visitExtensionTypeDeclaration(
covariant ExtensionTypeDeclarationImpl node,
) {
var outerClass = _enclosingClass;
try {
final element = node.declaredElement!;
@ -707,6 +709,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
_constructorFieldsVerifier.enterExtensionType(node, declarationElement);
_checkForNonCovariantTypeParameterPositionInRepresentationType(
node, element);
_checkForExtensionTypeRepresentationDependsOnItself(node, element);
// TODO(scheglov) Add checks.
super.visitExtensionTypeDeclaration(node);
@ -2887,6 +2890,18 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
}
}
void _checkForExtensionTypeRepresentationDependsOnItself(
ExtensionTypeDeclarationImpl node,
ExtensionTypeElementImpl element,
) {
if (element.hasSelfReference) {
errorReporter.reportErrorForToken(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
node.name,
);
}
}
/// Verify that the given field formal [parameter] is in a constructor
/// declaration.
///

View file

@ -5069,6 +5069,10 @@ CompileTimeErrorCode:
comment: |-
Parameters:
0: the display string of the disallowed type
EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF:
problemMessage: "The extension type representation can't depend on itself."
correctionMessage: Try specifying a different type.
comment: No parameters.
EXTERNAL_FIELD_CONSTRUCTOR_INITIALIZER:
sharedName: EXTERNAL_WITH_INITIALIZER
problemMessage: External fields can't have initializers.

View file

@ -0,0 +1,74 @@
// 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(ExtensionTypeRepresentationDependsOnItselfTest);
});
}
@reflectiveTest
class ExtensionTypeRepresentationDependsOnItselfTest
extends PubPackageResolutionTest {
test_depends_cycle2_direct() async {
await assertErrorsInCode('''
extension type A(B it) {}
extension type B(A it) {}
''', [
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
15,
1),
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
42,
1),
]);
}
test_depends_cycle2_typeArgument() async {
await assertErrorsInCode('''
extension type A(List<B> it) {}
extension type B(List<A> it) {}
''', [
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
15,
1),
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
48,
1),
]);
}
test_depends_self_direct() async {
await assertErrorsInCode('''
extension type A(A it) {}
''', [
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
15,
1),
]);
}
test_depends_self_typeArgument() async {
await assertErrorsInCode('''
extension type A(List<A> it) {}
''', [
error(
CompileTimeErrorCode.EXTENSION_TYPE_REPRESENTATION_DEPENDS_ON_ITSELF,
15,
1),
]);
}
}

View file

@ -244,6 +244,8 @@ import 'extension_type_declares_member_of_object_test.dart'
as extension_type_declares_member_of_object;
import 'extension_type_implements_disallowed_type_test.dart'
as extension_type_implements_disallowed_type;
import 'extension_type_representation_depends_on_itself_test.dart'
as extension_type_representation_depends_on_itself;
import 'external_field_constructor_initializer_test.dart'
as external_field_constructor_initializer;
import 'external_field_initializer_test.dart' as external_field_initializer;
@ -1045,6 +1047,7 @@ main() {
extension_type_declares_instance_field.main();
extension_type_declares_member_of_object.main();
extension_type_implements_disallowed_type.main();
extension_type_representation_depends_on_itself.main();
external_field_constructor_initializer.main();
external_field_initializer.main();
external_variable_initializer.main();