Skip to content

Avoid creating binding patterns in a couple more positions #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1220,10 +1220,10 @@ extension Parser {
)
)
case (.identifier, let handle)?, (.self, let handle)?, (.`init`, let handle)?:
// If we have "case let x." or "case let x(", we parse x as a normal
// name, not a binding, because it is the start of an enum pattern or
// call pattern.
if pattern.admitsBinding && !self.lookahead().isNextTokenCallPattern() {
// If we have "case let x" followed by ".", "(", "[", or a generic
// argument list, we parse x as a normal name, not a binding, because it
// is the start of an enum or expr pattern.
if pattern.admitsBinding && self.lookahead().isInBindingPatternPosition() {
let identifier = self.eat(handle)
let pattern = RawPatternSyntax(
RawIdentifierPatternSyntax(
Expand Down Expand Up @@ -2759,13 +2759,21 @@ extension Parser.Lookahead {
return self.peek().isLexerClassifiedKeyword || TokenSpec(.identifier) ~= self.peek()
}

fileprivate func isNextTokenCallPattern() -> Bool {
fileprivate func isInBindingPatternPosition() -> Bool {
// Cannot form a binding pattern if a generic argument list follows, this
// is something like 'case let E<Int>.foo(x)'.
if self.peek().isContextualPunctuator("<") {
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
return !lookahead.canParseAsGenericArgumentList()
}
switch self.peek().rawTokenKind {
case .period,
.leftParen:
return true
default:
// A '.' indicates a member access, '(' and '[' indicate a function call or
// subscript. We can't form a binding pattern as the base of these.
case .period, .leftParen, .leftSquare:
return false
default:
return true
}
}
}
171 changes: 171 additions & 0 deletions Tests/SwiftParserTest/PatternTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_spi(RawSyntax) import SwiftSyntax
@_spi(RawSyntax) import SwiftParser
import XCTest

final class PatternTests: XCTestCase {
private var genericArgEnumPattern: Syntax {
// let E<Int>.e(y)
Syntax(
ValueBindingPatternSyntax(
bindingKeyword: .keyword(.let),
valuePattern: ExpressionPatternSyntax(
expression: FunctionCallExprSyntax(
calledExpression: MemberAccessExprSyntax(
base: SpecializeExprSyntax(
expression: IdentifierExprSyntax(identifier: .identifier("E")),
genericArgumentClause: GenericArgumentClauseSyntax(
arguments: .init([
.init(argumentType: SimpleTypeIdentifierSyntax(name: .identifier("Int")))
])
)
),
name: .identifier("e")
),
leftParen: .leftParenToken(),
argumentList: TupleExprElementListSyntax([
.init(
expression: UnresolvedPatternExprSyntax(
pattern: IdentifierPatternSyntax(identifier: .identifier("y"))
)
)
]),
rightParen: .rightParenToken()
)
)
)
)
}

func testNonBinding1() {
assertParse(
"""
if case let E<Int>.e(y) = x {}
""",
substructure: genericArgEnumPattern
)
}

func testNonBinding2() {
assertParse(
"""
switch e {
case let E<Int>.e(y):
y
}
""",
substructure: genericArgEnumPattern
)
}

private var tupleWithSubscriptAndBindingPattern: Syntax {
// let (y[0], z)
Syntax(
ValueBindingPatternSyntax(
bindingKeyword: .keyword(.let),
valuePattern: ExpressionPatternSyntax(
expression: TupleExprSyntax(
elements: .init([
.init(
expression: SubscriptExprSyntax(
calledExpression: IdentifierExprSyntax(identifier: .identifier("y")),
leftBracket: .leftSquareToken(),
argumentList: TupleExprElementListSyntax([
.init(expression: IntegerLiteralExprSyntax(digits: .integerLiteral("0")))
]),
rightBracket: .rightSquareToken()
),
trailingComma: .commaToken()
),
.init(
expression: UnresolvedPatternExprSyntax(
pattern: IdentifierPatternSyntax(identifier: .identifier("z"))
)
),
])
)
)
)
)
}

func testNonBinding3() {
assertParse(
"""
if case let (y[0], z) = x {}
""",
substructure: tupleWithSubscriptAndBindingPattern
)
}

func testNonBinding4() {
assertParse(
"""
switch x {
case let (y[0], z):
z
}
""",
substructure: tupleWithSubscriptAndBindingPattern
)
}

private var subscriptWithBindingPattern: Syntax {
// let y[z]
Syntax(
ValueBindingPatternSyntax(
bindingKeyword: .keyword(.let),
valuePattern: ExpressionPatternSyntax(
expression: SubscriptExprSyntax(
calledExpression: IdentifierExprSyntax(identifier: .identifier("y")),
leftBracket: .leftSquareToken(),
argumentList: TupleExprElementListSyntax([
.init(
expression: UnresolvedPatternExprSyntax(
pattern: IdentifierPatternSyntax(identifier: .identifier("z"))
)
)
]),
rightBracket: .rightSquareToken()
)
)
)
)
}

func testNonBinding5() {
assertParse(
"""
if case let y[z] = x {}
""",
substructure: subscriptWithBindingPattern
)
}

func testNonBinding6() {
assertParse(
"""
switch 0 {
case let y[z]:
z
case y[z]:
0
default:
0
}
""",
substructure: subscriptWithBindingPattern
)
}
}