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-7634] EXC_BAD_ACCESS when using a Protocol which has a Constraint on it's definition #50175

Closed
swift-ci opened this issue May 8, 2018 · 4 comments
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

@swift-ci
Copy link
Collaborator

swift-ci commented May 8, 2018

Previous ID SR-7634
Radar rdar://problem/40059422
Original Reporter georgemp (JIRA User)
Type Bug
Status Closed
Resolution Duplicate
Environment

OS X 10.13.4

XCode 9.3 (9E145)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, RunTimeCrash
Assignee georgemp (JIRA)
Priority Medium

md5: c2650d576005727d191e3766798dc932

duplicates:

  • SR-6000 Let protocols inherit from class types

Issue Description:

I'm trying to constrain a protocol to instances of a certain class (or it's subclasses). The compiler doesn't complain about the following code (and, seems to restrict the protocol to the specified class)

import Foundation

class C {}

protocol P where Self: C {
    var x: Int { get }
    var y: Int { get }
}

extension P {
    var x: Int { return 42 }
}

class D: C, P {
    var y: Int { return x + 1 }
}

func f(vc: C) {
    if let vc = vc as? P {
        print(vc.x)
        print(vc.y)
    }
}

f(vc: D())

However, running the code causes a crash.

* thread #​1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #​0: 0x0000000000000000
    frame #​1: 0x0000000100001fee ProtocolConstraintBug`protocol witness for P.y.getter in conformance D at main.swift:0
  * frame #​2: 0x0000000100002363 ProtocolConstraintBug`f(vc=0x0000000103104230) at main.swift:29
    frame #​3: 0x0000000100001da6 ProtocolConstraintBug`main at main.swift:33
    frame #​4: 0x00007fff56379015 libdyld.dylib`start + 1
    frame #​5: 0x00007fff56379015 libdyld.dylib`start + 1

Moving the constraint to the protocol extension seems to work

import Foundation

class C {}

protocol P {
    var x: Int { get }
    var y: Int { get }
}

extension P where Self: C {
    var x: Int { return 42 }
}

class D: C, P {
    var y: Int { return x + 1 }
}

func f(vc: C) {
    if let vc = vc as? P {
        print(vc.x)
        print(vc.y)
    }
}

f(vc: D())
@belkadan
Copy link
Contributor

belkadan commented May 8, 2018

With assertions enabled:

TYPE MISMATCH IN ARGUMENT 0 OF APPLY AT expression at [<stdin>:18:15 - line:18:18] RangeText="vc.x"
  argument value:   %36 = alloc_stack $@opened("D5ADD730-52D8-11E8-BCAF-9A0002AC2BC0") P // type-defs: %35; user: %37
  parameter type: $@opened("D5ADD730-52D8-11E8-BCAF-9A0002AC2BC0") P

1.  While emitting SIL for 'f(vc:)' at <stdin>:16:1
2.  While silgen emitFunction SIL function "@$S4main1f2vcyAA1CC_tF".

@slavapestov's been fixing a bunch of these but I guess he didn't get to this one.

@swift-ci create

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 23, 2019

I cannot reproduce, getting:
42
43
Program ended with exit code: 0

So looks like this has been fixed.

georgemp (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

Comment by Malayil Philip George (JIRA)

Hi,

I tried the tests again

Xcode 10.1 - Crashes

Xcode 10.2.1 - No Crash

Xcode 10.3 - No Crash

Looks like it's been fixed. Thanks

@swift-ci
Copy link
Collaborator Author

Comment by Malayil Philip George (JIRA)

Doesn't crash on Xcode 10.2.1 and 10.3

@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 Oct 22, 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