Skip to content

Commit 4c7f0fd

Browse files
committed
Split out 'defer' tests into their own file
1 parent 5acd5e6 commit 4c7f0fd

File tree

2 files changed

+131
-130
lines changed

2 files changed

+131
-130
lines changed

test/stmt/defer.swift

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
func voidReturn1() {}
4+
func breakContinue(_: Int) -> Int {}
5+
6+
func testDefer(_ a : Int) {
7+
8+
defer { voidReturn1() }
9+
defer { breakContinue(1)+42 } // expected-warning {{result of operator '+' is unused}}
10+
11+
// Ok:
12+
defer { while false { break } }
13+
14+
// Not ok.
15+
while false { defer { break } } // expected-error {{'break' cannot transfer control out of a defer statement}}
16+
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{17-22=do}}
17+
defer { return } // expected-error {{'return' cannot transfer control out of a defer statement}}
18+
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{3-8=do}}
19+
}
20+
21+
class SomeTestClass {
22+
var x = 42
23+
24+
func method() {
25+
defer { x = 97 } // self. not required here!
26+
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{5-10=do}}
27+
}
28+
}
29+
30+
enum DeferThrowError: Error {
31+
case someError
32+
}
33+
34+
func throwInDefer() {
35+
defer { throw DeferThrowError.someError } // expected-error {{errors cannot be thrown out of a defer body}}
36+
print("Foo")
37+
}
38+
39+
func throwInDeferOK1() {
40+
defer {
41+
do {
42+
throw DeferThrowError.someError
43+
} catch {}
44+
}
45+
print("Bar")
46+
}
47+
48+
func throwInDeferOK2() throws {
49+
defer {
50+
do {
51+
throw DeferThrowError.someError
52+
} catch {}
53+
}
54+
print("Bar")
55+
}
56+
57+
func throwingFuncInDefer1() throws {
58+
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
59+
print("Bar")
60+
}
61+
62+
func throwingFuncInDefer1a() throws {
63+
defer {
64+
do {
65+
try throwingFunctionCalledInDefer()
66+
} catch {}
67+
}
68+
print("Bar")
69+
}
70+
71+
func throwingFuncInDefer2() throws {
72+
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
73+
print("Bar")
74+
}
75+
76+
func throwingFuncInDefer2a() throws {
77+
defer {
78+
do {
79+
throwingFunctionCalledInDefer()
80+
// expected-error@-1 {{call can throw but is not marked with 'try'}}
81+
// expected-note@-2 {{did you mean to use 'try'?}}
82+
// expected-note@-3 {{did you mean to handle error as optional value?}}
83+
// expected-note@-4 {{did you mean to disable error propagation?}}
84+
} catch {}
85+
}
86+
print("Bar")
87+
}
88+
89+
func throwingFuncInDefer3() {
90+
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
91+
print("Bar")
92+
}
93+
94+
func throwingFuncInDefer3a() {
95+
defer {
96+
do {
97+
try throwingFunctionCalledInDefer()
98+
} catch {}
99+
}
100+
print("Bar")
101+
}
102+
103+
func throwingFuncInDefer4() {
104+
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
105+
print("Bar")
106+
}
107+
108+
func throwingFuncInDefer4a() {
109+
defer {
110+
do {
111+
throwingFunctionCalledInDefer()
112+
// expected-error@-1 {{call can throw but is not marked with 'try'}}
113+
// expected-note@-2 {{did you mean to use 'try'?}}
114+
// expected-note@-3 {{did you mean to handle error as optional value?}}
115+
// expected-note@-4 {{did you mean to disable error propagation?}}
116+
} catch {}
117+
}
118+
print("Bar")
119+
}
120+
121+
func throwingFunctionCalledInDefer() throws {
122+
throw DeferThrowError.someError
123+
}
124+
125+
class SomeDerivedClass: SomeTestClass {
126+
override init() {
127+
defer {
128+
super.init() // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}}
129+
}
130+
}
131+
}

test/stmt/statements.swift

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

359359

