diff --git a/Sources/SwiftParser/Expressions.swift b/Sources/SwiftParser/Expressions.swift index e9332141879..b4205e88327 100644 --- a/Sources/SwiftParser/Expressions.swift +++ b/Sources/SwiftParser/Expressions.swift @@ -318,10 +318,7 @@ extension Parser { ) let rhs: RawExprSyntax? - if colon.isMissing { - // If the colon is missing there's not much more structure we can - // expect out of this expression sequence. Emit a missing expression - // to end the parsing here. + if colon.isMissing, currentToken.isAtStartOfLine { rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena)) } else { rhs = nil diff --git a/Tests/SwiftParserTest/translated/InvalidIfExprTests.swift b/Tests/SwiftParserTest/translated/InvalidIfExprTests.swift index bc9acf35685..80cc943f4ec 100644 --- a/Tests/SwiftParserTest/translated/InvalidIfExprTests.swift +++ b/Tests/SwiftParserTest/translated/InvalidIfExprTests.swift @@ -69,4 +69,43 @@ final class InvalidIfExprTests: XCTestCase { ) } + func testInvalidIfExpr5() { + assertParse( + """ + foo ? 1 1️⃣2 + """, + diagnostics: [ + DiagnosticSpec(message: "expected ':' after '? ...' in ternary expression", fixIts: ["insert ':'"]) + ], + fixedSource: "foo ? 1 : 2" + ) + } + + func testInvalidIfExpr6() { + assertParse( + """ + foo ? 1 1️⃣ + """, + diagnostics: [ + DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]) + ], + fixedSource: "foo ? 1 : <#expression#>" + ) + } + + func testInvalidIfExpr7() { + assertParse( + """ + condition ? 1 1️⃣ + someOtherVariable + """, + diagnostics: [ + DiagnosticSpec(message: "expected ':' and expression after '? ...' in ternary expression", fixIts: ["insert ':' and expression"]) + ], + fixedSource: """ + condition ? 1 : <#expression#> + someOtherVariable + """ + ) + } }