Skip to content

Commit

Permalink
Extension type. Issue 53930. Report BUILT_IN_IDENTIFIER_AS_EXTENSION_…
Browse files Browse the repository at this point in the history
…TYPE_NAME.

Bug: #53930
Change-Id: Ibf88b678cc45147f308de68614d6d127323b822a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/333600
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 3, 2023
1 parent 048c7b0 commit 6137e93
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down
11 changes: 11 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const List<ErrorCode> 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,
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,9 @@ class ErrorVerifier extends RecursiveAstVisitor<void>

_enclosingClass = declarationElement;

_checkForBuiltInIdentifierAsName(node.name,
CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_TYPE_NAME);

_duplicateDefinitionVerifier.checkExtensionType(node, declarationElement);
_checkForRepeatedType(node.implementsClause?.interfaces,
CompileTimeErrorCode.IMPLEMENTS_REPEATED);
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
],
);
}
}
3 changes: 3 additions & 0 deletions pkg/analyzer/test/src/diagnostics/test_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/tool/diagnostics/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6137e93

Please sign in to comment.