Skip to content

Commit cc0bc22

Browse files
committed
Correct The Parsing of Primary Associated Type Clauses in Protocols
The prior syntax tree did not take into account that the clause itself should own the angle brackets.
1 parent 377cdc1 commit cc0bc22

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,8 @@ class Parser {
11941194

11951195
ParserStatus parsePrimaryAssociatedTypes(
11961196
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
1197+
ParserStatus parsePrimaryAssociatedTypeList(
1198+
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
11971199
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
11981200
DeclAttributes &Attributes);
11991201

lib/Parse/ParseDecl.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7905,8 +7905,30 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
79057905

79067906
ParserStatus Parser::parsePrimaryAssociatedTypes(
79077907
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
7908+
SyntaxParsingContext GPSContext(SyntaxContext,
7909+
SyntaxKind::PrimaryAssociatedTypeClause);
7910+
79087911
SourceLoc LAngleLoc = consumeStartingLess();
79097912

7913+
auto Result = parsePrimaryAssociatedTypeList(AssocTypes);
7914+
7915+
// Parse the closing '>'.
7916+
SourceLoc RAngleLoc;
7917+
if (startsWithGreater(Tok)) {
7918+
RAngleLoc = consumeStartingGreater();
7919+
} else {
7920+
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
7921+
diagnose(LAngleLoc, diag::opening_angle);
7922+
7923+
// Skip until we hit the '>'.
7924+
RAngleLoc = skipUntilGreaterInTypeList();
7925+
}
7926+
7927+
return Result;
7928+
}
7929+
7930+
ParserStatus Parser::parsePrimaryAssociatedTypeList(
7931+
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
79107932
ParserStatus Result;
79117933
SyntaxParsingContext PATContext(SyntaxContext,
79127934
SyntaxKind::PrimaryAssociatedTypeList);
@@ -7988,18 +8010,6 @@ ParserStatus Parser::parsePrimaryAssociatedTypes(
79888010
HasNextParam = consumeIf(tok::comma);
79898011
} while (HasNextParam);
79908012

7991-
// Parse the closing '>'.
7992-
SourceLoc RAngleLoc;
7993-
if (startsWithGreater(Tok)) {
7994-
RAngleLoc = consumeStartingGreater();
7995-
} else {
7996-
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
7997-
diagnose(LAngleLoc, diag::opening_angle);
7998-
7999-
// Skip until we hit the '>'.
8000-
RAngleLoc = skipUntilGreaterInTypeList();
8001-
}
8002-
80038013
return Result;
80048014
}
80058015

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,44 @@ TEST(DeclSyntaxTests, FunctionDeclWithAPIs) {
633633
TEST(DeclSyntaxTests, FunctionDeclBuilderAPIs) {
634634

635635
}
636+
637+
#pragma mark - parameterized protocol-decl
638+
639+
TEST(DeclSyntaxTests, ProtocolMakeAPIs) {
640+
RC<SyntaxArena> Arena = SyntaxArena::make();
641+
SyntaxFactory Factory(Arena);
642+
{
643+
SmallString<1> Scratch;
644+
llvm::raw_svector_ostream OS(Scratch);
645+
Factory.makeBlankProtocolDecl().print(OS);
646+
ASSERT_EQ(OS.str().str(), "");
647+
}
648+
{
649+
SmallString<64> Scratch;
650+
llvm::raw_svector_ostream OS(Scratch);
651+
auto Protocol = Factory.makeProtocolKeyword("", " ");
652+
auto MyCollection = Factory.makeIdentifier("MyCollection", "", "");
653+
auto ElementName = Factory.makeIdentifier("Element", "", "");
654+
auto ElementParam =
655+
Factory.makePrimaryAssociatedType(None, ElementName, None, None, None, None);
656+
auto LeftAngle = Factory.makeLeftAngleToken("", "");
657+
auto RightAngle = Factory.makeRightAngleToken("", " ");
658+
auto PrimaryAssocs = PrimaryAssociatedTypeClauseSyntaxBuilder(Arena)
659+
.useLeftAngleBracket(LeftAngle)
660+
.useRightAngleBracket(RightAngle)
661+
.addPrimaryAssociatedType(ElementParam)
662+
.build();
663+
664+
auto LeftBrace = Factory.makeLeftBraceToken("", "");
665+
auto RightBrace = Factory.makeRightBraceToken("", "");
666+
auto Members = MemberDeclBlockSyntaxBuilder(Arena)
667+
.useLeftBrace(LeftBrace)
668+
.useRightBrace(RightBrace)
669+
.build();
670+
Factory
671+
.makeProtocolDecl(None, None, Protocol, MyCollection, PrimaryAssocs, None, None, Members)
672+
.print(OS);
673+
ASSERT_EQ(OS.str().str(),
674+
"protocol MyCollection<Element> {}");
675+
}
676+
}

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@
257257
collection_element_name='Modifier', is_optional=True),
258258
Child('ProtocolKeyword', kind='ProtocolToken'),
259259
Child('Identifier', kind='IdentifierToken'),
260+
Child('PrimaryAssociatedTypeClause',
261+
kind='PrimaryAssociatedTypeClause',
262+
is_optional=True),
260263
Child('InheritanceClause', kind='TypeInheritanceClause',
261264
is_optional=True),
262265
Child('GenericWhereClause', kind='GenericWhereClause',

utils/gyb_syntax_support/GenericNodes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,13 @@
101101
Child('Colon', kind='ColonToken'),
102102
Child('RightTypeIdentifier', kind='Type'),
103103
]),
104+
105+
# primary-associated-type-clause -> '<' primary-associated-type-list '>'
106+
Node('PrimaryAssociatedTypeClause', kind='Syntax',
107+
children=[
108+
Child('LeftAngleBracket', kind='LeftAngleToken'),
109+
Child('PrimaryAssociatedTypeList', kind='PrimaryAssociatedTypeList',
110+
collection_element_name='PrimaryAssociatedType'),
111+
Child('RightAngleBracket', kind='RightAngleToken'),
112+
]),
104113
]

utils/gyb_syntax_support/NodeSerializationCodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
'RegexLiteralExpr': 253,
258258
'PrimaryAssociatedTypeList' : 254,
259259
'PrimaryAssociatedType' : 255,
260+
'PrimaryAssociatedTypeClause' : 256,
260261
}
261262

262263

0 commit comments

Comments
 (0)