Skip to content

Commit

Permalink
[cfe] Allow typed super parameters
Browse files Browse the repository at this point in the history
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 <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
  • Loading branch information
chloestefantsova authored and Commit Bot committed Feb 23, 2022
1 parent 2244be0 commit c12238d
Show file tree
Hide file tree
Showing 51 changed files with 1,164 additions and 185 deletions.
2 changes: 1 addition & 1 deletion pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6895,7 +6895,7 @@ class Parser {
typeInfo.couldBeExpression) {
assert(optional('?', token));
assert(next.isKeywordOrIdentifier);
if (!next.isIdentifier) {
if (!looksLikeName(next)) {
reportRecoverableError(
next, codes.templateExpectedIdentifier.withArguments(next));
next = rewriter.insertSyntheticIdentifier(next);
Expand Down
11 changes: 0 additions & 11 deletions pkg/_fe_analyzer_shared/lib/src/parser/type_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,6 @@ TypeInfo computeType(final Token token, bool required,
assert(typeParamOrArg == noTypeParamOrArg);
next = next.next!;

// TODO(scheglov) This is a hack to partially fix.
// https://github.com/dart-lang/sdk/issues/47951
if (optional('?', next) &&
optional('super', next.next!) &&
optional('.', next.next!.next!)) {
return simpleNullableType;
}
if (optional('super', next) && optional('.', next.next!)) {
return simpleType;
}

if (optional('.', next)) {
next = next.next!;
if (isValidTypeReference(next)) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/_fe_analyzer_shared/lib/src/parser/type_info_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class VoidType implements TypeInfo {
bool looksLikeName(Token token) {
return token.kind == IDENTIFIER_TOKEN ||
optional('this', token) ||
optional('super', token) ||
(token.isIdentifier &&
// Although `typedef` is a legal identifier,
// type `typedef` identifier is not legal and in this situation
Expand Down Expand Up @@ -794,7 +795,9 @@ class ComplexTypeInfo implements TypeInfo {
if (optional('?', next)) {
next = next.next!;
}
if (!(next.isIdentifier || optional('this', next))) {
if (!(next.isIdentifier ||
optional('this', next) ||
optional('super', next))) {
break; // `Function` used as the name in a function declaration.
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/test/generated/nnbd_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ main() {
parseCompilationUnit(r'+t{{r?this}}', errors: [
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 0, 1),
expectedError(ParserErrorCode.MISSING_FUNCTION_PARAMETERS, 1, 1),
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 6, 4),
expectedError(ParserErrorCode.EXPECTED_TOKEN, 6, 4),
expectedError(ParserErrorCode.EXPECTED_TOKEN, 10, 1),
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 10, 1),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1471,28 +1471,27 @@ parseUnit(class)
listener: beginMetadataStar(int)
listener: endMetadataStar(0)
listener: beginMember()
isReservedKeyword(super)
indicatesMethodOrField(=)
parseFields(;, null, null, null, null, null, null, ;, Instance of 'SimpleType', super, DeclarationKind.Class, WrapperClass, true)
listener: beginFields(DeclarationKind.Class, null, null, null, null, null, null, ;)
listener: handleIdentifier(int, typeReference)
listener: handleNoTypeArguments(super)
listener: handleType(int, null)
ensureIdentifierPotentiallyRecovered(int, fieldDeclaration, true)
reportRecoverableErrorWithToken(super, Instance of 'Template<(Token) => Message>')
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)
listener: handleIdentifier(super, fieldDeclaration)
parseFieldInitializerOpt(super, super, null, null, null, null, DeclarationKind.Class, WrapperClass)
listener: beginFieldInitializer(=)
parseExpression(=)
parsePrecedenceExpression(=, 1, true)
parseUnaryExpression(=, true)
parsePrimary(=, expression)
parseLiteralInt(=)
listener: handleLiteralInt(42)
listener: endFieldInitializer(=, ;)
listener: endClassFields(null, null, null, null, null, null, 1, int, ;)
listener: endMember()
recoverFromInvalidMember(int, ;, null, null, null, null, null, null, ;, Instance of 'SimpleType', null, DeclarationKind.Class, WrapperClass)
parseFields(;, null, null, null, null, null, null, ;, Instance of 'SimpleType', super, DeclarationKind.Class, WrapperClass, false)
listener: beginFields(DeclarationKind.Class, null, null, null, null, null, null, ;)
listener: handleIdentifier(int, typeReference)
listener: handleNoTypeArguments(super)
listener: handleType(int, null)
ensureIdentifierPotentiallyRecovered(int, fieldDeclaration, false)
reportRecoverableErrorWithToken(super, Instance of 'Template<(Token) => Message>')
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)
listener: handleIdentifier(super, fieldDeclaration)
parseFieldInitializerOpt(super, super, null, null, null, null, DeclarationKind.Class, WrapperClass)
listener: beginFieldInitializer(=)
parseExpression(=)
parsePrecedenceExpression(=, 1, true)
parseUnaryExpression(=, true)
parsePrimary(=, expression)
parseLiteralInt(=)
listener: handleLiteralInt(42)
listener: endFieldInitializer(=, ;)
listener: endClassFields(null, null, null, null, null, null, 1, int, ;)
listener: endMember()
notEofOrValue(}, int)
parseClassOrMixinOrExtensionOrEnumMemberImpl(;, DeclarationKind.Class, WrapperClass)
parseMetadataStar(;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ parser/error_recovery/keyword_named_class_methods:282:7: 'super' can't be used a
int super(int x) {
^^^^^

parser/error_recovery/keyword_named_class_methods:282:7: Expected ';' after this.
int super(int x) {
^^^^^

parser/error_recovery/keyword_named_class_methods:282:12: Expected an identifier, but got '('.
int super(int x) {
^

parser/error_recovery/keyword_named_class_methods:287:7: 'switch' can't be used as an identifier because it's a keyword.
int switch(int x) {
^^^^^^
Expand Down Expand Up @@ -3883,12 +3891,23 @@ beginCompilationUnit(class)
beginMetadataStar(int)
endMetadataStar(0)
beginMember()
beginMethod(DeclarationKind.Class, null, null, null, null, null, super)
beginFields(DeclarationKind.Class, null, null, null, null, null, null, })
handleIdentifier(int, typeReference)
handleNoTypeArguments(super)
handleType(int, null)
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)
handleIdentifier(super, methodDeclaration)
handleIdentifier(super, fieldDeclaration)
handleNoFieldInitializer(()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], super, super)
endClassFields(null, null, null, null, null, null, 1, int, ;)
endMember()
beginMetadataStar(()
endMetadataStar(0)
beginMember()
beginMethod(DeclarationKind.Class, null, null, null, null, null, ()
handleNoType(;)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., Try inserting an identifier before '('., {lexeme: (}], (, ()
handleIdentifier(, methodDeclaration)
handleNoTypeVariables(()
beginFormalParameters((, MemberKind.NonStaticMethod)
beginMetadataStar(int)
Expand Down Expand Up @@ -3937,7 +3956,7 @@ beginCompilationUnit(class)
endBinaryExpression(+)
endReturnStatement(true, return, ;)
endBlockFunctionBody(2, {, })
endClassMethod(null, int, (, null, })
endClassMethod(null, , (, null, })
endMember()
beginMetadataStar(int)
endMetadataStar(0)
Expand Down Expand Up @@ -4743,7 +4762,7 @@ beginCompilationUnit(class)
endBlockFunctionBody(2, {, })
endClassMethod(null, int, (, null, })
endMember()
endClassOrMixinOrExtensionBody(DeclarationKind.Class, 70, {, })
endClassOrMixinOrExtensionBody(DeclarationKind.Class, 71, {, })
endClassDeclaration(class, })
endTopLevelDeclaration()
endCompilationUnit(1, )

0 comments on commit c12238d

Please sign in to comment.