Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14362] Crash running closures accessed via Mirror #56720

Closed
adam-fowler opened this issue Mar 16, 2021 · 3 comments
Closed

[SR-14362] Crash running closures accessed via Mirror #56720

adam-fowler opened this issue Mar 16, 2021 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@adam-fowler
Copy link

Previous ID SR-14362
Radar rdar://problem/75481739
Original Reporter @adam-fowler
Type Bug
Environment

Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin20.3.0

Apple Swift version 5.4 (swiftlang-1205.0.26.4 clang-1205.0.19.54)
Target: x86_64-apple-darwin20.3.0

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: 656953fcd979edcfc3bb456375a2014f

Issue Description:

The following code crashes when f(object) is called

struct Object {
    let s: String
    let function: (Object) -> String
}
let object = Object(s: "Hello") { obj in
    return "\(obj.s)"
}
let mirror = Mirror(reflecting: object)
for child in mirror.children {
    if let f = child.value as? (Object) -> String {
        print(f(object))
    }
}

If instead I store `function` as `Any` as below then everything works

struct Object {
    let s: String
    let function: Any
    init(s: String, function: @escaping MyFunction) {
        self.s = s
        self.function = function
    }
}

-There seems to have been a regression from 5.3 > 5.4 as well. While the following code works in 5.3.2 it fails in 5.4

struct A { let b: Any }
struct Z { let b: () -> Int }
let x = Int.random(in: .min ... .max)
assert((Mirror(reflecting: A { x }).descendant("b") as! () -> Int)() == x)
assert((Mirror(reflecting: Z { x }).descendant("b") as! () -> Int)() == x)
@gwynne
Copy link
Contributor

gwynne commented Mar 16, 2021

Correction: The version using assert() above DOES fail in both 5.3 and 5.4, but only when built Debug and built as an executable (either from Xcode or with swiftc). It does not fail in Release builds, nor in the REPL, in either Swift version.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@tbkka
Copy link
Contributor

tbkka commented Jul 9, 2022

This is basically the same issue as #54696 [SR-12268]

@tbkka tbkka closed this as completed Jul 9, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants