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-7722] Optional extension protocol not recognized at runtime in release build #50262

Closed
swift-ci opened this issue May 18, 2018 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7722
Radar rdar://problem/40500142
Original Reporter gahms (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 9.3.1, iOS project

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, OptimizedOnly
Assignee shajrawi (JIRA)
Priority Medium

md5: 5a9b2306bfe76c5e6ef2e0c209f7fcbd

Issue Description:

I have a protocol and I do an extension on Optional that ensures that Optional implements this protocol. In some circumstances, Swift fails to recognize that Optional implements this protocol when asked using "as?".

The problem only surfaces in release build, not in debug build.

To reproduce:

  1. Create a new Xcode project, iOS, single view app

  2. Past below code into AppDelegate.swift

  3. Edit the Scheme and change "Run" to use build configuration "Release"

  4. Run

  5. Expect two lines with "10" in console

  6. Actual result: one line with "10" another with "Optional(10)"

At top of AppDelegate.swift:

protocol OptionalMarkerProtocol {
    var orNil: String { get }
}

extension Optional: OptionalMarkerProtocol {
    var orNil : String {
        guard let me = self else {
            return "nil"
        }
        return "\(me)"
    }
}

func pretty1<T: Equatable>(_ value: T) -> String {
    let svalue: String
    if let optional = value as? OptionalMarkerProtocol {
        svalue = optional.orNil
    }
    else {
        svalue = String(describing: value)
    }
    
    return svalue
}

func pretty2(_ value: Any) -> String {
    let svalue: String
    if let optional = value as? OptionalMarkerProtocol {
        svalue = optional.orNil
    }
    else {
        svalue = String(describing: value)
    }
    
    return svalue
}

In application(application:didFinishLaunchingWithOptions🙂:

        let io: Int? = 10
        print(pretty1(io))
        print(pretty2(io as Any))

I am including pretty2() for reference as going via "Any" seems to be a work-around.

@rudkx
Copy link
Member

rudkx commented May 23, 2018

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Joe Shajrawi (JIRA)

Fixed: #16829

@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 optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

2 participants