The following program crashed when compiled with the Swift 5.7 and 5.8 compilers:
```
@available(macOS 12, *)
@_backDeploy(before: macOS 99)
public func foo<T>(_ t: __owned T) {
print("foo")
}
class C {
deinit {
print("deinit")
}
}
foo(C())
print("done")
```
```
> ./test
foo
deinit
[1] 49162 segmentation fault ./test
```
The root cause is that generated SIL for the back deployment thunk for `foo(_:)` included its own `destroy_addr` instruction for the value of `t`, but didn't copy the parameter before passing it to the real function implementation which also destroys the value. The fix is to forward ownership of the parameter values to the called function, which causes cleanup generation to be skipped.
Resolves rdar://104436515
When a throwing function which returns a generic (this is the simplest
example) is back deployed, it gets a back deploy thunk. This back
deploy thunk calls one of two variations of the function, depending on
availability, both which have the same signature and in particular both
return a generic. The result type of that function signature is an
unbound generic.
Previously, that result type was used as is. Consequently the success
blocks for the try_apply instructions in these thunks had arguments of
unbound generic type.
Here, that type is mapped into the context of the function being
emitted. The thunks now have the appropriate bound generic type.
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`
The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.
rdar://102362022
When computing the availability of a `SILFunction` for linkage the OS version specified in the `@_backDeploy` attribute should be preferred over the version in the `@available` attribute. This ensures that the corresponding symbol is weakly linked when deploying to older OSes than the back deploy "before" version.
Resolves rdar://99962885
The SIL verifier has identified an issue with the SIL generated for property accessors structured like this:
```
public struct S {
@available(macOS, introduced: 12.0)
@_backDeploy(before: macOS 13.0)
public var x: String {
_read {
yield "x"
}
}
}
```
The emitted SIL is invalid because the value `%9` is used after `end_apply` may have ended the lifetime of the value:
```
bb1:
%8 = function_ref @$s4test1SV1xSSvrTwB : $@yield_once @convention(method) (S) -> @yields @guaranteed String9
(%9, %10) = begin_apply %8(%0) : $@yield_once @convention(method) (S) -> @yields @guaranteed String
end_apply %10
yield %9 : $String, resume bb3, unwind bb2
```
The fix is to move the `end_apply` to the resume and unwind blocks, after the value has been yielded to the caller.
Resolves rdar://96879247