Skip to content

Commit 0696e31

Browse files
committed
[move-function] Add casting tests for all 4 cases of (loadable, addressonly) x (let, var)
1 parent 9370929 commit 0696e31

4 files changed

+404
-4
lines changed

test/SILOptimizer/move_function_kills_copyable_addressonly_lets.swift

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
import Swift
66

7-
public class Klass {}
8-
97
//////////////////
108
// Declarations //
119
//////////////////
1210

11+
public class Klass {}
12+
13+
public protocol P {}
14+
public protocol SubP1 : P {}
15+
public protocol SubP2 : P {}
16+
1317
func consumingUse<T>(_ k: __owned T) {}
1418
var booleanValue: Bool { false }
1519
func nonConsumingUse<T>(_ k: T) {}
@@ -284,3 +288,91 @@ public func multipleVarsWithSubsequentBorrows<T : Equatable>(_ p: T) -> Bool {
284288
let k3 = _move(k)
285289
return k2 == k3
286290
}
291+
292+
////////////////
293+
// Cast Tests //
294+
////////////////
295+
296+
public func castTest0<T : SubP1>(_ x: __owned T) -> P { // expected-error {{'x' used after being moved}}
297+
let _ = _move(x) // expected-note {{move here}}
298+
return x as P // expected-note {{use here}}
299+
}
300+
301+
public func castTest1<T : P>(_ x: __owned T) -> SubP2 { // expected-error {{'x' used after being moved}}
302+
let _ = _move(x) // expected-note {{move here}}
303+
return x as! SubP2 // expected-note {{use here}}
304+
}
305+
306+
public func castTest2<T : P>(_ x: __owned T) -> SubP1? { // expected-error {{'x' used after being moved}}
307+
let _ = _move(x) // expected-note {{move here}}
308+
return x as? SubP1 // expected-note {{use here}}
309+
}
310+
311+
public func castTestSwitch1<T : P>(_ x: __owned T) { // expected-error {{'x' used after being moved}}
312+
let _ = _move(x) // expected-note {{move here}}
313+
switch x { // expected-note {{use here}}
314+
case let k as SubP1:
315+
print(k)
316+
default:
317+
print("Nope")
318+
}
319+
}
320+
321+
public func castTestSwitch2<T : P>(_ x: __owned T) { // expected-error {{'x' used after being moved}}
322+
let _ = _move(x) // expected-note {{move here}}
323+
switch x { // expected-note {{use here}}
324+
case let k as SubP1:
325+
print(k)
326+
case let k as SubP2:
327+
print(k)
328+
default:
329+
print("Nope")
330+
}
331+
}
332+
333+
public func castTestSwitchInLoop<T : P>(_ x: __owned T) { // expected-error {{'x' used after being moved}}
334+
let _ = _move(x) // expected-note {{move here}}
335+
336+
for _ in 0..<1024 {
337+
switch x { // expected-note {{use here}}
338+
case let k as SubP1:
339+
print(k)
340+
default:
341+
print("Nope")
342+
}
343+
}
344+
}
345+
346+
public func castTestIfLet<T : P>(_ x: __owned T) { // expected-error {{'x' used after being moved}}
347+
let _ = _move(x) // expected-note {{move here}}
348+
if case let k as SubP1 = x { // expected-note {{use here}}
349+
print(k)
350+
} else {
351+
print("no")
352+
}
353+
}
354+
355+
public func castTestIfLetInLoop<T : P>(_ x: __owned T) { // expected-error {{'x' used after being moved}}
356+
let _ = _move(x) // expected-note {{move here}}
357+
for _ in 0..<1024 {
358+
if case let k as SubP1 = x { // expected-note {{use here}}
359+
print(k)
360+
} else {
361+
print("no")
362+
}
363+
}
364+
}
365+
366+
public enum EnumWithKlass {
367+
case none
368+
case klass(P)
369+
}
370+
371+
public func castTestIfLet2(_ x : __owned EnumWithKlass) { // expected-error {{'x' used after being moved}}
372+
let _ = _move(x) // expected-note {{move here}}
373+
if case let .klass(k as SubP1) = x { // expected-note {{use here}}
374+
print(k)
375+
} else {
376+
print("no")
377+
}
378+
}

test/SILOptimizer/move_function_kills_copyable_addressonly_vars.swift

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ var booleanValue: Bool { false }
1919
func nonConsumingUse<T>(_ k: T) {}
2020
func exchangeUse<T>(_ k: __owned T) -> T { k }
2121

