Skip to content

Commit 5c57421

Browse files
authored
Merge pull request swiftlang#2980 from DougGregor/disambiguate-unsafe-member-access
2 parents c09cd63 + df30392 commit 5c57421

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Sources/SwiftParser/Expressions.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,15 @@ extension Parser {
445445
)
446446
)
447447
case (.unsafe, let handle)?:
448-
if self.peek().isAtStartOfLine || self.peek(isAt: .rightParen) {
448+
if self.peek().isAtStartOfLine
449+
// Closing paired syntax
450+
|| self.peek(isAt: .rightParen, .rightSquare, .rightBrace)
451+
// Assignment
452+
|| self.peek(isAt: .equal)
453+
// `unsafe.something` with no trivia
454+
|| (self.peek(isAt: .period) && self.peek().leadingTriviaByteLength == 0
455+
&& self.currentToken.trailingTriviaByteLength == 0)
456+
{
449457
break EXPR_PREFIX
450458
}
451459

Tests/SwiftParserTest/ExpressionTests.swift

+52
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,58 @@ final class StatementExpressionTests: ParserTestCase {
22062206
""",
22072207
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
22082208
)
2209+
2210+
assertParse(
2211+
"""
2212+
func f() {
2213+
1️⃣unsafe.lock()
2214+
}
2215+
""",
2216+
substructure: MemberAccessExprSyntax(
2217+
base: DeclReferenceExprSyntax(baseName: .identifier("unsafe")),
2218+
period: .periodToken(),
2219+
declName: DeclReferenceExprSyntax(baseName: .identifier("lock"))
2220+
),
2221+
substructureAfterMarker: "1️⃣"
2222+
)
2223+
2224+
assertParse(
2225+
"""
2226+
func f() {
2227+
unsafe .lock
2228+
}
2229+
""",
2230+
substructure: UnsafeExprSyntax(
2231+
expression: MemberAccessExprSyntax(name: .identifier("lock"))
2232+
)
2233+
)
2234+
2235+
assertParse(
2236+
"""
2237+
func f() {
2238+
_ = [unsafe]
2239+
}
2240+
""",
2241+
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
2242+
)
2243+
2244+
assertParse(
2245+
"""
2246+
func f() {
2247+
_ = [unsafe]
2248+
}
2249+
""",
2250+
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
2251+
)
2252+
2253+
assertParse(
2254+
"""
2255+
func f() {
2256+
unsafe = 17
2257+
}
2258+
""",
2259+
substructure: DeclReferenceExprSyntax(baseName: .identifier("unsafe"))
2260+
)
22092261
}
22102262

22112263
func testUnterminatedInterpolationAtEndOfMultilineStringLiteral() {

0 commit comments

Comments
 (0)