1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// RUN: %target-typecheck-verify-swift
struct NC: ~Copyable {}
func testBorrowing(_ v: borrowing NC?) {}
func testConsuming(_ v: consuming NC?) {}
func testInout(_ v: inout NC?) {}
class MethodSir {
func borrow(_ v: borrowing NC?) {}
func consume(_ v: consuming NC?) {}
}
func testExplicitCasts() {
let nc = NC()
_ = nc as NC?
}
func testCalls() {
let method = MethodSir()
let foo = NC()
testBorrowing(foo) // expected-error {{implicit conversion to 'NC?' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
testBorrowing(consume foo)
testBorrowing(foo as NC?)
method.borrow(foo) // expected-error {{implicit conversion to 'NC?' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
method.borrow(consume foo)
testConsuming(foo)
testConsuming(consume foo)
var optNC: NC? = NC() // ConstraintLocator::ContextualType
testInout(&optNC)
}
func testReturn() -> NC? {
let foo = NC()
return foo // ConstraintLocator::ContextualType
}
func higherOrder(_ f: () -> NC?) -> NC? {
if let nc = f() {
nc // ConstraintLocator::ContextualType
} else {
nil
}
}
func callHigherOrder() {
let nc = NC()
let _ = higherOrder { nc } // ConstraintLocator::ClosureBody
let _ = higherOrder { return nc } // ConstraintLocator::ClosureBody
}
func delay(_ f: @autoclosure () -> NC?) -> NC? { f() }
func testDelay() {
let nc = NC()
let _ = delay(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
}
struct PropCity {
var harmless1: NC? { NC() }
var harmless2: NC? {
get { return NC() }
}
subscript(_ i: Int) -> NC? { return NC() }
func chk(_ t: borrowing NC!) {}
func chkWithDefaultArg(_ oath: borrowing NC? = NC()) {}
func test(_ nc: consuming NC) {
chk(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{9-9=consume }}
chk(consume nc)
chkWithDefaultArg()
chkWithDefaultArg(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{23-23=consume }}
}
}
protocol Veggie: ~Copyable {}
struct Carrot: ~Copyable, Veggie {}
func restockBorrow(_ x: borrowing any Veggie & ~Copyable) {}
func restockConsume(_ x: consuming any Veggie & ~Copyable) {}
func checkExistential() {
let carrot = Carrot()
restockBorrow(carrot) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
restockBorrow(consume carrot)
restockConsume(carrot)
}
func genericErasure<T: Veggie & ~Copyable>(_ veg: consuming T) {
restockBorrow(veg) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
restockBorrow(consume veg)
restockBorrow(veg as any Veggie & ~Copyable)
restockConsume(veg)
let _ = veg as any Veggie & ~Copyable
}
extension Veggie where Self: ~Copyable {
func inspect(_ b: borrowing any Veggie & ~Copyable) {}
}
extension Carrot {
consuming func check() {
inspect(self) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{13-13=consume }}
inspect(consume self)
inspect(self as any Veggie & ~Copyable)
let _: any Veggie & ~Copyable = self
}
}
|