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) {