Files
swift-mirror/test/Interpreter/apply_partial_apply_consuming_elim.swift
Michael Gottesman e5aaa68b52 [sil] Do not eliminate (apply (partial_apply)) if the partial_apply captures an @in parameter and handle @in_guaranteed appropriately.
Otherwise the code as written miscompiles code like:

```
@inline(never)
func consumeSelf<T>(_ t : __owned T) {
  print("Consuming self!")
  print(t)
}

class Klass {}
struct S<T> {
  let t: T? = (Klass() as! T)

  @inline(__always)
  __consuming func foo(_ t: T) {
    consumeSelf(self)
  }
}

public func test<T>(_ t: __owned T) {
  let k = S<T>()
  let f = k.foo

  for _ in 0..<1024 {
    f(t)
  }
}

test(Klass())
```

As one can tell, without annotations it is hard to create an example like the
above, but there is no reason why we couldn't emit more code like this from the
frontend.

If the parameter is guaranteed though, the current impl is fine for
@in_guaranteed since in the loop, we do not need to retain or release the
value.

rdar://58885352
2020-02-04 14:20:58 -08:00

31 lines
433 B
Swift

// RUN: %target-run-simple-swift(-Osize)
// REQUIRES: executable_test
@inline(never)
func consumeSelf<T>(_ t : __owned T) {
print("Consuming self!")
print(t)
}
class Klass {}
struct S<T> {
let t: T? = (Klass() as! T)
@inline(__always)
__consuming func foo(_ t: T) {
consumeSelf(self)
}
}
public func test<T>(_ t: __owned T) {
let k = S<T>()
let f = k.foo
for _ in 0..<1024 {
f(t)
}
}
test(Klass())