Repository: swiftlang/swift-syntax (swift)

Issue:
Wrong diagnostics for ternary expression when only the colon is missing
### Description

When diagnostics are generated for ternary expressions where the `:` is missing but both of the first and second choice expressions are present, the incorrect diagnostic message is used and the wrong Fix-It is applied.


### Steps to Reproduce

Example: 
```swift
foo ? 1 2
```

This results in the following diagnostic message and Fix-It:
```
expected ':' and expression after '? ...' in ternary expression
```
```swift
foo ? 1 : <#expression#>2
```


As the expression isn't missing in the example I expected the following diagnostic message and Fix-It:
```
expected ':' after '? ...' in ternary expression
```
```swift
foo ? 1 : 2
```

Pull request description:
Resolve #1624 

Previously, in the ternary operator, the absence of a colon always resulted in parsing it as the right-hand side expression being missing.
Added logic to check if the next token is the same kind as the token before the colon when parsing a ternary operator, if there is no colon.

Patch:
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


Test patch:
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
+        """
+    )
+  }
 }