Skip to content

Commit d9c7ec4

Browse files
CodaFijpsim
authored andcommitted
Harden Closure Capture Parsing
Fix a regression where we didn't recognize unowned(safe) in closure captures. Fixes swiftlang#722 rdar://99657725
1 parent 32c6f30 commit d9c7ec4

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,15 +1715,17 @@ extension Parser {
17151715
// Parse identifier (',' identifier)*
17161716
var keepGoing: RawTokenSyntax? = nil
17171717
repeat {
1718+
let unexpected: RawUnexpectedNodesSyntax?
17181719
let name: RawTokenSyntax
17191720
if self.currentToken.isIdentifier {
1721+
unexpected = nil
17201722
name = self.consumeIdentifier()
17211723
} else {
1722-
name = self.eat(.wildcardKeyword)
1724+
(unexpected, name) = self.expect(.wildcardKeyword)
17231725
}
17241726
keepGoing = consume(if: .comma)
17251727
params.append(RawClosureParamSyntax(
1726-
name: name, trailingComma: keepGoing, arena: self.arena))
1728+
unexpected, name: name, trailingComma: keepGoing, arena: self.arena))
17271729
} while keepGoing != nil && loopProgress.evaluate(currentToken)
17281730
}
17291731

@@ -1772,7 +1774,11 @@ extension Parser {
17721774
specifiers.append(self.consumeIdentifier())
17731775
if let lparen = self.consume(if: .leftParen) {
17741776
specifiers.append(lparen)
1775-
specifiers.append(self.expectWithoutLookahead(.identifier, "unsafe"))
1777+
if self.currentToken.tokenText == "safe" {
1778+
specifiers.append(self.expectWithoutLookahead(.identifier, "safe"))
1779+
} else {
1780+
specifiers.append(self.expectWithoutLookahead(.identifier, "unsafe"))
1781+
}
17761782
specifiers.append(self.expectWithoutLookahead(.rightParen))
17771783
}
17781784
} else if (self.currentToken.isIdentifier || self.at(.selfKeyword)) {

Tests/SwiftParserTest/Expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ final class ExpressionTests: XCTestCase {
443443
)
444444
}
445445

446-
func testClouserExpression() {
446+
func testClosureExpression() {
447447
AssertParse(
448448
"""
449449
let :(#^DIAG_1^#..)->

Tests/SwiftParserTest/Types.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,9 @@ final class TypeTests: XCTestCase {
3939
throws -> Void in }
4040
""",
4141
{ $0.parseClosureExpression() })
42+
43+
AssertParse("""
44+
{ [weak a, unowned(safe) self, b = 3] (a: Int, b: Int, _: Int) -> Int in }
45+
""")
4246
}
4347
}

0 commit comments

Comments
 (0)