Skip to content

Commit 25d5fff

Browse files
committed
[reference-bindings] Put inout parsing behind a flag harder.
In my earlier commit that attempted to do this I wasn't aggressive enough. In this commit, I was more aggressive in putting it behind a flag and as a result we reject all of the patterns in the tests I added into tree.
1 parent 46da4e9 commit 25d5fff

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
220220
// this, because then we can produce a fixit to rewrite the && into a ,
221221
// if we're in a stmt-condition.
222222
if (Tok.getText() == "&&" &&
223-
peekToken().isAny(tok::pound_available, tok::pound_unavailable,
224-
tok::pound__hasSymbol, tok::kw_let, tok::kw_var,
225-
tok::kw_case, tok::kw_inout))
223+
(peekToken().isAny(tok::pound_available, tok::pound_unavailable,
224+
tok::pound__hasSymbol, tok::kw_let, tok::kw_var,
225+
tok::kw_case) ||
226+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
227+
peekToken().isAny(tok::kw_inout))))
226228
goto done;
227229

228230
// Parse the operator.

lib/Parse/ParsePattern.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
327327

328328
// If let or var is being used as an argument label, allow it but
329329
// generate a warning.
330-
if (!isClosure && Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_inout)) {
330+
if (!isClosure &&
331+
(Tok.isAny(tok::kw_let, tok::kw_var) ||
332+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
333+
Tok.isAny(tok::kw_inout)))) {
331334
diagnose(Tok, diag::parameter_let_var_as_attr, Tok.getText())
332335
.fixItReplace(Tok.getLoc(), "`" + Tok.getText().str() + "`");
333336
}
@@ -1286,7 +1289,9 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
12861289
// through the expr parser for ambiguous productions.
12871290

12881291
// Parse productions that can only be patterns.
1289-
if (Tok.isAny(tok::kw_var, tok::kw_let, tok::kw_inout)) {
1292+
if (Tok.isAny(tok::kw_var, tok::kw_let) ||
1293+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
1294+
Tok.isAny(tok::kw_inout))) {
12901295
assert(Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_inout) && "expects var or let");
12911296
auto newPatternBindingState = PatternBindingState(Tok);
12921297
SourceLoc varLoc = consumeToken();
@@ -1373,7 +1378,9 @@ Parser::parseMatchingPatternAsBinding(PatternBindingState newState,
13731378
}
13741379

13751380
bool Parser::isOnlyStartOfMatchingPattern() {
1376-
return Tok.isAny(tok::kw_var, tok::kw_let, tok::kw_is, tok::kw_inout);
1381+
return Tok.isAny(tok::kw_var, tok::kw_let, tok::kw_is) ||
1382+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
1383+
Tok.isAny(tok::kw_inout));
13771384
}
13781385

13791386

lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,9 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
15681568

