Files
swift-mirror/test/Sema/consume_operator_noop_warning.swift
Kavon Farvardin e04b35d709 Consume: warn about no-op consumes to be fixed
As of now, SE-366 is not correctly implemented with respect to concrete,
bitwise-copyable types like `Int`. Writing `consume someInt` doesn't
actually consume the variable binding as it should, meaning code that
should be flagged as having a use-after-consume is being silently
permitted:

```swift
let someInt = 10
let y = consume someInt
print(someInt) // no error!
```

This has been a problem since Swift 5.9. Eventually we plan to fix this
issue, which means code previously doing the above would become an
error. To help people get ready for the fix, start warning people that
these consumes are actually no-ops and suggest removing them until the
intended behavior is actually enforced in the future.

resolves rdar://127081103
2024-07-24 16:05:47 -07:00

61 lines
3.2 KiB
Swift

// RUN: %target-swift-emit-sil %s -verify -sil-verify-all
struct Point {
let x: Float
let y: Float
}
struct ConditionallyBC<T> {
var t: T
}
extension ConditionallyBC: BitwiseCopyable where T: BitwiseCopyable {}
func test<T, BCG: BitwiseCopyable>(_ t: T, // expected-error {{'t' is borrowed and cannot be consumed}}
_ bcg: BCG, // expected-error {{'bcg' is borrowed and cannot be consumed}}
_ cbcg_generic: ConditionallyBC<BCG>, // expected-error {{'cbcg_generic' is borrowed and cannot be consumed}}
_ maybeBCG: BCG?, // expected-error {{'maybeBCG' is borrowed and cannot be consumed}}
_ maybeT: T?, // expected-error {{'maybeT' is borrowed and cannot be consumed}}
_ anyBC: any BitwiseCopyable, // expected-error {{'anyBC' is borrowed and cannot be consumed}}
_ x: Int,
_ point: Point,
_ cbcg_concrete: ConditionallyBC<Int>,
_ maybeFloat: Float?) {
_ = consume t // expected-note {{consumed here}}
_ = consume bcg // expected-note {{consumed here}}
_ = consume cbcg_generic // expected-note {{consumed here}}
_ = consume maybeBCG // expected-note {{consumed here}}
_ = consume maybeT // expected-note {{consumed here}}
_ = consume anyBC // expected-note {{consumed here}}
_ = consume x // expected-warning {{'consume' applied to bitwise-copyable type 'Int' has no effect}}{{7-15=}}
_ = consume point // expected-warning {{'consume' applied to bitwise-copyable type 'Point' has no effect}}{{7-15=}}
_ = consume cbcg_concrete // expected-warning {{'consume' applied to bitwise-copyable type 'ConditionallyBC<Int>' has no effect}}{{7-16=}}
_ = consume maybeFloat // expected-warning {{'consume' applied to bitwise-copyable type 'Float?' has no effect}}{{8-16=}}
}
func proofOfUseAfterConsume() -> Int {
let someInt = 10
let y = consume someInt // expected-warning {{'consume' applied to bitwise-copyable type 'Int' has no effect}}
print(y)
return someInt // undiagnosed use-after-consume
}
func moreProofs(_ share: __shared Int,
_ own: __owned Int,
_ snd: sending Int, // expected-error {{'snd' used after consume}}
_ ino: inout Int, // expected-error {{'ino' used after consume}}
_ brw: borrowing Int, // expected-error {{'brw' is borrowed and cannot be consumed}}
_ csm: consuming Int // expected-error {{'csm' consumed more than once}}
) {
_ = consume share // expected-warning {{'consume' applied to bitwise-copyable type 'Int' has no effect}}
_ = consume own // expected-warning {{'consume' applied to bitwise-copyable type 'Int' has no effect}}
let _ = (share, own)
_ = consume ino // expected-note {{consumed}}
_ = consume brw // expected-note {{consumed}}
_ = consume csm // expected-note {{consumed}}
_ = consume csm // expected-note {{consumed}}
_ = consume snd // expected-note {{consumed}}
_ = snd // expected-note {{used}}
} // expected-note {{used here}}