Skip to content

[6.0] Accept borrowing in pattern matches without underscore. #72996

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
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
4 changes: 3 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,10 @@ ERROR(extra_var_in_multiple_pattern_list,none,
"%0 must be bound in every pattern", (Identifier))
ERROR(let_pattern_in_immutable_context,none,
"'let' pattern cannot appear nested in an already immutable context", ())
WARNING(borrowing_syntax_change,none,
"'_borrowing' spelling is deprecated; use 'borrowing' without the underscore", ())
ERROR(borrowing_subpattern_unsupported,none,
"'_borrowing' pattern modifier must be directly applied to pattern variable name", ())
"'borrowing' pattern modifier must be directly applied to pattern variable name", ())
ERROR(specifier_must_have_type,none,
"%0 arguments must have a type specified", (StringRef))
ERROR(expected_rparen_parameter,PointsToFirstBadToken,
Expand Down
12 changes: 10 additions & 2 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,17 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
// The `borrowing` modifier is a contextual keyword, so it's only accepted
// directly applied to a binding name, as in `case .foo(borrowing x)`.
if (Context.LangOpts.hasFeature(Feature::BorrowingSwitch)) {
if (Tok.isContextualKeyword("_borrowing")
if ((Tok.isContextualKeyword("_borrowing")
|| Tok.isContextualKeyword("borrowing"))
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
tok::code_complete)
&& !peekToken().isAtStartOfLine()) {
if (Tok.isContextualKeyword("_borrowing")) {
diagnose(Tok.getLoc(),
diag::borrowing_syntax_change)
.fixItReplace(Tok.getLoc(), "borrowing");
}

Tok.setKind(tok::contextual_keyword);
SourceLoc borrowingLoc = consumeToken();

Expand Down Expand Up @@ -1464,7 +1471,8 @@ Parser::parseMatchingPatternAsBinding(PatternBindingState newState,

bool Parser::isOnlyStartOfMatchingPattern() {
if (Context.LangOpts.hasFeature(Feature::BorrowingSwitch)) {
if (Tok.isContextualKeyword("_borrowing")
if ((Tok.isContextualKeyword("_borrowing")
|| Tok.isContextualKeyword("borrowing"))
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
tok::code_complete)
&& !peekToken().isAtStartOfLine()) {
Expand Down
6 changes: 3 additions & 3 deletions test/Inputs/Swiftskell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension Maybe: Copyable {}
extension Maybe: Show where Value: Show & ~Copyable {
public borrowing func show() -> String {
switch self {
case let .just(_borrowing elm):
case let .just(borrowing elm):
return elm.show()
case .nothing:
return "<nothing>"
Expand All @@ -78,9 +78,9 @@ extension Maybe: Show where Value: Show & ~Copyable {
extension Maybe: Eq where Value: Eq, Value: ~Copyable {
public static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
switch a {
case let .just(_borrowing a1):
case let .just(borrowing a1):
switch b {
case let .just(_borrowing b1):
case let .just(borrowing b1):
return a1 == b1
case .nothing:
return false
Expand Down
15 changes: 9 additions & 6 deletions test/Parse/pattern_borrow_bindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SourceBreakTest {
func callAsFunction() -> Bar { fatalError() }
}

let _borrowing = SourceBreakTest()
let borrowing = SourceBreakTest()

func ~=(_: borrowing Bar, _: borrowing Bar) -> Bool { fatalError() }

Expand All @@ -33,20 +33,23 @@ func useBorrowPayload(_: borrowing Payload) { fatalError() }

func testBorrowingPatterns(bar: borrowing Bar) {
switch bar {
case _borrowing .foo(): // parses as `_borrowing.foo()` as before
case borrowing .foo(): // parses as `borrowing.foo()` as before
break
case _borrowing (): // parses as `_borrowing()` as before
case borrowing (): // parses as `borrowing()` as before
break

case _borrowing x:
case borrowing x:
useBorrowBar(x)

case .payload(_borrowing x):
case .payload(borrowing x):
useBorrowFoo(x)

case _borrowing x.member: // expected-error{{'_borrowing' pattern modifier must be directly applied to pattern variable name}} expected-error{{cannot find 'x' in scope}}
case borrowing x.member: // expected-error{{'borrowing' pattern modifier must be directly applied to pattern variable name}} expected-error{{cannot find 'x' in scope}}
break

case _borrowing x: // expected-warning{{'_borrowing' spelling is deprecated}} {{10-20=borrowing}}
useBorrowBar(x)

default:
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ func test(borrowing foo: borrowing Foo) {
eat(x)
nibble(x)

// `_borrowing` match variables impose the no-implicit-copy constraint
// `borrowing` match variables impose the no-implicit-copy constraint
// like `borrowing` parameters do.
case .copyablePayload(_borrowing x) // expected-error{{'x' is borrowed and cannot be consumed}}
case .copyablePayload(borrowing x) // expected-error{{'x' is borrowed and cannot be consumed}}
where hungryCondition(x): // expected-note{{consumed here}}
eat(x) // expected-note{{consumed here}}
nibble(x)

case .copyablePayload(_borrowing x) // expected-error{{'x' is borrowed and cannot be consumed}}
case .copyablePayload(borrowing x) // expected-error{{'x' is borrowed and cannot be consumed}}
where condition(x):
eat(x) // expected-note{{consumed here}}
nibble(x)

// Explicit copies are OK.
case .copyablePayload(_borrowing x)
case .copyablePayload(borrowing x)
where hungryCondition(copy x):
eat(copy x)
nibble(x)

case .copyablePayload(_borrowing x):
case .copyablePayload(borrowing x):
nibble(x)
}
}