Skip to content

Commit bc84e48

Browse files
committed
[consume] Change how we parse consume to match copy so we properly handle consume used as a name in case stmt as pattern match.
rdar://109413278
1 parent ac4556e commit bc84e48

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,16 @@ extension Parser {
504504

505505
case (.consumeKeyword, let handle)?:
506506
// `consume` is only contextually a keyword, if it's followed by an
507-
// identifier or keyword on the same line.
508-
let next = peek()
509-
if next.isAtStartOfLine {
510-
break
511-
}
512-
if next.rawTokenKind != .identifier,
513-
next.rawTokenKind != .dollarIdentifier,
514-
next.rawTokenKind != .keyword
515-
{
507+
// identifier or keyword on the same line. We do this to ensure that we do
508+
// not break any copy functions defined by users. This is following with
509+
// what we have done for the consume keyword.
510+
switch self.peek() {
511+
case TokenSpec(.identifier, allowAtStartOfLine: false),
512+
TokenSpec(.dollarIdentifier, allowAtStartOfLine: false),
513+
TokenSpec(.self, allowAtStartOfLine: false):
516514
break
515+
default:
516+
break EXPR_PREFIX // break out of the outer `switch`
517517
}
518518

519519
let consumeTok = self.eat(handle)

Tests/SwiftParserTest/translated/MoveExprTests.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,20 @@ final class MoveExprTests: XCTestCase {
8282
"""
8383
)
8484
}
85-
}}
85+
86+
func testConsumeVariableNameInCast() {
87+
assertParse(
88+
"""
89+
class ParentKlass {}
90+
class SubKlass : ParentKlass {}
91+
92+
func test(_ x: SubKlass) {
93+
switch x {
94+
case let consume as ParentKlass:
95+
fallthrough
96+
}
97+
}
98+
""")
99+
}
100+
}
86101

0 commit comments

Comments
 (0)