360-
361-
// "defer"
362-
363-
func test_defer(_ a : Int) {
364-
365-
defer { VoidReturn1() }
366-
defer { breakContinue(1)+42 } // expected-warning {{result of operator '+' is unused}}
367-
368-
// Ok:
369-
defer { while false { break } }
370-
371-
// Not ok.
372-
while false { defer { break } } // expected-error {{'break' cannot transfer control out of a defer statement}}
373-
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{17-22=do}}
374-
defer { return } // expected-error {{'return' cannot transfer control out of a defer statement}}
375-
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{3-8=do}}
376-
}
377-
378-
class SomeTestClass {
379-
var x = 42
380-
381-
func method() {
382-
defer { x = 97 } // self. not required here!
383-
// expected-warning@-1 {{'defer' statement at end of scope always executes immediately}}{{5-10=do}}
384-
}
385-
}
386-
387-
enum DeferThrowError: Error {
388-
case someError
389-
}
390-
391-
func throwInDefer() {
392-
defer { throw DeferThrowError.someError } // expected-error {{errors cannot be thrown out of a defer body}}
393-
print("Foo")
394-
}
395-
396-
func throwInDeferOK1() {
397-
defer {
398-
do {
399-
throw DeferThrowError.someError
400-
} catch {}
401-
}
402-
print("Bar")
403-
}
404-
405-
func throwInDeferOK2() throws {
406-
defer {
407-
do {
408-
throw DeferThrowError.someError
409-
} catch {}
410-
}
411-
print("Bar")
412-
}
413-
414-
func throwingFuncInDefer1() throws {
415-
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
416-
print("Bar")
417-
}
418-
419-
func throwingFuncInDefer1a() throws {
420-
defer {
421-
do {
422-
try throwingFunctionCalledInDefer()
423-
} catch {}
424-
}
425-
print("Bar")
426-
}
427-
428-
func throwingFuncInDefer2() throws {
429-
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
430-
print("Bar")
431-
}
432-
433-
func throwingFuncInDefer2a() throws {
434-
defer {
435-
do {
436-
throwingFunctionCalledInDefer()
437-
// expected-error@-1 {{call can throw but is not marked with 'try'}}
438-
// expected-note@-2 {{did you mean to use 'try'?}}
439-
// expected-note@-3 {{did you mean to handle error as optional value?}}
440-
// expected-note@-4 {{did you mean to disable error propagation?}}
441-
} catch {}
442-
}
443-
print("Bar")
444-
}
445-
446-
func throwingFuncInDefer3() {
447-
defer { try throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
448-
print("Bar")
449-
}
450-
451-
func throwingFuncInDefer3a() {
452-
defer {
453-
do {
454-
try throwingFunctionCalledInDefer()
455-
} catch {}
456-
}
457-
print("Bar")
458-
}
459-
460-
func throwingFuncInDefer4() {
461-
defer { throwingFunctionCalledInDefer() } // expected-error {{errors cannot be thrown out of a defer body}}
462-
print("Bar")
463-
}
464-
465-
func throwingFuncInDefer4a() {
466-
defer {
467-
do {
468-
throwingFunctionCalledInDefer()
469-
// expected-error@-1 {{call can throw but is not marked with 'try'}}
470-
// expected-note@-2 {{did you mean to use 'try'?}}
471-
// expected-note@-3 {{did you mean to handle error as optional value?}}
472-
// expected-note@-4 {{did you mean to disable error propagation?}}
473-
} catch {}
474-
}
475-
print("Bar")
476-
}
477-
478-
func throwingFunctionCalledInDefer() throws {
479-
throw DeferThrowError.someError
480-
}
481-
482-
class SomeDerivedClass: SomeTestClass {
483-
override init() {
484-
defer {
485-
super.init() // expected-error {{initializer chaining ('super.init') cannot be nested in another expression}}
486-
}
487-
}
488-
}
489-
490360
func test_guard(_ x : Int, y : Int??, cond : Bool) {
491361

492362
// These are all ok.

0 commit comments

Comments
 (0)