Skip to content

Commit c12238d

Browse files
chloestefantsovaCommit Bot
authored and
Commit Bot
committed
[cfe] Allow typed super parameters
Part of #47525 Closes #47741 Closes #48444 Closes #47951 Change-Id: If9aadd996414df5091fc667a7202aafa9a0213db Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/233889 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 2244be0 commit c12238d

File tree

51 files changed

+1164
-185
lines changed

Some content is hidden

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

51 files changed

+1164
-185
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6895,7 +6895,7 @@ class Parser {
68956895
typeInfo.couldBeExpression) {
68966896
assert(optional('?', token));
68976897
assert(next.isKeywordOrIdentifier);
6898-
if (!next.isIdentifier) {
6898+
if (!looksLikeName(next)) {
68996899
reportRecoverableError(
69006900
next, codes.templateExpectedIdentifier.withArguments(next));
69016901
next = rewriter.insertSyntheticIdentifier(next);

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,6 @@ TypeInfo computeType(final Token token, bool required,
252252
assert(typeParamOrArg == noTypeParamOrArg);
253253
next = next.next!;
254254

255-
// TODO(scheglov) This is a hack to partially fix.
256-
// https://github.com/dart-lang/sdk/issues/47951
257-
if (optional('?', next) &&
258-
optional('super', next.next!) &&
259-
optional('.', next.next!.next!)) {
260-
return simpleNullableType;
261-
}
262-
if (optional('super', next) && optional('.', next.next!)) {
263-
return simpleType;
264-
}
265-
266255
if (optional('.', next)) {
267256
next = next.next!;
268257
if (isValidTypeReference(next)) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ class VoidType implements TypeInfo {
451451
bool looksLikeName(Token token) {
452452
return token.kind == IDENTIFIER_TOKEN ||
453453
optional('this', token) ||
454+
optional('super', token) ||
454455
(token.isIdentifier &&
455456
// Although `typedef` is a legal identifier,
456457
// type `typedef` identifier is not legal and in this situation
@@ -794,7 +795,9 @@ class ComplexTypeInfo implements TypeInfo {
794795
if (optional('?', next)) {
795796
next = next.next!;
796797
}
797-
if (!(next.isIdentifier || optional('this', next))) {
798+
if (!(next.isIdentifier ||
799+
optional('this', next) ||
800+
optional('super', next))) {
798801
break; // `Function` used as the name in a function declaration.
799802
}
800803
}

pkg/analyzer/test/generated/nnbd_parser_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ main() {
235235
parseCompilationUnit(r'+t{{r?this}}', errors: [
236236
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 0, 1),
237237
expectedError(ParserErrorCode.MISSING_FUNCTION_PARAMETERS, 1, 1),
238-
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 6, 4),
239238
expectedError(ParserErrorCode.EXPECTED_TOKEN, 6, 4),
240239
expectedError(ParserErrorCode.EXPECTED_TOKEN, 10, 1),
240+
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 10, 1),
241241
]);
242242
}
243243

pkg/front_end/parser_testcases/error_recovery/keyword_named_class_fields.dart.intertwined.expect

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,28 +1471,27 @@ parseUnit(class)
14711471
listener: beginMetadataStar(int)
14721472
listener: endMetadataStar(0)
14731473
listener: beginMember()
1474-
isReservedKeyword(super)
1475-
indicatesMethodOrField(=)
1476-
parseFields(;, null, null, null, null, null, null, ;, Instance of 'SimpleType', super, DeclarationKind.Class, WrapperClass, true)
1477-
listener: beginFields(DeclarationKind.Class, null, null, null, null, null, null, ;)
1478-
listener: handleIdentifier(int, typeReference)
1479-
listener: handleNoTypeArguments(super)
1480-
listener: handleType(int, null)
1481-
ensureIdentifierPotentiallyRecovered(int, fieldDeclaration, true)
1482-
reportRecoverableErrorWithToken(super, Instance of 'Template<(Token) => Message>')
1483-
listener: handleRecoverableError(Message[ExpectedIdentifierButGotKeyword, 'super' can't be used as an identifier because it's a keyword., Try renaming this to be an identifier that isn't a keyword., {lexeme: super}], super, super)
1484-
listener: handleIdentifier(super, fieldDeclaration)
1485-
parseFieldInitializerOpt(super, super, null, null, null, null, DeclarationKind.Class, WrapperClass)
1486-
listener: beginFieldInitializer(=)
1487-
parseExpression(=)
1488-
parsePrecedenceExpression(=, 1, true)
1489-
parseUnaryExpression(=, true)
1490-
parsePrimary(=, expression)
1491-
parseLiteralInt(=)
1492-
listener: handleLiteralInt(42)
1493-
listener: endFieldInitializer(=, ;)
1494-
listener: endClassFields(null, null, null, null, null, null, 1, int, ;)
1495-
listener: endMember()
1474+
recoverFromInvalidMember(int, ;, null, null, null, null, null, null, ;, Instance of 'SimpleType', null, DeclarationKind.Class, WrapperClass)
1475+
parseFields(;, null, null, null, null, null, null, ;, Instance of 'SimpleType', super, DeclarationKind.Class, WrapperClass, false)
1476+
listener: beginFields(DeclarationKind.Class, null, null, null, null, null, null, ;)
1477+
listener: handleIdentifier(int, typeReference)
1478+
listener: handleNoTypeArguments(super)
1479+
listener: handleType(int, null)
1480+
ensureIdentifierPotentiallyRecovered(int, fieldDeclaration, false)
1481+
reportRecoverableErrorWithToken(super, Instance of 'Template<(Token) => Message>')
1482+
listener: handleRecoverableError(Message[ExpectedIdentifierButGotKeyword, 'super' can't be used as an identifier because it's a keyword., Try renaming this to be an identifier that isn't a keyword., {lexeme: super}], super, super)
1483+
listener: handleIdentifier(super, fieldDeclaration)
1484+
parseFieldInitializerOpt(super, super, null, null, null, null, DeclarationKind.Class, WrapperClass)
1485+
listener: beginFieldInitializer(=)
1486+
parseExpression(=)
1487+
parsePrecedenceExpression(=, 1, true)
1488+
parseUnaryExpression(=, true)
1489+
parsePrimary(=, expression)
1490+
parseLiteralInt(=)
1491+
listener: handleLiteralInt(42)
1492+
listener: endFieldInitializer(=, ;)
1493+
listener: endClassFields(null, null, null, null, null, null, 1, int, ;)
1494+
listener: endMember()
14961495
notEofOrValue(}, int)
14971496
parseClassOrMixinOrExtensionOrEnumMemberImpl(;, DeclarationKind.Class, WrapperClass)
14981497
parseMetadataStar(;)

pkg/front_end/parser_testcases/error_recovery/keyword_named_class_methods.dart.expect

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ parser/error_recovery/keyword_named_class_methods:282:7: 'super' can't be used a
308308
int super(int x) {
309309
^^^^^
310310

311+
parser/error_recovery/keyword_named_class_methods:282:7: Expected ';' after this.
312+
int super(int x) {
313+
^^^^^
314+
315+
parser/error_recovery/keyword_named_class_methods:282:12: Expected an identifier, but got '('.
316+
int super(int x) {
317+
^
318+
311319
parser/error_recovery/keyword_named_class_methods:287:7: 'switch' can't be used as an identifier because it's a keyword.
312320
int switch(int x) {
313321
^^^^^^
@@ -3883,12 +3891,23 @@ beginCompilationUnit(class)
38833891
beginMetadataStar(int)
38843892
endMetadataStar(0)
38853893
beginMember()
3886-
beginMethod(DeclarationKind.Class, null, null, null, null, null, super)
3894+
beginFields(DeclarationKind.Class, null, null, null, null, null, null, })
38873895
handleIdentifier(int, typeReference)
38883896
handleNoTypeArguments(super)
38893897
handleType(int, null)
38903898
handleRecoverableError(Message[ExpectedIdentifierButGotKeyword, 'super' can't be used as an identifier because it's a keyword., Try renaming this to be an identifier that isn't a keyword., {lexeme: super}], super, super)
3891-
handleIdentifier(super, methodDeclaration)
3899+
handleIdentifier(super, fieldDeclaration)
3900+
handleNoFieldInitializer(()
3901+
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], super, super)
3902+
endClassFields(null, null, null, null, null, null, 1, int, ;)
3903+
endMember()
3904+
beginMetadataStar(()
3905+
endMetadataStar(0)
3906+
beginMember()
3907+
beginMethod(DeclarationKind.Class, null, null, null, null, null, ()
3908+
handleNoType(;)
3909+
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., Try inserting an identifier before '('., {lexeme: (}], (, ()
3910+
handleIdentifier(, methodDeclaration)
38923911
handleNoTypeVariables(()
38933912
beginFormalParameters((, MemberKind.NonStaticMethod)
38943913
beginMetadataStar(int)
@@ -3937,7 +3956,7 @@ beginCompilationUnit(class)
39373956
endBinaryExpression(+)
39383957
endReturnStatement(true, return, ;)
39393958
endBlockFunctionBody(2, {, })
3940-
endClassMethod(null, int, (, null, })
3959+
endClassMethod(null, , (, null, })
39413960
endMember()
39423961
beginMetadataStar(int)
39433962
endMetadataStar(0)
@@ -4743,7 +4762,7 @@ beginCompilationUnit(class)
47434762
endBlockFunctionBody(2, {, })
47444763
endClassMethod(null, int, (, null, })
47454764
endMember()
4746-
endClassOrMixinOrExtensionBody(DeclarationKind.Class, 70, {, })
4765+
endClassOrMixinOrExtensionBody(DeclarationKind.Class, 71, {, })
47474766
endClassDeclaration(class, })
47484767
endTopLevelDeclaration()
47494768
endCompilationUnit(1, )

0 commit comments

Comments
 (0)