Skip to content

Commit 38e2b70

Browse files
committed
Add tests
1 parent b9c2370 commit 38e2b70

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

test/stmt/if_while_var.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
struct NonOptionalStruct {}
3+
struct NonOptionalStruct { let property: Any }
44
enum NonOptionalEnum { case foo }
55

66
func foo() -> Int? { return .none }
@@ -21,11 +21,30 @@ if var x = foo() {
2121
modify(&x)
2222
}
2323

24+
if let x { // expected-error{{cannot find 'x' in scope}}
25+
use(x)
26+
}
27+
28+
if var x { // expected-error{{cannot find 'x' in scope}}
29+
use(x)
30+
}
31+
2432
use(x) // expected-error{{cannot find 'x' in scope}}
2533

34+
let nonOptional = nonOptionalStruct()
35+
2636
if let x = nonOptionalStruct() { _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
2737
if let x = nonOptionalEnum() { _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
2838

39+
if let nonOptional { _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
40+
if var nonOptional { nonOptional = nonOptionalStruct(); _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
41+
42+
guard let nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
43+
guard var nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
44+
45+
if let nonOptional.property { } // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
46+
if var nonOptional.property { } // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
47+
2948
guard let _ = nonOptionalStruct() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
3049
guard let _ = nonOptionalEnum() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
3150

@@ -59,6 +78,16 @@ if let x = foo() {
5978

6079
var opt: Int? = .none
6180

81+
if let opt {
82+
use(opt)
83+
opt = 10 // expected-error{{cannot assign to value: 'opt' is a 'let' constant}}
84+
}
85+
86+
if var opt {
87+
use(opt)
88+
opt = 10
89+
}
90+
6291
if let x = opt {} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
6392
if var x = opt {} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
6493

@@ -137,13 +166,26 @@ func testShadowing(_ a: Int?, b: Int?, c: Int?, d: Int?) {
137166
}
138167
}
139168

169+
func testShadowingWithShorthand(_ a: Int?, b: Int?, c: Int?, d: Int?) {
170+
guard let a, let b = a > 0 ? b : nil else { return }
171+
_ = b
172+
173+
if let c, let d = c > 0 ? d : nil {
174+
_ = d
175+
}
176+
}
177+
140178
func useInt(_ x: Int) {}
141179

142-
func testWhileScoping(_ a: Int?) {// expected-note {{did you mean 'a'?}}
180+
func testWhileScoping(_ a: Int?) {
143181
while let x = a { }
144182
useInt(x) // expected-error{{cannot find 'x' in scope}}
145183
}
146184

185+
func testWhileShorthand(_ b: Int?) {
186+
while let b { _ = b }
187+
}
188+
147189
// Matching a case with a single, labeled associated value.
148190
public enum SomeParseResult<T> {
149191
case error(length: Int)

test/stmt/statements.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func testMyEnumWithCaseLabels(_ a : MyEnumWithCaseLabels) {
357357
}
358358

359359

360-
func test_guard(_ x : Int, y : Int??, cond : Bool) {
360+
func test_guard(_ x : Int, y : Int??, z: Int?, cond : Bool) {
361361

362362
// These are all ok.
363363
guard let a = y else {}
@@ -366,9 +366,9 @@ func test_guard(_ x : Int, y : Int??, cond : Bool) {
366366
guard case let c = x, cond else {}
367367
guard case let Optional.some(d) = y else {}
368368
guard x != 4, case _ = x else { }
369-
370-
371-
guard let e, cond else {} // expected-error {{variable binding in a condition requires an initializer}}
369+
guard let z, cond else {}
370+
371+
372372
guard case let f? : Int?, cond else {} // expected-error {{variable binding in a condition requires an initializer}}
373373

374374
// FIXME: Bring back the tailored diagnostic

0 commit comments

Comments
 (0)