Skip to content

Commit 19458ae

Browse files
authored
Merge pull request #2982 from DougGregor/more-unsafe-disambig
[SE-0458] Improved disambiguation for `unsafe` expressions
2 parents 5c57421 + 0ca4948 commit 19458ae

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Sources/SwiftParser/Expressions.swift

+5
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,11 @@ extension Parser {
450450
|| self.peek(isAt: .rightParen, .rightSquare, .rightBrace)
451451
// Assignment
452452
|| self.peek(isAt: .equal)
453+
// As an argument label or in a list context.
454+
|| self.peek(isAt: .colon, .comma)
455+
// Start of a closure in a context where it should be interpreted as
456+
// being part of a statement.
457+
|| (flavor == .stmtCondition && self.peek(isAt: .leftBrace))
453458
// `unsafe.something` with no trivia
454459
|| (self.peek(isAt: .period) && self.peek().leadingTriviaByteLength == 0
455460
&& self.currentToken.trailingTriviaByteLength == 0)

Tests/SwiftParserTest/ExpressionTests.swift

+37
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,43 @@ final class StatementExpressionTests: ParserTestCase {
22582258
""",
22592259
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
22602260
)
2261+
2262+
assertParse(
2263+
"""
2264+
func f() {
2265+
let unsafe = 17
2266+
}
2267+
""",
2268+
substructure: IdentifierPatternSyntax(identifier: .identifier("unsafe"))
2269+
)
2270+
2271+
assertParse(
2272+
"""
2273+
func f() {
2274+
f(1️⃣unsafe, blah: unsafe, unsafe, unsafe: unsafe, unsafe)
2275+
}
2276+
""",
2277+
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe")),
2278+
substructureAfterMarker: "1️⃣"
2279+
)
2280+
2281+
assertParse(
2282+
"""
2283+
func f() {
2284+
guard let unsafe = a else { }
2285+
}
2286+
""",
2287+
substructure: IdentifierPatternSyntax(identifier: .identifier("unsafe"))
2288+
)
2289+
2290+
assertParse(
2291+
"""
2292+
func f() {
2293+
if unsafe { }
2294+
}
2295+
""",
2296+
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
2297+
)
22612298
}
22622299

22632300
func testUnterminatedInterpolationAtEndOfMultilineStringLiteral() {

0 commit comments

Comments
 (0)