Skip to content

Commit b96e06d

Browse files
committed
REVERTME: Temporarily make vars in refutable patterns a warning
Revert "Make function parameters and refutable patterns always immutable" This reverts commit 8f2fbdc. Once we have finally merged master into the Swift 2.2 branch to be, we should revert this commit to turn the errors back on for Swift 3.0.
1 parent 6fb18a3 commit b96e06d

File tree

11 files changed

+53
-47
lines changed

11 files changed

+53
-47
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,8 @@ ERROR(untyped_pattern_ellipsis,pattern_parsing,none,
641641
"'...' cannot be applied to a subpattern which is not explicitly typed", ())
642642
ERROR(non_func_decl_pattern_init,pattern_parsing,none,
643643
"default argument is only permitted for a non-curried function parameter",())
644-
ERROR(var_not_allowed_in_pattern,pattern_parsing, none,
645-
"Use of 'var' binding here is not allowed", ())
646-
WARNING(let_on_param_is_redundant,pattern_parsing, none,
647-
"'let' keyword is unnecessary; function parameters are immutable by default", (unsigned))
644+
WARNING(var_not_allowed_in_pattern,pattern_parsing, none,
645+
"Use of '%select{var|let}0' binding here is deprecated and will be removed in a future version of Swift", (unsigned))
648646
ERROR(var_pattern_in_var,pattern_parsing,none,
649647
"'%select{var|let}0' cannot appear nested inside another 'var' or "
650648
"'let' pattern", (unsigned))

lib/Parse/ParsePattern.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
179179
param.LetVarInOutLoc = consumeToken();
180180
param.SpecifierKind = ParsedParameter::InOut;
181181
} else if (Tok.is(tok::kw_let)) {
182-
diagnose(Tok.getLoc(), diag::let_on_param_is_redundant,
182+
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern,
183183
Tok.is(tok::kw_let)).fixItRemove(Tok.getLoc());
184184
param.LetVarInOutLoc = consumeToken();
185185
param.SpecifierKind = ParsedParameter::Let;
186186
} else if (Tok.is(tok::kw_var)) {
187-
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern)
188-
.fixItRemove(Tok.getLoc());
187+
diagnose(Tok.getLoc(), diag::var_not_allowed_in_pattern,
188+
Tok.is(tok::kw_let)).fixItRemove(Tok.getLoc());
189189
param.LetVarInOutLoc = consumeToken();
190-
param.SpecifierKind = ParsedParameter::Let;
190+
param.SpecifierKind = ParsedParameter::Var;
191191
}
192192

193193
// Redundant specifiers are fairly common, recognize, reject, and recover
@@ -855,8 +855,8 @@ ParserResult<Pattern> Parser::parsePattern() {
855855
} else {
856856
// In an always immutable context, `var` is not allowed.
857857
if (alwaysImmutable)
858-
diagnose(varLoc, diag::var_not_allowed_in_pattern)
859-
.fixItRemove(varLoc);
858+
diagnose(varLoc, diag::var_not_allowed_in_pattern, isLetKeyword)
859+
.fixItRemove(varLoc);
860860
}
861861

