diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml index d3fc91eb7421..40a9e2bb2802 100644 --- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml +++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml @@ -252,6 +252,10 @@ CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME: status: noFix notes: |- The correction is to change the name. +CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME: + status: noFix + notes: |- + The correction is to change the name. CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME: status: noFix notes: |- diff --git a/pkg/analyzer/lib/src/error/codes.g.dart b/pkg/analyzer/lib/src/error/codes.g.dart index c3e0129ab24d..65ad4177d025 100644 --- a/pkg/analyzer/lib/src/error/codes.g.dart +++ b/pkg/analyzer/lib/src/error/codes.g.dart @@ -311,6 +311,17 @@ class CompileTimeErrorCode extends AnalyzerErrorCode { uniqueName: 'BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME', ); + /// Parameters: + /// 0: the built-in identifier that is being used + static const CompileTimeErrorCode BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME = + CompileTimeErrorCode( + 'BUILT_IN_IDENTIFIER_IN_DECLARATION', + "The built-in identifier '{0}' can't be used as an extension type name.", + correctionMessage: "Try choosing a different name for the extension type.", + hasPublishedDocs: true, + uniqueName: 'BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME', + ); + /// Parameters: /// 0: the built-in identifier that is being used static const CompileTimeErrorCode BUILT_IN_IDENTIFIER_AS_PREFIX_NAME = diff --git a/pkg/analyzer/lib/src/error/error_code_values.g.dart b/pkg/analyzer/lib/src/error/error_code_values.g.dart index a157e22bf9e3..3a0bdfc7fa71 100644 --- a/pkg/analyzer/lib/src/error/error_code_values.g.dart +++ b/pkg/analyzer/lib/src/error/error_code_values.g.dart @@ -74,6 +74,7 @@ const List errorCodeValues = [ CompileTimeErrorCode.BODY_MIGHT_COMPLETE_NORMALLY, CompileTimeErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME, + CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME, diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index 526e7c40ce84..d5b09ccb12bc 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -706,6 +706,9 @@ class ErrorVerifier extends RecursiveAstVisitor _enclosingClass = declarationElement; + _checkForBuiltInIdentifierAsName(node.name, + CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME); + _duplicateDefinitionVerifier.checkExtensionType(node, declarationElement); _checkForRepeatedType(node.implementsClause?.interfaces, CompileTimeErrorCode.IMPLEMENTS_REPEATED); diff --git a/pkg/analyzer/messages.yaml b/pkg/analyzer/messages.yaml index 4356d9f09d99..d9d7dbf7d8b1 100644 --- a/pkg/analyzer/messages.yaml +++ b/pkg/analyzer/messages.yaml @@ -1424,6 +1424,14 @@ CompileTimeErrorCode: #### Common fixes Choose a different name for the declaration. + BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME: + sharedName: BUILT_IN_IDENTIFIER_IN_DECLARATION + problemMessage: "The built-in identifier '{0}' can't be used as an extension type name." + correctionMessage: Try choosing a different name for the extension type. + hasPublishedDocs: true + comment: |- + Parameters: + 0: the built-in identifier that is being used BUILT_IN_IDENTIFIER_AS_PREFIX_NAME: sharedName: BUILT_IN_IDENTIFIER_IN_DECLARATION problemMessage: "The built-in identifier '{0}' can't be used as a prefix name." diff --git a/pkg/analyzer/test/src/diagnostics/built_in_identifier_as_extension_type_name_test.dart b/pkg/analyzer/test/src/diagnostics/built_in_identifier_as_extension_type_name_test.dart new file mode 100644 index 000000000000..8c67539fbf90 --- /dev/null +++ b/pkg/analyzer/test/src/diagnostics/built_in_identifier_as_extension_type_name_test.dart @@ -0,0 +1,42 @@ +// 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(BuiltInIdentifierAsExtensionTypeNameTest); + }); +} + +@reflectiveTest +class BuiltInIdentifierAsExtensionTypeNameTest + extends PubPackageResolutionTest { + test_as() async { + await assertErrorsInCode( + r''' +extension type as(int it) {} +''', + [ + error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME, + 15, 2), + ], + ); + } + + test_Function() async { + await assertErrorsInCode( + r''' +extension type Function(int it) {} +''', + [ + error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME, + 15, 8), + ], + ); + } +} diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart index cc1ef24b24f6..33949bf7b5fc 100644 --- a/pkg/analyzer/test/src/diagnostics/test_all.dart +++ b/pkg/analyzer/test/src/diagnostics/test_all.dart @@ -56,6 +56,8 @@ import 'body_might_complete_normally_test.dart' as body_might_complete_normally; import 'break_label_on_switch_member_test.dart' as break_label_on_switch_member; import 'built_in_identifier_as_extension_name_test.dart' as built_in_as_extension_name; +import 'built_in_identifier_as_extension_type_name_test.dart' + as built_in_identifier_as_extension_type_name; import 'built_in_identifier_as_prefix_name_test.dart' as built_in_as_prefix_name; import 'built_in_identifier_as_type_name_test.dart' as built_in_as_type_name; @@ -959,6 +961,7 @@ main() { body_might_complete_normally.main(); break_label_on_switch_member.main(); built_in_as_extension_name.main(); + built_in_identifier_as_extension_type_name.main(); built_in_as_prefix_name.main(); built_in_as_type_name.main(); built_in_as_type_parameter_name.main(); diff --git a/pkg/analyzer/tool/diagnostics/diagnostics.md b/pkg/analyzer/tool/diagnostics/diagnostics.md index 5d3ab98b8f0c..32c5cf96c93e 100644 --- a/pkg/analyzer/tool/diagnostics/diagnostics.md +++ b/pkg/analyzer/tool/diagnostics/diagnostics.md @@ -1728,6 +1728,8 @@ _The built-in identifier '{0}' can't be used as a typedef name._ _The built-in identifier '{0}' can't be used as an extension name._ +_The built-in identifier '{0}' can't be used as an extension type name._ + #### Description The analyzer produces this diagnostic when the name used in the declaration