Skip to content

Commit f90b782

Browse files
authored
Merge pull request #23360 from akyrtzi/fix-syntax-parser-assertion
[syntax parser] Fix assertion hit with invalid type parsing
2 parents 4082b44 + 375bec4 commit f90b782

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/Parse/ParseType.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -705,16 +705,19 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID,
705705
SyntaxContext->setCreateSyntax(SyntaxKind::CompositionType);
706706
assert(Tok.isContextualPunctuator("&"));
707707
do {
708-
consumeToken(); // consume '&'
709-
710-
if (SyntaxContext->isEnabled() && Status.isSuccess()) {
711-
ParsedCompositionTypeElementSyntaxBuilder Builder(*SyntaxContext);
712-
auto Ampersand = SyntaxContext->popToken();
713-
auto Type = SyntaxContext->popIf<ParsedTypeSyntax>().getValue();
714-
Builder
715-
.useAmpersand(Ampersand)
716-
.useType(Type);
717-
SyntaxContext->addSyntax(Builder.build());
708+
if (SyntaxContext->isEnabled()) {
709+
auto Type = SyntaxContext->popIf<ParsedTypeSyntax>();
710+
consumeToken(); // consume '&'
711+
if (Type) {
712+
ParsedCompositionTypeElementSyntaxBuilder Builder(*SyntaxContext);
713+
auto Ampersand = SyntaxContext->popToken();
714+
Builder
715+
.useAmpersand(Ampersand)
716+
.useType(Type.getValue());
717+
SyntaxContext->addSyntax(Builder.build());
718+
}
719+
} else {
720+
consumeToken(); // consume '&'
718721
}
719722

720723
// Parse next type.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// REQUIRES: syntax_parser_lib
2+
// RUN: %swift-syntax-parser-test %s -dump-tree | %FileCheck %s
3+
4+
let x: a[i] & b
5+
// CHECK: |let|

0 commit comments

Comments
 (0)