862862
// In our recursive parse, remember that we're in a var/let pattern.
@@ -1067,7 +1067,7 @@ ParserResult<Pattern> Parser::parseMatchingPatternAsLetOrVar(bool isLet,
10671067
diagnose(varLoc, diag::let_pattern_in_immutable_context);
10681068

10691069
if (!isLet && InVarOrLetPattern == IVOLP_AlwaysImmutable)
1070-
diagnose(varLoc, diag::var_not_allowed_in_pattern)
1070+
diagnose(varLoc, diag::var_not_allowed_in_pattern, isLet)
10711071
.fixItReplace(varLoc, "let");
10721072

10731073
// In our recursive parse, remember that we're in a var/let pattern.

test/Constraints/patterns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ default: break
167167

168168
// FIXME: rdar://problem/23378003
169169
// These will eventually become errors.
170-
for (var x) in 0...100 {} // expected-error {{Use of 'var' binding here is not allowed}} {{6-9=}}
171-
for var x in 0...100 {} // expected-error {{Use of 'var' binding here is not allowed}} {{5-9=}}
170+
for (var x) in 0...100 {} // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{6-9=}}
171+
for var x in 0...100 {} // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{5-9=}}
172172

173173
for (let x) in 0...100 {} // expected-error {{'let' pattern is already in an immutable context}}
174174

test/Parse/foreach.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ func for_each(r: Range<Int>, iir: IntRange<Int>) {
3535
for i in r sum = sum + i; // expected-error{{expected '{' to start the body of for-each loop}}
3636
for let x in 0..<10 {} // expected-error {{'let' pattern is already in an immutable context}} {{7-11=}}
3737

38-
for var x in 0..<10 {} // expected-error {{Use of 'var' binding here is not allowed}} {{7-11=}}
38+
// FIXME: rdar://problem/23378003
39+
// This will eventually become an error.
40+
for var x in 0..<10 {} // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{7-11=}}
3941
}

test/Parse/matching_patterns.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ case let (let b): // expected-error {{'let' cannot appear nested inside another
3232
print(b)
3333

3434
// 'var' patterns (not allowed)
35-
case var a: // expected-error {{Use of 'var' binding here is not allowed}} {{6-9=let}}
35+
// FIXME: rdar://problem/23378003
36+
// This will eventually be an error.
37+
case var a: // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{6-9=let}}
3638
a += 1
37-
case var let a: // expected-error {{Use of 'var' binding here is not allowed}} {{6-9=let}}
39+
case var let a: // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{6-9=let}}
3840
// expected-error@-1 {{'let' cannot appear nested inside another 'var' or 'let' pattern}}
3941
print(a, terminator: "")
4042

test/Parse/switch.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ case (1, let b): // let bindings
216216
// var bindings are not allowed in cases.
217217
// FIXME: rdar://problem/23378003
218218
// This will eventually be an error.
219-
case (1, var b): // expected-error {{Use of 'var' binding here is not allowed}} {{10-13=let}}
219+
case (1, var b): // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{10-13=let}}
220220
()
221-
case (var a, 2): // expected-error {{Use of 'var' binding here is not allowed}} {{7-10=let}}
221+
case (var a, 2): // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{7-10=let}}
222222
()
223223

224224
case (let a, 2), (1, let b): // expected-error {{'case' labels with multiple patterns cannot declare variables}}

test/Sema/immutability.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ func test_mutability() {
221221

222222

223223
func test_arguments(a : Int,
224-
var b : Int, // expected-error {{Use of 'var' binding here is not allowed}} {{21-25=}}
225-
let c : Int) { // expected-warning {{'let' keyword is unnecessary; function parameters are immutable by default}} {{21-25=}}
224+
var b : Int, // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{21-25=}}
225+
let c : Int) { // expected-warning {{Use of 'let' binding here is deprecated and will be removed in a future version of Swift}} {{21-25=}}
226226
a = 1 // expected-error {{cannot assign to value: 'a' is a 'let' constant}}
227-
var b = 2 // ok.
227+
b = 2 // ok.
228228
c = 3 // expected-error {{cannot assign to value: 'c' is a 'let' constant}}
229229
}
230230

test/SourceKit/SourceDocInfo/cursor_info.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public class SubscriptCursorTest {
146146

147147
// RUN: %sourcekitd-test -req=cursor -pos=28:24 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck -check-prefix=CHECK12 %s
148148
// CHECK12: source.lang.swift.decl.var.local (28:23-28:27)
149-
// CHECK12: <Declaration>let arg1: <Type usr="s:Si">Int</Type></Declaration>
149+
// CHECK12: <Declaration>var arg1: <Type usr="s:Si">Int</Type></Declaration>
150150

151151
// RUN: %sourcekitd-test -req=cursor -pos=31:7 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %mcp_opt %s | FileCheck -check-prefix=CHECK13 %s
152152
// CHECK13: source.lang.swift.decl.function.free (31:6-31:37)

test/decl/func/functions.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ func testObjCMethodCurry(a : ClassWithObjCMethod) -> (Int) -> () {
121121
}
122122

123123
// We used to crash on this.
124-
func rdar16786220(var let c: Int) -> () { // expected-error {{Use of 'var' binding here is not allowed}} {{19-22=}} expected-error {{parameter may not have multiple 'inout', 'var', or 'let' specifiers}}
125-
var c = c
124+
func rdar16786220(var let c: Int) -> () { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{19-22=}} expected-error {{parameter may not have multiple 'inout', 'var', or 'let' specifiers}}
126125
c = 42
127-
_ = c
128126
}
129127

130128

@@ -138,11 +136,9 @@ func !!!<T>(lhs: UnsafePointer<T>, rhs: UnsafePointer<T>) -> Bool { return false
138136

139137
// <rdar://problem/16786168> Functions currently permit 'var inout' parameters
140138
func inout_inout_error(inout inout x : Int) {} // expected-error {{parameter may not have multiple 'inout', 'var', or 'let' specifiers}} {{30-36=}}
141-
// expected-error@+1 {{Use of 'var' binding here is not allowed}} {{22-25=}}
139+
// expected-warning@+1 {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{22-25=}}
142140
func var_inout_error(var inout x : Int) { // expected-error {{parameter may not have multiple 'inout', 'var', or 'let' specifiers}} {{26-32=}}
143-
var x = x
144141
x = 2
145-
_ = x
146142
}
147143

148144
// Unnamed parameters require the name "_":

test/decl/var/usage.swift

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ func basicTests() -> Int {
1212
return y
1313
}
1414

15-
// expected-error@+2 {{Use of 'var' binding here is not allowed}} {{41-45=}}
16-
// expected-error@+1 {{Use of 'var' binding here is not allowed}} {{54-58=}}
15+
// expected-warning@+2 {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{41-45=}}
16+
// expected-warning@+1 {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{54-58=}}
1717
func mutableParameter(a : Int, h : Int, var i : Int, var j: Int,
18-
var g : Int) -> Int { // expected-error {{Use of 'var' binding here is not allowed}} {{8-12=}}
19-
// expected-error@+1 {{left side of mutating operator isn't mutable: 'g' is a 'let' constant}}
18+
var g : Int) -> Int { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{8-12=}}
2019
g += 1
21-
// expected-error@+1 {{cannot pass immutable value as inout argument: 'i' is a 'let' constant}}
2220
swap(&i, &j)
2321
return i+g
2422
}
@@ -102,10 +100,9 @@ func testSubscript() -> [Int] {
102100
}
103101

104102

105-
func testTuple(var x : Int) -> Int { // expected-error {{Use of 'var' binding here is not allowed}} {{16-19=}}
103+
func testTuple(var x : Int) -> Int { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{16-19=}}
106104
var y : Int // Ok, stored by a tuple
107105

108-
// expected-error@+1 {{cannot assign to value: 'x' is a 'let' constant}}
109106
(x, y) = (1,2)
110107
return y
111108
}
@@ -166,9 +163,8 @@ protocol Fooable {
166163
mutating func mutFoo()
167164
func immutFoo()
168165
}
169-
func testOpenExistential(var x: Fooable, // expected-error {{Use of 'var' binding here is not allowed}} {{26-29=}}
166+
func testOpenExistential(var x: Fooable, // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{26-29=}}
170167
y: Fooable) {
171-
// expected-error@+1 {{cannot use mutating member on immutable value}}
172168
x.mutFoo()
173169
y.immutFoo()
174170
}
@@ -177,27 +173,35 @@ func testOpenExistential(var x: Fooable, // expected-error {{Use of 'var' bindin
177173
func couldThrow() throws {}
178174

179175
func testFixitsInStatementsWithPatterns(a : Int?) {
180-
if var b = a, // expected-error {{Use of 'var' binding here is not allowed}} {{6-9=let}}
181-
var b2 = a { // expected-error {{Use of 'var' binding here is not allowed}} {{7-10=let}}
176+
// FIXME: rdar://problem/23378003
177+
// This will eventually be an error.
178+
if var b = a, // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{6-9=let}}
179+
var b2 = a { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{7-10=let}}
182180
b = 1
183181
b2 = 1
184182
_ = b
185183
_ = b2
186184
}
187185

188186
var g = [1,2,3].generate()
189-
while var x = g.next() { // expected-error {{Use of 'var' binding here is not allowed}} {{9-12=let}}
187+
// FIXME: rdar://problem/23378003
188+
// This will eventually be an error.
189+
while var x = g.next() { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{9-12=let}}
190190
x = 0
191191
_ = x
192192
}
193193

194-
guard var y = Optional.Some(1) else { // expected-error {{Use of 'var' binding here is not allowed}} {{9-12=let}}
194+
// FIXME: rdar://problem/23378003
195+
// This will eventually be an error.
196+
guard var y = Optional.Some(1) else { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{9-12=let}}
195197
return
196198
}
197199
y = 0
198200
_ = y
199201

200-
for var b in [42] { // expected-error {{Use of 'var' binding here is not allowed}} {{7-11=}}
202+
// FIXME: rdar://problem/23378003
203+
// This will eventually be an error.
204+
for var b in [42] { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{7-11=}}
201205
b = 42
202206
_ = b
203207
}
@@ -208,14 +212,18 @@ func testFixitsInStatementsWithPatterns(a : Int?) {
208212

209213
do {
210214
try couldThrow()
211-
} catch var err { // expected-error {{Use of 'var' binding here is not allowed}} {{11-14=let}}
215+
// FIXME: rdar://problem/23378003
216+
// This will eventually be an error.
217+
} catch var err { // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{11-14=let}}
212218
// expected-warning@-1 {{variable 'err' was never mutated; consider changing to 'let' constant}}
213219
_ = err
214220
}
215221

216222
switch a {
217-
case var b: // expected-error {{Use of 'var' binding here is not allowed}} {{10-13=let}}
218-
// expected-warning@-1 {{was never mutated; consider changing to 'let' constant}}
223+
// FIXME: rdar://problem/23378003
224+
// This will eventually be an error.
225+
case var b: // expected-warning {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{10-13=let}}
226+
// expected-warning@-1 {{variable 'b' was never mutated; consider changing to 'let' constant}}
219227
_ = b
220228
}
221229
}

test/expr/closure/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ class ExplicitSelfRequiredTest {
158158
}
159159
}
160160

161-
// expected-error@+2 {{Use of 'var' binding here is not allowed}} {{57-60=}}
162-
// expected-warning@+1 {{'let' keyword is unnecessary; function parameters are immutable by default}} {{64-68=}}
161+
// expected-warning@+2 {{Use of 'var' binding here is deprecated and will be removed in a future version of Swift}} {{57-60=}}
162+
// expected-warning@+1 {{Use of 'let' binding here is deprecated and will be removed in a future version of Swift}} {{64-68=}}
163163
var testClosureArgumentPatterns: (Int, Int) -> Int = { (var x, let y) in x+y+1 }
164164

165165
class SomeClass {

0 commit comments

Comments
 (0)