Skip to content

Commit 7ff66fd

Browse files
committed
Accept borrowing in pattern matches without underscore.
rdar://126305009
1 parent 9f2acb2 commit 7ff66fd

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

Diff for: include/swift/AST/DiagnosticsParse.def

+3-1
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,10 @@ ERROR(extra_var_in_multiple_pattern_list,none,
996996
"%0 must be bound in every pattern", (Identifier))
997997
ERROR(let_pattern_in_immutable_context,none,
998998
"'let' pattern cannot appear nested in an already immutable context", ())
999+
WARNING(borrowing_syntax_change,none,
1000+
"'_borrowing' spelling is deprecated; use 'borrowing' without the underscore", ())
9991001
ERROR(borrowing_subpattern_unsupported,none,
1000-
"'_borrowing' pattern modifier must be directly applied to pattern variable name", ())
1002+
"'borrowing' pattern modifier must be directly applied to pattern variable name", ())
10011003
ERROR(specifier_must_have_type,none,
10021004
"%0 arguments must have a type specified", (StringRef))
10031005
ERROR(expected_rparen_parameter,PointsToFirstBadToken,

Diff for: lib/Parse/ParsePattern.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -1352,10 +1352,17 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
13521352
// The `borrowing` modifier is a contextual keyword, so it's only accepted
13531353
// directly applied to a binding name, as in `case .foo(borrowing x)`.
13541354
if (Context.LangOpts.hasFeature(Feature::BorrowingSwitch)) {
1355-
if (Tok.isContextualKeyword("_borrowing")
1355+
if ((Tok.isContextualKeyword("_borrowing")
1356+
|| Tok.isContextualKeyword("borrowing"))
13561357
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
13571358
tok::code_complete)
13581359
&& !peekToken().isAtStartOfLine()) {
1360+
if (Tok.isContextualKeyword("_borrowing")) {
1361+
diagnose(Tok.getLoc(),
1362+
diag::borrowing_syntax_change)
1363+
.fixItReplace(Tok.getLoc(), "borrowing");
1364+
}
1365+
13591366
Tok.setKind(tok::contextual_keyword);
13601367
SourceLoc borrowingLoc = consumeToken();
13611368

@@ -1464,7 +1471,8 @@ Parser::parseMatchingPatternAsBinding(PatternBindingState newState,
14641471

14651472
bool Parser::isOnlyStartOfMatchingPattern() {
14661473
if (Context.LangOpts.hasFeature(Feature::BorrowingSwitch)) {
1467-
if (Tok.isContextualKeyword("_borrowing")
1474+
if ((Tok.isContextualKeyword("_borrowing")
1475+
|| Tok.isContextualKeyword("borrowing"))
14681476
&& peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
14691477
tok::code_complete)
14701478
&& !peekToken().isAtStartOfLine()) {

Diff for: test/Inputs/Swiftskell.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension Maybe: Copyable {}
6767
extension Maybe: Show where Value: Show & ~Copyable {
6868
public borrowing func show() -> String {
6969
switch self {
70-
case let .just(_borrowing elm):
70+
case let .just(borrowing elm):
7171
return elm.show()
7272
case .nothing:
7373
return "<nothing>"
@@ -78,9 +78,9 @@ extension Maybe: Show where Value: Show & ~Copyable {
7878
extension Maybe: Eq where Value: Eq, Value: ~Copyable {
7979
public static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
8080
switch a {
81-
case let .just(_borrowing a1):
81+
case let .just(borrowing a1):
8282
switch b {
83-
case let .just(_borrowing b1):
83+
case let .just(borrowing b1):
8484
return a1 == b1
8585
case .nothing:
8686
return false

Diff for: test/Parse/pattern_borrow_bindings.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct SourceBreakTest {
2323
func callAsFunction() -> Bar { fatalError() }
2424
}
2525

26-
let _borrowing = SourceBreakTest()
26+
let borrowing = SourceBreakTest()
2727

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

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

3434
func testBorrowingPatterns(bar: borrowing Bar) {
3535
switch bar {
36-
case _borrowing .foo(): // parses as `_borrowing.foo()` as before
36+
case borrowing .foo(): // parses as `borrowing.foo()` as before
3737
break
38-
case _borrowing (): // parses as `_borrowing()` as before
38+
case borrowing (): // parses as `borrowing()` as before
3939
break
4040

41-
case _borrowing x:
41+
case borrowing x:
4242
useBorrowBar(x)
4343

44-
case .payload(_borrowing x):
44+
case .payload(borrowing x):
4545
useBorrowFoo(x)
4646

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

50+
case _borrowing x: // expected-warning{{'_borrowing' spelling is deprecated}} {{10-20=borrowing}}
51+
useBorrowBar(x)
52+
5053
default:
5154
break
5255
}

Diff for: test/SILOptimizer/moveonly_borrowing_switch_copyable_subpattern.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ func test(borrowing foo: borrowing Foo) {
3232
eat(x)
3333
nibble(x)
3434

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

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

4747
// Explicit copies are OK.
48-
case .copyablePayload(_borrowing x)
48+
case .copyablePayload(borrowing x)
4949
where hungryCondition(copy x):
5050
eat(copy x)
5151
nibble(x)
5252

53-
case .copyablePayload(_borrowing x):
53+
case .copyablePayload(borrowing x):
5454
nibble(x)
5555
}
5656
}

0 commit comments

Comments
 (0)