Skip to content

Commit 071a713

Browse files
authored
Merge pull request #27544 from rintaro/syntaxparse-attrincomplete
[SyntaxParse] Fix crashers for generic parameter with attributes
2 parents dbddb0d + d2971fa commit 071a713

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

lib/Parse/ASTGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,9 @@ GenericParamList *ASTGen::generate(const GenericParameterClauseSyntax &clause,
976976
params.reserve(clause.getGenericParameterList().getNumChildren());
977977

978978
for (auto elem : clause.getGenericParameterList()) {
979+
auto nameTok = elem.getName();
980+
if (nameTok.isMissing())
981+
break;
979982

980983
DeclAttributes attrs = generateDeclAttributes(elem, Loc, false);
981984
Identifier name = Context.getIdentifier(elem.getName().getIdentifierText());

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,9 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc At
16321632
}
16331633
consumeToken(tok::code_complete);
16341634
return makeParserCodeCompletionStatus();
1635+
} else {
1636+
// Synthesize an r_brace syntax node if the token is absent
1637+
SyntaxContext->synthesize(tok::identifier, AtLoc.getAdvancedLoc(1));
16351638
}
16361639

16371640
diagnose(Tok, diag::expected_attribute_name);

lib/Parse/ParseGeneric.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Parser::parseGenericParameterClauseSyntax() {
7474

7575
// Parse attributes.
7676
// TODO: Implement syntax attribute parsing.
77+
Optional<ParsedAttributeListSyntax> attrs;
7778
if (Tok.is(tok::at_sign)) {
7879
SyntaxParsingContext TmpCtxt(SyntaxContext);
7980
TmpCtxt.setTransparent();
@@ -83,18 +84,23 @@ Parser::parseGenericParameterClauseSyntax() {
8384
parseDeclAttributeList(attrsAST);
8485
if (!attrsAST.isEmpty())
8586
Generator.addDeclAttributes(attrsAST, AttrsLoc);
86-
auto attrs = SyntaxContext->popIf<ParsedAttributeListSyntax>();
87-
if (attrs)
88-
paramBuilder.useAttributes(std::move(*attrs));
87+
attrs = SyntaxContext->popIf<ParsedAttributeListSyntax>();
8988
}
9089

9190
// Parse the name of the parameter.
9291
auto ident = Context.getIdentifier(Tok.getText());
9392
auto name = parseIdentifierSyntax(diag::expected_generics_parameter_name);
9493
if (!name) {
94+
if (attrs) {
95+
paramBuilder.useAttributes(std::move(*attrs));
96+
builder.addGenericParameterListMember(paramBuilder.build());
97+
}
9598
status.setIsParseError();
9699
break;
97100
}
101+
102+
if (attrs)
103+
paramBuilder.useAttributes(std::move(*attrs));
98104
paramBuilder.useName(std::move(*name));
99105

100106
// Parse the ':' followed by a type.

test/Parse/invalid.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,6 @@ enum sr8202_enum<@indirect T> {} // expected-error {{'indirect' is a declaration
145145
protocol P {
146146
@available(swift, introduced: 4.2) associatedtype Assoc // expected-error {{'@availability' attribute cannot be applied to this declaration}}
147147
}
148+
149+
struct genericParamIncomplete1<@> {} // expected-error {{expected an attribute name}} expected-error {{expected an identifier to name generic parameter}}
150+
struct genericParamIncomplete2<@objc> {} // expected-error {{expected an identifier to name generic parameter}}

0 commit comments

Comments
 (0)