15691569
// Parse the basic expression case. If we have a leading let/var/case
15701570
// keyword or an assignment, then we know this is a binding.
1571-
if (Tok.isNot(tok::kw_let, tok::kw_var, tok::kw_case, tok::kw_inout)) {
1571+
if (Tok.isNot(tok::kw_let, tok::kw_var, tok::kw_case) &&
1572+
(!Context.LangOpts.hasFeature(Feature::ReferenceBindings) ||
1573+
Tok.isNot(tok::kw_inout))) {
15721574
// If we lack it, then this is theoretically a boolean condition.
15731575
// However, we also need to handle migrating from Swift 2 syntax, in
15741576
// which a comma followed by an expression could actually be a pattern
@@ -1600,7 +1602,9 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
16001602
}
16011603

16021604
SourceLoc IntroducerLoc;
1603-
if (Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_case, tok::kw_inout)) {
1605+
if (Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_case) ||
1606+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
1607+
Tok.isAny(tok::kw_inout))) {
16041608
BindingKindStr = Tok.getText();
16051609
IntroducerLoc = consumeToken();
16061610
} else {

lib/Parse/Parser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ void Parser::skipListUntilDeclRBrace(SourceLoc startLoc, tok T1, tok T2) {
747747

748748
// Could have encountered something like `_ var:`
749749
// or `let foo:` or `var:`
750-
if (Tok.isAny(tok::kw_var, tok::kw_let, tok::kw_inout)) {
750+
if (Tok.isAny(tok::kw_var, tok::kw_let) ||
751+
(Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
752+
Tok.isAny(tok::kw_inout))) {
751753
if (possibleDeclStartsLine && !hasDelimiter) {
752754
break;
753755
}

test/Parse/matching_patterns.swift

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ case var a:
3030
a = 1
3131
case let a:
3232
a = 1 // expected-error {{cannot assign}}
33-
case inout a:
34-
a = 1
33+
case inout a: // expected-error {{'inout' may only be used on parameters}} expected-error {{'is' keyword required to pattern match against type name}}
34+
a = 1 // expected-error {{cannot find 'a' in scope}}
3535
case var var a: // expected-error {{'var' cannot appear nested inside another 'var' or 'let' pattern}}
3636
a += 1
3737
case var let a: // expected-error {{'let' cannot appear nested inside another 'var' or 'let' pattern}}
@@ -54,14 +54,6 @@ case _: // expected-warning {{case is already handled by previous patterns; cons
5454
()
5555
}
5656

57-
switch (x,x) {
58-
case (inout a, inout a): // expected-error {{invalid redeclaration of 'a'}}
59-
// expected-note @-1 {{'a' previously declared here}}
60-
// xpected-warning @-2 {{variable 'a' was never used; consider replacing with '_' or removing it}}
61-
// xpected-warning @-3 {{variable 'a' was never used; consider replacing with '_' or removing it}}
62-
break
63-
}
64-
6557
var e : Any = 0
6658

6759
switch e { // expected-error {{switch must be exhaustive}} expected-note{{do you want to add a default clause?}}
@@ -128,12 +120,6 @@ if case let .Naught(value) = n {} // expected-error{{pattern with associated val
128120
if case let .Naught(value1, value2, value3) = n {} // expected-error{{pattern with associated values does not match enum case 'Naught'}}
129121
// expected-note@-1 {{remove associated values to make the pattern match}} {{20-44=}}
130122

131-
if case inout .Naught(value) = n {} // expected-error{{pattern with associated values does not match enum case 'Naught'}}
132-
// expected-note@-1 {{remove associated values to make the pattern match}} {{22-29=}}
133-
if case inout .Naught(value1, value2, value3) = n {} // expected-error{{pattern with associated values does not match enum case 'Naught'}}
134-
// expected-note@-1 {{remove associated values to make the pattern match}} {{22-46=}}
135-
136-
137123

138124
switch n {
139125
case Foo.A: // expected-error{{enum case 'A' is not a member of type 'Voluntary<Int>'}}
@@ -156,7 +142,7 @@ case Voluntary<Int>.Mere,
156142
()
157143
case .Twain,
158144
.Twain(_), // expected-warning {{enum case 'Twain' has 2 associated values; matching them as a tuple is deprecated}}
159-
// expected-note@-74 {{'Twain' declared here}}
145+
// expected-note@-68 {{'Twain' declared here}}
160146
.Twain(_, _),
161147
.Twain(_, _, _): // expected-error{{tuple pattern has the wrong length for tuple type '(Int, Int)'}}
162148
()
@@ -299,9 +285,6 @@ case (_, var e, 3) +++ (1, 2, 3):
299285
// expected-error@-1{{'_' can only appear in a pattern or on the left side of an assignment}}
300286
()
301287
case (let (_, _, _)) + 1:
302-
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
303-
()
304-
case (inout (_, _, 2)) + 1:
305288
// expected-error@-1 {{'_' can only appear in a pattern or on the left side of an assignment}}
306289
()
307290
}

0 commit comments

Comments
 (0)