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-4377] Swift 3.1 regression with conditional casting to protocol #46956

Closed
NachoSoto opened this issue Mar 27, 2017 · 11 comments
Closed

[SR-4377] Swift 3.1 regression with conditional casting to protocol #46956

NachoSoto opened this issue Mar 27, 2017 · 11 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@NachoSoto
Copy link
Contributor

Previous ID SR-4377
Radar rdar://problem/31301668
Original Reporter @NachoSoto
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment
  • Swift version 3.1 (swiftlang-802.0.48 clang-802.0.38)

  • Swift version 4.0.2 (swiftlang-900.0.69.1 clang-900.0.38)

  • Swift version 4.1 (swiftlang-902.0.38 clang-902.0.31)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 3.1Regression, TypeChecker
Assignee @DougGregor
Priority Medium

md5: 0121f0ca456189104247c42e9e89b1b8

is duplicated by:

  • SR-4376 Casting an existential to another existential type for a protocol in a different module erroneously 'always succeeds'

Issue Description:

See screenshots.
The setup:

  • module A:
protocol R: Error {}
  • module B:
enum E: Error, R { case a }
  • module C:
let e: Error = E.a
let _: R? = e as? R

Expected result (worked in Swift 3.0.x): cast compiles and succeeds.
Actual result: Error "Conditional cast from 'Error' to 'ReportableError' always succeeds".

@jckarter
Copy link
Member

@swift-ci create

@DougGregor
Copy link
Member

I'm failing to reproduce this with exactly the same setup. Argh!

@NachoSoto
Copy link
Contributor Author

That sucks :/
Some details I left out:

  • R has a couple properties.

  • E's conformance to R is in an extension.

  • E also conforms to another protocol in a separate module.

@NachoSoto
Copy link
Contributor Author

I attached some screenshots from the real setup. Hopefully you see something I'm not thinking off that's making the difference.
`APIClient.Error` is `E` in my example, and `ReportableError` is `R`.

@NachoSoto
Copy link
Contributor Author

FYI this is still an issue with Swift 4.0.2 (the workaround still works though):

func cast<T, U>(_ x: T, to: U.Type) -> U? {
    return x as? U
}

if let reportableError = cast(error.error, to: ReportableError.self) { ... }

@jckarter
Copy link
Member

jckarter commented Nov 9, 2017

What was your workaround?

@NachoSoto
Copy link
Contributor Author

@jckarter the one you gave me back then 🙂 (I updated my comment).

@jckarter
Copy link
Member

jckarter commented Nov 9, 2017

Sorry, it's been a while. So the cast(to🙂 form works, but as? doesn't? Does it vary by optimization level?

@NachoSoto
Copy link
Contributor Author

That's correct. And I just checked with optimization and as? still produces Conditional cast from 'Error' to 'ReportableError' always succeeds.

@NachoSoto
Copy link
Contributor Author

This still reproduces in Swift 4.1.

@DougGregor
Copy link
Member

Appears fixed in Swift 5.1

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants