Closure obtained from reflection cannot properly address input parameters

Number:rdar://FB9823642 Date Originated:12.27.21
Status: Resolved:
Product:Swift Standard Library Product Version:
Classification:Bug Reproducible:Always
Given this trivial code:

struct Container {
    let closure: (Int) -> Void

let container = Container { param in

One can assume calling container.closure(42) will print out 42, which is true.

However, if this same closure is retrieved from the container's Mirror:

let mirror = Mirror(reflecting: container)
let closure = mirror.children
    .first(where: { $0.label == "closure" })!
    .value as! ((Int) -> Void)

... then calling closure(42) distorts the parameter's value, and it prints out 6166589480.

The same thing happens if you use String instead of Int, and I assume with other types too.
If I pass a reference to an object, expectedly that reference gets messed up too and I get EXC_BAD_ACCESS when trying to access the object.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!