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-6109] AnyObject as associated type constraint causes EXC_BAD_ACCESS #48664

Closed
DevAndArtist mannequin opened this issue Oct 10, 2017 · 3 comments
Closed

[SR-6109] AnyObject as associated type constraint causes EXC_BAD_ACCESS #48664

DevAndArtist mannequin opened this issue Oct 10, 2017 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@DevAndArtist
Copy link
Mannequin

DevAndArtist mannequin commented Oct 10, 2017

Previous ID SR-6109
Radar rdar://problem/34911213
Original Reporter @DevAndArtist
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 9.0 (9A235), Swift 4

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee @slavapestov
Priority Medium

md5: c76065f5f8d3ae209d6bdced4604aea6

Issue Description:

The Code bellow with uncommented lines crashes at runtime.

protocol DelegateProtocol : AnyObject {
    func foo()
}

protocol DelegateContainer : AnyObject {
    associatedtype ConcreteDelegate : AnyObject // <== THIS ONE HERE
    var delegate: ConcreteDelegate? { get set }
}

class SomeClass : DelegateContainer {
    // Typealias is needed to solve https://bugs.swift.org/browse/SR-6097
    typealias ConcreteDelegate = DelegateProtocol
    weak var delegate: DelegateProtocol?
}

class Delegate : DelegateProtocol {
    func foo() { print("foo") }
}

func connectDelegateContainer<T>(_ delegateContainer: T, with delegate: AnyObject) where T : DelegateContainer {
    if let aDelegate = delegate as? T.ConcreteDelegate {
        // Uncomment next code line to trigger the issue:
        //   error: Playground execution aborted: error: Execution was interrupted,
        //   reason: EXC_BAD_ACCESS (code=2, address=0x119a19180).
        //   The process has been left at the point where it was interrupted, use
        //   "thread return -x" to return to the state before expression evaluation.
        //
        // print("connected")
        delegateContainer.delegate = aDelegate
    }
}

// Test
let someClass = SomeClass()

let delegate = Delegate()

connectDelegateContainer(someClass, with: delegate)

print(someClass.delegate === delegate) // true

// Uncomment next code line to trigger the issue:
//   error: Playground execution aborted: error: Execution was interrupted,
//   reason: EXC_BAD_ACCESS (code=2, address=0x119a19180).
//   The process has been left at the point where it was interrupted, use
//   "thread return -x" to return to the state before expression evaluation.
//
// someClass.delegate?.foo()

Current workaround is to remove that constraint from the associated type, but in theory this constraint should be allowed.

@belkadan
Copy link
Contributor

First issue seems to be fixed, but uncommenting the last line reproduces the crash.

@swift-ci create

@slavapestov
Copy link
Member

The problem is the protocol conformance should be rejected, because existentails don’t conform to AnyObject.

@slavapestov
Copy link
Member

#12698

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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 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

3 participants