From e9983a51b2767e36413270a1298360d208296417 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 16 Jan 2024 20:45:08 +0000 Subject: [PATCH] Extension type. Issue 54593. Report ParserErrorCode.MEMBER_WITH_CLASS_NAME for the representation field. Bug: https://github.com/dart-lang/sdk/issues/54593 Change-Id: Ia8f6d6b78f4a49d20c5573e8fa31115c5dbda415 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/345829 Reviewed-by: Phil Quitslund Commit-Queue: Konstantin Shcheglov Reviewed-by: Brian Wilkerson --- pkg/analyzer/lib/src/fasta/ast_builder.dart | 8 ++++++ .../src/dart/parser/extension_type_test.dart | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart index d4c6a342dd5e..e5bffb313f84 100644 --- a/pkg/analyzer/lib/src/fasta/ast_builder.dart +++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart @@ -1656,6 +1656,14 @@ class AstBuilder extends StackListener { rightParenthesis: rightParenthesis, ); } + // Check for extension type name conflict. + var representationName = representation.fieldName; + if (representationName.lexeme == builder.name.lexeme) { + errorReporter.errorReporter?.reportErrorForToken( + ParserErrorCode.MEMBER_WITH_CLASS_NAME, + representationName, + ); + } declarations.add( builder.build( typeKeyword: typeKeyword, diff --git a/pkg/analyzer/test/src/dart/parser/extension_type_test.dart b/pkg/analyzer/test/src/dart/parser/extension_type_test.dart index 3ace940083ba..c4e2b2211d1e 100644 --- a/pkg/analyzer/test/src/dart/parser/extension_type_test.dart +++ b/pkg/analyzer/test/src/dart/parser/extension_type_test.dart @@ -166,6 +166,31 @@ ExtensionTypeDeclaration '''); } + test_error_fieldName_asDeclaration() { + final parseResult = parseStringWithErrors(r''' +extension type A(int A) {} +'''); + parseResult.assertErrors([ + error(ParserErrorCode.MEMBER_WITH_CLASS_NAME, 21, 1), + ]); + + final node = parseResult.findNode.singleExtensionTypeDeclaration; + assertParsedNodeText(node, r''' +ExtensionTypeDeclaration + extensionKeyword: extension + typeKeyword: type + name: A + representation: RepresentationDeclaration + leftParenthesis: ( + fieldType: NamedType + name: int + fieldName: A + rightParenthesis: ) + leftBracket: { + rightBracket: } +'''); + } + test_error_formalParameterModifier_covariant_method_instance() { final parseResult = parseStringWithErrors(r''' extension type A(int it) {