22+
public protocol P {}
23+
public protocol SubP1 : P {}
24+
public protocol SubP2 : P {}
25+
2226
///////////
2327
// Tests //
2428
///////////
@@ -206,7 +210,7 @@ struct S<T> {
206210
// Defer Tests //
207211
/////////////////
208212

209-
protocol P {
213+
protocol DeferTestProtocol {
210214
var k: Klass { get }
211215

212216
static func getP() -> Self
@@ -222,7 +226,7 @@ protocol P {
222226
mutating func deferTestFail7()
223227
}
224228

225-
extension P {
229+
extension DeferTestProtocol {
226230
mutating func deferTestSuccess1() {
227231
let selfType = type(of: self)
228232
let _ = _move(self)
@@ -343,3 +347,109 @@ extension P {
343347
print("123")
344348
}
345349
}
350+
351+
////////////////
352+
// Cast Tests //
353+
////////////////
354+
355+
public func castTest0<T : SubP1>(_ x: __owned T) -> P {
356+
var x2 = x // expected-error {{'x2' used after being moved}}
357+
x2 = x
358+
let _ = _move(x2) // expected-note {{move here}}
359+
return x2 as P // expected-note {{use here}}
360+
}
361+
362+
public func castTest1<T : P>(_ x: __owned T) -> SubP1 {
363+
var x2 = x // expected-error {{'x2' used after being moved}}
364+
x2 = x
365+
let _ = _move(x2) // expected-note {{move here}}
366+
return x2 as! SubP1 // expected-note {{use here}}
367+
}
368+
369+
public func castTest2<T : P>(_ x: __owned T) -> SubP1? {
370+
var x2 = x // expected-error {{'x2' used after being moved}}
371+
x2 = x
372+
let _ = _move(x2) // expected-note {{move here}}
373+
return x2 as? SubP1 // expected-note {{use here}}
374+
}
375+
376+
public func castTestSwitch1<T : P>(_ x : __owned T) {
377+
var x2 = x // expected-error {{'x2' used after being moved}}
378+
x2 = x
379+
let _ = _move(x2) // expected-note {{move here}}
380+
switch x2 { // expected-note {{use here}}
381+
case let k as SubP1:
382+
print(k)
383+
default:
384+
print("Nope")
385+
}
386+
}
387+
388+
public func castTestSwitch2<T : P>(_ x : __owned T) {
389+
var x2 = x // expected-error {{'x2' used after being moved}}
390+
x2 = x
391+
let _ = _move(x2) // expected-note {{move here}}
392+
switch x2 { // expected-note {{use here}}
393+
case let k as SubP1:
394+
print(k)
395+
case let k as SubP2:
396+
print(k)
397+
default:
398+
print("Nope")
399+
}
400+
}
401+
402+
public func castTestSwitchInLoop<T : P>(_ x : __owned T) {
403+
var x2 = x // expected-error {{'x2' used after being moved}}
404+
x2 = x
405+
let _ = _move(x2) // expected-note {{move here}}
406+
407+
for _ in 0..<1024 {
408+
switch x2 { // expected-note {{use here}}
409+
case let k as SubP1:
410+
print(k)
411+
default:
412+
print("Nope")
413+
}
414+
}
415+
}
416+
417+
public func castTestIfLet<T : P>(_ x : __owned T) {
418+
var x2 = x // expected-error {{'x2' used after being moved}}
419+
x2 = x
420+
let _ = _move(x2) // expected-note {{move here}}
421+
if case let k as SubP1 = x2 { // expected-note {{use here}}
422+
print(k)
423+
} else {
424+
print("no")
425+
}
426+
}
427+
428+
public func castTestIfLetInLoop<T : P>(_ x : __owned T) {
429+
var x2 = x // expected-error {{'x2' used after being moved}}
430+
x2 = x
431+
let _ = _move(x2) // expected-note {{move here}}
432+
for _ in 0..<1024 {
433+
if case let k as SubP1 = x2 { // expected-note {{use here}}
434+
print(k)
435+
} else {
436+
print("no")
437+
}
438+
}
439+
}
440+
441+
public enum EnumWithP<T> {
442+
case none
443+
case klass(T)
444+
}
445+
446+
public func castTestIfLet2<T : P>(_ x : __owned EnumWithP<T>) {
447+
var x2 = x // expected-error {{'x2' used after being moved}}
448+
x2 = x
449+
let _ = _move(x2) // expected-note {{move here}}
450+
if case let .klass(k as SubP1) = x2 { // expected-note {{use here}}
451+
print(k)
452+
} else {
453+
print("no")
454+
}
455+
}

test/SILOptimizer/move_function_kills_copyable_loadable_vars.swift

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Swift
99
//////////////////
1010

1111
public class Klass {}
12+
public class SubKlass1 : Klass {}
13+
public class SubKlass2 : Klass {}
1214

1315
struct KlassWrapper {
1416
var k: Klass
@@ -390,3 +392,109 @@ extension KlassWrapper {
390392
print("123")
391393
}
392394
}
395+
396+
////////////////
397+
// Cast Tests //
398+
////////////////
399+
400+
public func castTest0(_ x: __owned SubKlass1) -> Klass {
401+
var x2 = x // expected-error {{'x2' used after being moved}}
402+
x2 = x
403+
let _ = _move(x2) // expected-note {{move here}}
404+
return x2 as Klass // expected-note {{use here}}
405+
}
406+
407+
public func castTest1(_ x: __owned Klass) -> SubKlass1 {
408+
var x2 = x // expected-error {{'x2' used after being moved}}
409+
x2 = x
410+
let _ = _move(x2) // expected-note {{move here}}
411+
return x2 as! SubKlass1 // expected-note {{use here}}
412+
}
413+
414+
public func castTest2(_ x: __owned Klass) -> SubKlass1? {
415+
var x2 = x // expected-error {{'x2' used after being moved}}
416+
x2 = x
417+
let _ = _move(x2) // expected-note {{move here}}
418+
return x2 as? SubKlass1 // expected-note {{use here}}
419+
}
420+
421+
public func castTestSwitch1(_ x : __owned Klass) {
422+
var x2 = x // expected-error {{'x2' used after being moved}}
423+
x2 = x
424+
let _ = _move(x2) // expected-note {{move here}}
425+
switch x2 { // expected-note {{use here}}
426+
case let k as SubKlass1:
427+
print(k)
428+
default:
429+
print("Nope")
430+
}
431+
}
432+
433+
public func castTestSwitch2(_ x : __owned Klass) {
434+
var x2 = x // expected-error {{'x2' used after being moved}}
435+
x2 = x
436+
let _ = _move(x2) // expected-note {{move here}}
437+
switch x2 { // expected-note {{use here}}
438+
case let k as SubKlass1:
439+
print(k)
440+
case let k as SubKlass2:
441+
print(k)
442+
default:
443+
print("Nope")
444+
}
445+
}
446+
447+
public func castTestSwitchInLoop(_ x : __owned Klass) {
448+
var x2 = x // expected-error {{'x2' used after being moved}}
449+
x2 = x
450+
let _ = _move(x2) // expected-note {{move here}}
451+
452+
for _ in 0..<1024 {
453+
switch x2 { // expected-note {{use here}}
454+
case let k as SubKlass1:
455+
print(k)
456+
default:
457+
print("Nope")
458+
}
459+
}
460+
}
461+
462+
public func castTestIfLet(_ x : __owned Klass) {
463+
var x2 = x // expected-error {{'x2' used after being moved}}
464+
x2 = x
465+
let _ = _move(x2) // expected-note {{move here}}
466+
if case let k as SubKlass1 = x2 { // expected-note {{use here}}
467+
print(k)
468+
} else {
469+
print("no")
470+
}
471+
}
472+
473+
public func castTestIfLetInLoop(_ x : __owned Klass) {
474+
var x2 = x // expected-error {{'x2' used after being moved}}
475+
x2 = x
476+
let _ = _move(x2) // expected-note {{move here}}
477+
for _ in 0..<1024 {
478+
if case let k as SubKlass1 = x2 { // expected-note {{use here}}
479+
print(k)
480+
} else {
481+
print("no")
482+
}
483+
}
484+
}
485+
486+
public enum EnumWithKlass {
487+
case none
488+
case klass(Klass)
489+
}
490+
491+
public func castTestIfLet2(_ x : __owned EnumWithKlass) {
492+
var x2 = x // expected-error {{'x2' used after being moved}}
493+
x2 = x
494+
let _ = _move(x2) // expected-note {{move here}}
495+
if case let .klass(k as SubKlass1) = x2 { // expected-note {{use here}}
496+
print(k)
497+
} else {
498+
print("no")
499+
}
500+
}

0 commit comments

Comments
 (0)