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-9957] EXC_BAD_ACCESS when protocol casted object is being used #52361

Closed
swift-ci opened this issue Feb 19, 2019 · 1 comment
Closed

[SR-9957] EXC_BAD_ACCESS when protocol casted object is being used #52361

swift-ci opened this issue Feb 19, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9957
Radar None
Original Reporter leonid.kokhnovych (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

OS X: 10.14.2 (18C54)

Xcode: Version 10.1 (10B61)

swiftc --version

Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)

Target: x86_64-apple-darwin18.2.0

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

md5: 358532371b4d064581630d4043d15438

Issue Description:

Hi! Thanks for your efforts on improving the Swift ecosystem.

When I run the following code:

import Foundation


protocol TrackingDelegate where Self: UIViewController {
    func test()
}


class Test: UIViewController { }


extension Test: TrackingDelegate {
    func test() {
        print(self)
    }
}


func methodUsedToTriggerTest(for controller: UIViewController) {
    if let infoProvider = controller as? TrackingDelegate {
        print("Run test")
        return infoProvider.test()
    }
}


print("Test started \(arc4random())")
let test = Test()
methodUsedToTriggerTest(for: test)
print("Test completed")

I get the exception on line:

methodUsedToTriggerTest(for: test)

with the following message:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x0).

The actual exception is thrown on line:

print(self)

For me, it looks like when the casting is done to a protocol TrackingDelegate that doesn't have the class conformance like "protocol TrackingDelegate: class", the object is considered as a value type and not the reference one.

It might have something similar to https://bugs.swift.org/browse/SR-9207 , but I'm not.

@belkadan
Copy link
Contributor

The where Self: UIViewController syntax wasn't fully implemented in Swift 4.2 and shouldn't really have been allowed. Your example works in Swift 5.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants