Skip to content

Commit 25dcfca

Browse files
jensjohaCommit Bot
authored and
Commit Bot
committed
[parser][CFE][analyzer] Function is builtIn keyword
This CL makes `Function` a builtIn keyword instead of a `pseudo` keyword. See also (and fixes): #45703 #45704 #45705 #49197 This undoes https://dart-review.googlesource.com/c/sdk/+/195761 This is ~a merge of https://dart-review.googlesource.com/c/sdk/+/195906 and https://dart-review.googlesource.com/c/sdk/+/200080 Change-Id: I8bfee6976d43819fa355de99b3b2429eb67a7cdd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249484 Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent e43aad1 commit 25dcfca

File tree

49 files changed

+840
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+840
-179
lines changed

pkg/_fe_analyzer_shared/lib/src/messages/codes_generated.dart

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5010,15 +5010,6 @@ const MessageCode messageForInLoopWithConstVariable = const MessageCode(
50105010
problemMessage: r"""A for-in loop-variable can't be 'const'.""",
50115011
correctionMessage: r"""Try removing the 'const' modifier.""");
50125012

5013-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5014-
const Code<Null> codeFunctionAsTypeParameter = messageFunctionAsTypeParameter;
5015-
5016-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5017-
const MessageCode messageFunctionAsTypeParameter = const MessageCode(
5018-
"FunctionAsTypeParameter",
5019-
problemMessage:
5020-
r"""'Function' is a built-in identifier, could not used as a type identifier.""");
5021-
50225013
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
50235014
const Code<Null> codeFunctionTypeDefaultValue = messageFunctionTypeDefaultValue;
50245015

@@ -5040,30 +5031,6 @@ const MessageCode messageFunctionTypedParameterVar = const MessageCode(
50405031
r"""Function-typed parameters can't specify 'const', 'final' or 'var' in place of a return type.""",
50415032
correctionMessage: r"""Try replacing the keyword with a return type.""");
50425033

5043-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5044-
const Template<
5045-
Message Function(String name)> templateFunctionUsedAsDec = const Template<
5046-
Message Function(String name)>(
5047-
problemMessageTemplate:
5048-
r"""'Function' is a built-in identifier, could not used as a #name name.""",
5049-
withArguments: _withArgumentsFunctionUsedAsDec);
5050-
5051-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5052-
const Code<Message Function(String name)> codeFunctionUsedAsDec =
5053-
const Code<Message Function(String name)>(
5054-
"FunctionUsedAsDec",
5055-
);
5056-
5057-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
5058-
Message _withArgumentsFunctionUsedAsDec(String name) {
5059-
if (name.isEmpty) throw 'No name provided';
5060-
name = demangleMixinApplicationName(name);
5061-
return new Message(codeFunctionUsedAsDec,
5062-
problemMessage:
5063-
"""'Function' is a built-in identifier, could not used as a ${name} name.""",
5064-
arguments: {'name': name});
5065-
}
5066-
50675034
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
50685035
const Code<Null> codeGeneratorReturnsValue = messageGeneratorReturnsValue;
50695036

pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../messages/codes.dart'
99
Code,
1010
LocatedMessage,
1111
Message,
12+
codeBuiltInIdentifierInDeclaration,
1213
codeCatchSyntaxExtraParameters,
1314
codeNativeClauseShouldBeAnnotation,
1415
templateInternalProblemStackNotEmpty,
@@ -216,6 +217,8 @@ abstract class StackListener extends Listener {
216217
@override
217218
Uri get uri;
218219

220+
Uri get importUri;
221+
219222
void discard(int n) {
220223
for (int i = 0; i < n; i++) {
221224
pop();
@@ -498,6 +501,10 @@ abstract class StackListener extends Listener {
498501
} else if (code == codeCatchSyntaxExtraParameters) {
499502
// Ignored. This error is handled by the BodyBuilder.
500503
return true;
504+
} else if (code == codeBuiltInIdentifierInDeclaration) {
505+
if (importUri.isScheme("dart")) return true;
506+
if (uri.isScheme("org-dartlang-sdk")) return true;
507+
return false;
501508
} else {
502509
return false;
503510
}

pkg/_fe_analyzer_shared/lib/src/parser/type_info.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ bool isValidTypeReference(Token token) {
138138
return type.isPseudo ||
139139
(type.isBuiltIn && optional('.', token.next!)) ||
140140
(identical(value, 'dynamic')) ||
141+
(identical(value, 'Function')) ||
141142
(identical(value, 'void'));
142143
}
143144
return false;

pkg/_fe_analyzer_shared/lib/src/scanner/token.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Keyword extends TokenType {
197197
static const Keyword FOR = const Keyword("for", "FOR", KeywordStyle.reserved);
198198

199199
static const Keyword FUNCTION =
200-
const Keyword("Function", "FUNCTION", KeywordStyle.pseudo);
200+
const Keyword("Function", "FUNCTION", KeywordStyle.builtIn);
201201

202202
static const Keyword GET = const Keyword("get", "GET", KeywordStyle.builtIn);
203203

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,6 @@ HintCode.DEPRECATED_EXTENDS_FUNCTION:
12281228
status: needsFix
12291229
notes: |-
12301230
The fix is to remove `Function` from where it's referenced.
1231-
HintCode.DEPRECATED_FUNCTION_CLASS_DECLARATION:
1232-
status: noFix
1233-
notes: |-
1234-
The fix is to rename the class, which can't be done as a fix.
12351231
HintCode.DEPRECATED_IMPLEMENTS_FUNCTION:
12361232
status: needsFix
12371233
notes: |-

pkg/analyzer/lib/error/error.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ const List<ErrorCode> errorCodeValues = [
555555
HintCode.DEAD_CODE_ON_CATCH_SUBTYPE,
556556
HintCode.DEPRECATED_EXPORT_USE,
557557
HintCode.DEPRECATED_EXTENDS_FUNCTION,
558-
HintCode.DEPRECATED_FUNCTION_CLASS_DECLARATION,
559558
HintCode.DEPRECATED_IMPLEMENTS_FUNCTION,
560559
HintCode.DEPRECATED_MEMBER_USE,
561560
HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE,

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ abstract class AbstractClassElementImpl extends _ExistingElementImpl
140140
return name == '_Enum' && library.isDartCore;
141141
}
142142

143+
/// Return `true` if this class represents the class 'Function' defined in the
144+
/// dart:core library.
145+
bool get isDartCoreFunctionImpl {
146+
return name == 'Function' && library.isDartCore;
147+
}
148+
143149
@override
144150
bool get isDartCoreObject => false;
145151

pkg/analyzer/lib/src/dart/error/hint_codes.g.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ class HintCode extends AnalyzerErrorCode {
111111
uniqueName: 'DEPRECATED_EXTENDS_FUNCTION',
112112
);
113113

114-
/// Users should not create a class named `Function` anymore.
115-
static const HintCode DEPRECATED_FUNCTION_CLASS_DECLARATION = HintCode(
116-
'DEPRECATED_FUNCTION_CLASS_DECLARATION',
117-
"Declaring a class named 'Function' is deprecated.",
118-
correctionMessage: "Try renaming the class.",
119-
);
120-
121114
/// No parameters.
122115
static const HintCode DEPRECATED_IMPLEMENTS_FUNCTION = HintCode(
123116
'DEPRECATED_SUBTYPE_OF_FUNCTION',

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:_fe_analyzer_shared/src/messages/codes.dart'
77
LocatedMessage,
88
Message,
99
MessageCode,
10+
codeBuiltInIdentifierInDeclaration,
1011
messageAbstractClassMember,
1112
messageAbstractLateField,
1213
messageAbstractStaticField,
@@ -205,6 +206,9 @@ class AstBuilder extends StackListener {
205206
}
206207
}
207208

209+
@override
210+
Uri get importUri => uri;
211+
208212
@override
209213
void addProblem(Message message, int charOffset, int length,
210214
{bool wasHandled = false, List<LocatedMessage>? context}) {
@@ -3782,6 +3786,10 @@ class AstBuilder extends StackListener {
37823786
/// TODO(danrubel): Ignore this error until we deprecate `native` support.
37833787
if (message == messageNativeClauseShouldBeAnnotation && allowNativeClause) {
37843788
return;
3789+
} else if (message.code == codeBuiltInIdentifierInDeclaration) {
3790+
// Allow e.g. 'class Function' in sdk.
3791+
if (importUri.isScheme("dart")) return;
3792+
if (uri.isScheme("org-dartlang-sdk")) return;
37853793
}
37863794
debugEvent("Error: ${message.problemMessage}");
37873795
if (message.code.analyzerCodes == null && startToken is ErrorToken) {

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
426426

427427
List<ClassMember> members = node.members;
428428
_duplicateDefinitionVerifier.checkClass(node);
429-
_checkForBuiltInIdentifierAsName(
430-
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
429+
if (!element.isDartCoreFunctionImpl) {
430+
_checkForBuiltInIdentifierAsName(
431+
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
432+
}
431433
_checkForConflictingClassTypeVariableErrorCodes();
432434
var superclass = node.extendsClause?.superclass;
433435
var implementsClause = node.implementsClause;
@@ -1660,11 +1662,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
16601662
var implementsClause = node.implementsClause;
16611663
var withClause = node.withClause;
16621664

1663-
if (node.name.name == "Function") {
1664-
errorReporter.reportErrorForNode(
1665-
HintCode.DEPRECATED_FUNCTION_CLASS_DECLARATION, node.name);
1666-
}
1667-
16681665
if (extendsClause != null) {
16691666
var superElement = extendsClause.superclass.name.staticElement;
16701667
if (superElement != null && superElement.name == "Function") {

pkg/analyzer/messages.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17669,10 +17669,6 @@ HintCode:
1766917669
}
1767017670
}
1767117671
```
17672-
DEPRECATED_FUNCTION_CLASS_DECLARATION:
17673-
problemMessage: "Declaring a class named 'Function' is deprecated."
17674-
correctionMessage: Try renaming the class.
17675-
comment: Users should not create a class named `Function` anymore.
1767617672
DEPRECATED_EXPORT_USE:
1767717673
problemMessage: "The ability to import '{0}' indirectly has been deprecated."
1767817674
correctionMessage: "Try importing '{0}' directly."

pkg/analyzer/test/src/diagnostics/built_in_identifier_as_extension_name_test.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ main() {
1515

1616
@reflectiveTest
1717
class BuiltInIdentifierAsExtensionNameTest extends PubPackageResolutionTest {
18-
test_error_builtInIdentifierAsExtensionName() async {
18+
test_as() async {
1919
await assertErrorsInCode(
2020
r'''
2121
extension as on Object {}
@@ -26,4 +26,16 @@ extension as on Object {}
2626
],
2727
);
2828
}
29+
30+
test_Function() async {
31+
await assertErrorsInCode(
32+
r'''
33+
extension Function on Object {}
34+
''',
35+
[
36+
error(
37+
CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME, 10, 8),
38+
],
39+
);
40+
}
2941
}

pkg/analyzer/test/src/diagnostics/built_in_identifier_as_prefix_name_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ main() {
1515

1616
@reflectiveTest
1717
class BuiltInIdentifierAsPrefixNameTest extends PubPackageResolutionTest {
18-
test_builtInIdentifierAsPrefixName() async {
18+
test_abstract() async {
1919
await assertErrorsInCode('''
2020
import 'dart:async' as abstract;
21+
''', [
22+
error(HintCode.UNUSED_IMPORT, 7, 12),
23+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME, 23, 8),
24+
]);
25+
}
26+
27+
test_Function() async {
28+
await assertErrorsInCode('''
29+
import 'dart:async' as Function;
2130
''', [
2231
error(HintCode.UNUSED_IMPORT, 7, 12),
2332
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME, 23, 8),

pkg/analyzer/test/src/diagnostics/built_in_identifier_as_type_name_test.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ main() {
1515

1616
@reflectiveTest
1717
class BuiltInIdentifierAsTypeNameTest extends PubPackageResolutionTest {
18-
test_class() async {
18+
test_class_as() async {
1919
await assertErrorsInCode('''
2020
class as {}
2121
''', [
2222
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME, 6, 2),
2323
]);
2424
}
2525

26+
test_class_Function() async {
27+
await assertErrorsInCode('''
28+
class Function {}
29+
''', [
30+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME, 6, 8),
31+
]);
32+
}
33+
2634
test_enum() async {
2735
await assertErrorsInCode('''
2836
enum as {
@@ -33,14 +41,22 @@ enum as {
3341
]);
3442
}
3543

36-
test_mixin() async {
44+
test_mixin_as() async {
3745
await assertErrorsInCode('''
3846
mixin as {}
3947
''', [
4048
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME, 6, 2),
4149
]);
4250
}
4351

52+
test_mixin_Function() async {
53+
await assertErrorsInCode('''
54+
mixin Function {}
55+
''', [
56+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME, 6, 8),
57+
]);
58+
}
59+
4460
test_mixin_OK_on() async {
4561
await assertNoErrorsInCode(r'''
4662
class A {}

pkg/analyzer/test/src/diagnostics/built_in_identifier_as_type_parameter_name_test.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ main() {
1616
@reflectiveTest
1717
class BuiltInIdentifierAsTypeParameterNameTest
1818
extends PubPackageResolutionTest {
19-
test_class() async {
19+
test_class_as() async {
2020
await assertErrorsInCode('''
2121
class A<as> {}
2222
''', [
@@ -25,7 +25,16 @@ class A<as> {}
2525
]);
2626
}
2727

28-
test_extension() async {
28+
test_class_Function() async {
29+
await assertErrorsInCode('''
30+
class A<Function> {}
31+
''', [
32+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME, 8,
33+
8),
34+
]);
35+
}
36+
37+
test_extension_as() async {
2938
await assertErrorsInCode('''
3039
extension <as> on List {}
3140
''', [
@@ -34,7 +43,7 @@ extension <as> on List {}
3443
]);
3544
}
3645

37-
test_function() async {
46+
test_function_as() async {
3847
await assertErrorsInCode('''
3948
void f<as>() {}
4049
''', [

pkg/analyzer/test/src/diagnostics/built_in_identifier_as_typedef_name_test.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,16 @@ typedef void as();
3535
]);
3636
}
3737

38-
test_typedef_generic() async {
38+
test_typedef_classic_as() async {
39+
await assertErrorsInCode(r'''
40+
typedef void as();
41+
''', [
42+
error(ParserErrorCode.EXPECTED_IDENTIFIER_BUT_GOT_KEYWORD, 13, 2),
43+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME, 13, 2),
44+
]);
45+
}
46+
47+
test_typedef_generic_as() async {
3948
await assertErrorsInCode(r'''
4049
typedef as = void Function();
4150
''', [
@@ -44,12 +53,21 @@ typedef as = void Function();
4453
]);
4554
}
4655

47-
test_typedef_interfaceType() async {
56+
test_typedef_interfaceType_as() async {
4857
await assertErrorsInCode(r'''
4958
typedef as = List<int>;
5059
''', [
5160
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME, 8, 2),
5261
error(ParserErrorCode.EXPECTED_IDENTIFIER_BUT_GOT_KEYWORD, 8, 2)
5362
]);
5463
}
64+
65+
test_typedef_interfaceType_Function() async {
66+
await assertErrorsInCode(r'''
67+
typedef Function = List<int>;
68+
''', [
69+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME, 8, 8),
70+
error(ParserErrorCode.EXPECTED_IDENTIFIER_BUT_GOT_KEYWORD, 8, 8)
71+
]);
72+
}
5573
}

pkg/analyzer/test/src/diagnostics/deprecated_extends_function_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/src/dart/error/hint_codes.dart';
5+
import 'package:analyzer/src/error/codes.dart';
66
import 'package:test_reflective_loader/test_reflective_loader.dart';
77

88
import '../dart/resolution/context_collection_resolution.dart';
@@ -28,7 +28,7 @@ class A extends Function {}
2828
class Function {}
2929
class A extends Function {}
3030
''', [
31-
error(HintCode.DEPRECATED_FUNCTION_CLASS_DECLARATION, 6, 8),
31+
error(CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME, 6, 8),
3232
error(HintCode.DEPRECATED_EXTENDS_FUNCTION, 34, 8),
3333
]);
3434
}

0 commit comments

Comments
 (0)