Skip to content

Correct The Parsing of Primary Associated Type Clauses in Protocols #41848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ class Parser {

ParserStatus parsePrimaryAssociatedTypes(
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
ParserStatus parsePrimaryAssociatedTypeList(
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
DeclAttributes &Attributes);

Expand Down
34 changes: 22 additions & 12 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7905,8 +7905,30 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,

ParserStatus Parser::parsePrimaryAssociatedTypes(
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
SyntaxParsingContext GPSContext(SyntaxContext,
SyntaxKind::PrimaryAssociatedTypeClause);

SourceLoc LAngleLoc = consumeStartingLess();

auto Result = parsePrimaryAssociatedTypeList(AssocTypes);

// Parse the closing '>'.
SourceLoc RAngleLoc;
if (startsWithGreater(Tok)) {
RAngleLoc = consumeStartingGreater();
} else {
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
diagnose(LAngleLoc, diag::opening_angle);

// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
}

return Result;
}

ParserStatus Parser::parsePrimaryAssociatedTypeList(
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
ParserStatus Result;
SyntaxParsingContext PATContext(SyntaxContext,
SyntaxKind::PrimaryAssociatedTypeList);
Expand Down Expand Up @@ -7988,18 +8010,6 @@ ParserStatus Parser::parsePrimaryAssociatedTypes(
HasNextParam = consumeIf(tok::comma);
} while (HasNextParam);

// Parse the closing '>'.
SourceLoc RAngleLoc;
if (startsWithGreater(Tok)) {
RAngleLoc = consumeStartingGreater();
} else {
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
diagnose(LAngleLoc, diag::opening_angle);

// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
}

return Result;
}

Expand Down
41 changes: 41 additions & 0 deletions unittests/Syntax/DeclSyntaxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,44 @@ TEST(DeclSyntaxTests, FunctionDeclWithAPIs) {
TEST(DeclSyntaxTests, FunctionDeclBuilderAPIs) {

}

#pragma mark - parameterized protocol-decl

TEST(DeclSyntaxTests, ProtocolMakeAPIs) {
RC<SyntaxArena> Arena = SyntaxArena::make();
SyntaxFactory Factory(Arena);
{
SmallString<1> Scratch;
llvm::raw_svector_ostream OS(Scratch);
Factory.makeBlankProtocolDecl().print(OS);
ASSERT_EQ(OS.str().str(), "");
}
{
SmallString<64> Scratch;
llvm::raw_svector_ostream OS(Scratch);
auto Protocol = Factory.makeProtocolKeyword("", " ");
auto MyCollection = Factory.makeIdentifier("MyCollection", "", "");
auto ElementName = Factory.makeIdentifier("Element", "", "");
auto ElementParam =
Factory.makePrimaryAssociatedType(None, ElementName, None, None, None, None);
auto LeftAngle = Factory.makeLeftAngleToken("", "");
auto RightAngle = Factory.makeRightAngleToken("", " ");
auto PrimaryAssocs = PrimaryAssociatedTypeClauseSyntaxBuilder(Arena)
.useLeftAngleBracket(LeftAngle)
.useRightAngleBracket(RightAngle)
.addPrimaryAssociatedType(ElementParam)
.build();

auto LeftBrace = Factory.makeLeftBraceToken("", "");
auto RightBrace = Factory.makeRightBraceToken("", "");
auto Members = MemberDeclBlockSyntaxBuilder(Arena)
.useLeftBrace(LeftBrace)
.useRightBrace(RightBrace)
.build();
Factory
.makeProtocolDecl(None, None, Protocol, MyCollection, PrimaryAssocs, None, None, Members)
.print(OS);
ASSERT_EQ(OS.str().str(),
"protocol MyCollection<Element> {}");
}
}
3 changes: 3 additions & 0 deletions utils/gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@
collection_element_name='Modifier', is_optional=True),
Child('ProtocolKeyword', kind='ProtocolToken'),
Child('Identifier', kind='IdentifierToken'),
Child('PrimaryAssociatedTypeClause',
kind='PrimaryAssociatedTypeClause',
is_optional=True),
Child('InheritanceClause', kind='TypeInheritanceClause',
is_optional=True),
Child('GenericWhereClause', kind='GenericWhereClause',
Expand Down
9 changes: 9 additions & 0 deletions utils/gyb_syntax_support/GenericNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,13 @@
Child('Colon', kind='ColonToken'),
Child('RightTypeIdentifier', kind='Type'),
]),

# primary-associated-type-clause -> '<' primary-associated-type-list '>'
Node('PrimaryAssociatedTypeClause', kind='Syntax',
children=[
Child('LeftAngleBracket', kind='LeftAngleToken'),
Child('PrimaryAssociatedTypeList', kind='PrimaryAssociatedTypeList',
collection_element_name='PrimaryAssociatedType'),
Child('RightAngleBracket', kind='RightAngleToken'),
]),
]
1 change: 1 addition & 0 deletions utils/gyb_syntax_support/NodeSerializationCodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
'RegexLiteralExpr': 253,
'PrimaryAssociatedTypeList' : 254,
'PrimaryAssociatedType' : 255,
'PrimaryAssociatedTypeClause' : 256,
}


Expand Down