Skip to content

[libSyntax] Fix a crash that happened when parsing an invalid type #18022

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
Jul 19, 2018
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
16 changes: 11 additions & 5 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,11 +1180,17 @@ Parser::parseTypeOptional(TypeRepr *base) {
auto TyR = new (Context) OptionalTypeRepr(base, questionLoc);
llvm::Optional<TypeSyntax> SyntaxNode;
if (SyntaxContext->isEnabled()) {
OptionalTypeSyntaxBuilder Builder(Context.getSyntaxArena());
Builder
.useQuestionMark(SyntaxContext->popToken())
.useWrappedType(SyntaxContext->popIf<TypeSyntax>().getValue());
SyntaxNode.emplace(Builder.build());
auto QuestionMark = SyntaxContext->popToken();
if (auto WrappedType = SyntaxContext->popIf<TypeSyntax>()) {
OptionalTypeSyntaxBuilder Builder(Context.getSyntaxArena());
Builder
.useQuestionMark(QuestionMark)
.useWrappedType(WrappedType.getValue());
SyntaxNode.emplace(Builder.build());
} else {
// Undo the popping of the question mark
SyntaxContext->addSyntax(QuestionMark);
}
}
return makeSyntaxResult(SyntaxNode, TyR);
}
Expand Down
4 changes: 3 additions & 1 deletion test/Syntax/Outputs/round_trip_invalid.swift.withkinds
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<FunctionDecl>// RUN: rm -rf %t
<VariableDecl>// RUN: rm -rf %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
Expand All @@ -9,6 +9,8 @@
// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t
// RUN: diff -u %s %t

let <PatternBinding><IdentifierPattern>strings</IdentifierPattern><TypeAnnotation>: [<SimpleTypeIdentifier>Strin</SimpleTypeIdentifier>[<UnresolvedPatternExpr><IdentifierPattern>g</IdentifierPattern></UnresolvedPatternExpr>]?</TypeAnnotation></PatternBinding></VariableDecl><FunctionDecl>

// Function body without closing brace token.
func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<VariableDecl>
var <PatternBinding><IdentifierPattern>a </IdentifierPattern><InitializerClause>= <IntegerLiteralExpr>2</IntegerLiteralExpr></InitializerClause></PatternBinding></VariableDecl></CodeBlock></FunctionDecl>
2 changes: 2 additions & 0 deletions test/Syntax/round_trip_invalid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t
// RUN: diff -u %s %t

let strings: [Strin[g]?

// Function body without closing brace token.
func foo() {
var a = 2
2 changes: 2 additions & 0 deletions test/Syntax/round_trip_misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ typealias c = @foobar(a) () -> Void
let d = \.foo
let e = \.[1]
let f = \.?.bar

let optionalArray: [Int]?