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-8037] Method generic type inference is broken in Swift 4.2 #50570

Closed
swift-ci opened this issue Jun 19, 2018 · 7 comments
Closed

[SR-8037] Method generic type inference is broken in Swift 4.2 #50570

swift-ci opened this issue Jun 19, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8037
Radar rdar://problem/41251418
Original Reporter Tang (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

macOS High Sierra 10.13.4

Xcode 10.0 beta (10L176w) with Swift 4.2

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

md5: 1e942561e9c836589c1407f6f0931023

duplicates:

  • SR-7664 Inconsistent optional casting behaviour with generics

Issue Description:

My app built by Swift 4.2 is broken. I find it is related to method generic type reference.

The attachment is a test project which can reproduce this problem.

  1. Build and run test project in Xcode 9.4, Swift 4.1. It will print "Hello" in console.

  2. Build and run test project in Xcode 10.0 beta (10L176w), Swift 4.2. Nothing will be printed in console.

I don't know whether it is a bug or feature.

@belkadan
Copy link
Contributor

The code contains this line:

let result = (try? body(self)) as? Result

where body(self) returns Result? already. The expected behavior here is to try to unwrap multiple layers of Optional, I guess? @jckarter, did something change here?

Tianyong, you can work around this issue by using (try? body(self)) ?? nil instead.

@belkadan
Copy link
Contributor

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Tianyong (JIRA)

The expected behavior here is to try to unwrap multiple layers of Optional, I guess?

Yes, I want to unwrap multiple layers of Optional here. Thank you for the workaround.

@jckarter
Copy link
Member

Nothing changed in the runtime implementation of casting AFAIK. Is it type checking the expression differently?

@swift-ci
Copy link
Collaborator Author

Comment by Tianyong (JIRA)

When I cast `(try? body(self))` to `String` instead of the generic type `Result`, the value can be unwrapped out.

Contrast the following two expressions:

(try? body(self)) as? String // not nil

(try? body(self)) as? Result // nil

The `Result.self == String.self` is true, but the value of later expression is nil. I think it's an unexpected behavior.

@rudkx
Copy link
Member

rudkx commented Jun 22, 2018

This does not reproduce for me using a build from the swift-4.2-branch.

I think this is the same issue as SR-7664, right @mikeash?

@mikeash
Copy link
Contributor

mikeash commented Jun 22, 2018

Yes, looks like that to me. The underlying problem there was that the runtime function called for the generic version of the cast wouldn't unwrap more than one level of optional. This PR is the fix on the 4.2 branch: #16800

@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 regression swift 4.2
Projects
None yet
Development

No branches or pull requests

6 participants