Skip to content

Commit b169bed

Browse files
committed
Add more tests around geps/partial apply capture of vars
1 parent a734a24 commit b169bed

File tree

2 files changed

+77
-13
lines changed

2 files changed

+77
-13
lines changed

test/SILOptimizer/move_function_kills_copyable_addressonly_vars.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import Swift
88
// Declarations //
99
//////////////////
1010

11-
public class Klass {}
11+
public class Klass {
12+
public func getOtherKlass() -> Klass? { return nil }
13+
}
1214

1315
struct KlassWrapper {
1416
var k: Klass
@@ -19,7 +21,9 @@ var booleanValue: Bool { false }
1921
func nonConsumingUse<T>(_ k: T) {}
2022
func exchangeUse<T>(_ k: __owned T) -> T { k }
2123

22-
public protocol P {}
24+
public protocol P {
25+
var k: Klass { get }
26+
}
2327
public protocol SubP1 : P {}
2428
public protocol SubP2 : P {}
2529

@@ -214,16 +218,6 @@ protocol DeferTestProtocol {
214218
var k: Klass { get }
215219

216220
static func getP() -> Self
217-
mutating func deferTestSuccess1()
218-
mutating func deferTestSuccess2()
219-
mutating func deferTestSuccess3()
220-
mutating func deferTestFail1()
221-
mutating func deferTestFail2()
222-
mutating func deferTestFail3()
223-
mutating func deferTestFail4()
224-
mutating func deferTestFail5()
225-
mutating func deferTestFail6()
226-
mutating func deferTestFail7()
227221
}
228222

229223
extension DeferTestProtocol {
@@ -526,3 +520,36 @@ public func castTestIfLet2<T : P>(_ x : __owned EnumWithP<T>) {
526520
print("no")
527521
}
528522
}
523+
524+
///////////////
525+
// GEP Tests //
526+
///////////////
527+
528+
public func castAccess<T : P>(_ x : __owned T) {
529+
var x2 = x // expected-error {{'x2' used after being moved}}
530+
x2 = x
531+
let _ = _move(x2) // expected-note {{move here}}
532+
let _ = x2.k // expected-note {{use here}}
533+
}
534+
535+
public func castAccess2<T : P>(_ x : __owned T) {
536+
var x2 = x // expected-error {{'x2' used after being moved}}
537+
x2 = x
538+
let _ = _move(x2) // expected-note {{move here}}
539+
let _ = x2.k.getOtherKlass() // expected-note {{use here}}
540+
}
541+
542+
/////////////////////////
543+
// Partial Apply Tests //
544+
/////////////////////////
545+
546+
// This makes sure we always fail if we are asked to check in a partial apply.
547+
public func partialApplyTest<T : P>(_ x: __owned T) {
548+
var x2 = x
549+
x2 = x
550+
let _ = _move(x2) // expected-error {{_move applied to value that the compiler does not support checking}}
551+
let f = {
552+
print(x2)
553+
}
554+
f()
555+
}

test/SILOptimizer/move_function_kills_copyable_loadable_vars.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import Swift
88
// Declarations //
99
//////////////////
1010

11-
public class Klass {}
11+
public class Klass {
12+
var k: Klass? = nil
13+
14+
func getOtherKlass() -> Klass? { nil }
15+
}
1216
public class SubKlass1 : Klass {}
1317
public class SubKlass2 : Klass {}
1418

@@ -566,3 +570,36 @@ public func castTestIfLet2(_ x : __owned EnumWithKlass) {
566570
print("no")
567571
}
568572
}
573+
574+
///////////////
575+
// GEP Tests //
576+
///////////////
577+
578+
public func castAccess(_ x : __owned Klass) {
579+
var x2 = x // expected-error {{'x2' used after being moved}}
580+
x2 = x
581+
let _ = _move(x2) // expected-note {{move here}}
582+
let _ = x2.k // expected-note {{use here}}
583+
}
584+
585+
public func castAccess2(_ x : __owned Klass) {
586+
var x2 = x // expected-error {{'x2' used after being moved}}
587+
x2 = x
588+
let _ = _move(x2) // expected-note {{move here}}
589+
let _ = x2.k!.getOtherKlass() // expected-note {{use here}}
590+
}
591+
592+
/////////////////////////
593+
// Partial Apply Tests //
594+
/////////////////////////
595+
596+
// Emit a better error here. At least we properly error.
597+
public func partialApplyTest(_ x: __owned Klass) {
598+
var x2 = x // expected-error {{'x2' used after being moved}}
599+
x2 = x
600+
let _ = _move(x2) // expected-note {{move here}}
601+
let f = { // expected-note {{use here}}
602+
print(x2)
603+
}
604+
f()
605+
}

0 commit comments

Comments
 (0)