1- // RUN: %target-swift-frontend %s -emit-sil -verify -enable-experimental-feature ManualOwnership
1+ // RUN: %target-swift-frontend %s -emit-sil -verify \
2+ // RUN: -enable-experimental-feature ManualOwnership \
3+ // RUN: -enable-copy-propagation=always
24
35// REQUIRES: swift_feature_ManualOwnership
46
@@ -12,6 +14,7 @@ public struct Pair {
1214 var x : Int
1315 var y : Int
1416
17+ @_manualOwnership
1518 consuming func midpoint( _ other: borrowing Pair ) -> Pair {
1619 return Pair ( x: ( x + other. x) / 2 , y: ( y + other. y) / 2 )
1720 }
@@ -24,7 +27,10 @@ public class Triangle {
2427
2528 var nontrivial = Whatever ( )
2629
30+ @_manualOwnership
2731 consuming func consuming( ) { }
32+
33+ @_manualOwnership
2834 borrowing func borrowing( ) { }
2935}
3036
@@ -33,14 +39,8 @@ public class Triangle {
3339@_manualOwnership
3440public func basic_return1( ) -> Triangle {
3541 let x = Triangle ( )
36- return x // expected-error {{explicit 'copy' required here}}
42+ return x
3743}
38- @_manualOwnership
39- public func basic_return1_fixed( ) -> Triangle {
40- let x = Triangle ( )
41- return copy x
42- }
43-
4444
4545@_manualOwnership
4646public func basic_return2( t: Triangle ) -> Triangle {
@@ -56,24 +56,39 @@ public func basic_return3() -> Triangle {
5656 return Triangle ( )
5757}
5858
59- // FIXME: we need copy propagation in -Onone to eliminate all these copies
6059@_manualOwnership
6160func reassign_with_lets( ) -> Triangle {
6261 let x = Triangle ( )
63- let y = x // expected-error {{explicit 'copy' required here}}
64- let z = y // expected-error {{explicit 'copy' required here}}
65- return z // expected-error {{explicit 'copy' required here}}
62+ let y = x
63+ let z = y
64+ return z
6665}
6766
68- // FIXME: we need copy propagation in -Onone to eliminate all but the copies for returning
6967@_manualOwnership
7068func renamed_return( _ cond: Bool , _ a: Triangle ) -> Triangle {
71- let b = a // expected-error {{explicit 'copy' required here}}
72- let c = b // expected-error {{explicit 'copy' required here}}
69+ let b = a
70+ let c = b
7371 if cond { return b } // expected-error {{explicit 'copy' required here}}
7472 return c // expected-error {{explicit 'copy' required here}}
7573}
7674
75+ @_manualOwnership
76+ func renamed_return_fix1( _ cond: Bool , _ a: Triangle ) -> Triangle {
77+ let b = copy a
78+ let c = copy b // FIXME: not needed! Is explicit_copy_value is blocking propagation?
79+ if cond { return b }
80+ return c
81+ }
82+
83+ // FIXME: this crashes CopyPropagation!
84+ //@_manualOwnership
85+ //func renamed_return_fix2(_ cond: Bool, _ a: Triangle) -> Triangle {
86+ // let b = a
87+ // let c = b
88+ // if cond { return copy b }
89+ // return copy c
90+ //}
91+
7792/// MARK: method calls
7893
7994@_manualOwnership
@@ -87,7 +102,7 @@ func basic_methods_borrowing(_ t1: Triangle) {
87102func basic_methods_consuming( _ t1: Triangle ) {
88103 let t2 = Triangle ( )
89104 t1. consuming ( ) // expected-error {{explicit 'copy' required here}}
90- t2. consuming ( ) // expected-error {{explicit 'copy' required here}}
105+ t2. consuming ( )
91106}
92107@_manualOwnership
93108func basic_methods_consuming_fixed( _ t1: Triangle ) {
@@ -96,7 +111,7 @@ func basic_methods_consuming_fixed(_ t1: Triangle) {
96111 t3 = Triangle ( )
97112
98113 ( copy t1 ) . consuming ( )
99- ( copy t2 ) . consuming ( )
114+ ( copy t2 ) . consuming ( ) // FIXME: why is this not propagated?
100115 ( copy t3 ) . consuming ( )
101116}
102117
@@ -113,12 +128,12 @@ func basic_function_call(_ t1: Triangle) {
113128/// MARK: control-flow
114129
115130
116- // FIXME: var's and assignments are a little busted
131+ // FIXME: var assignments are somtimes impossible to satisfy with 'copy'
117132
118133// @_manualOwnership
119134// func reassignments_1() {
120135// var t3 = Triangle()
121- // t3 = Triangle()
136+ // t3 = copy Triangle() // FIXME: should not be needed
122137// t3.borrowing()
123138// }
124139// @_manualOwnership
0 commit comments