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-2453] Optional<T> as? AnyObject breaks in Swift 3 #45058

Closed
pcantrell opened this issue Aug 22, 2016 · 5 comments
Closed

[SR-2453] Optional<T> as? AnyObject breaks in Swift 3 #45058

pcantrell opened this issue Aug 22, 2016 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 3.0 type checker Area → compiler: Semantic analysis

Comments

@pcantrell
Copy link

Previous ID SR-2453
Radar None
Original Reporter @pcantrell
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 8 beta 6

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

md5: e5ec372253da24e4c63703915f06834f

duplicates:

  • SR-2420 AnyObject not working in Xcode8 beta6

Issue Description:

Swift 3’s auto-wrapping of Swift values as objects when casting to AnyObject produces surprising (and breaking) behavior when casting from Optional.

In Swift 2, as? AnyObject would unwrap an optional to check whether the wrapped value is an object. In Swift 3, the same syntax wraps the Optional itself in an object. This gives surprising behavior for ===:

class Foo { }
let foo: Foo? = Foo()

// Swift 2: true
// Swift 3: false (ouch!), with “Conditional cast always succeeds” warning
foo === (foo as? AnyObject)

// Swift 2: “Foo? is not convertible to AnyObject”
// Swift 3: false, no warning
foo === (foo as AnyObject)

This regression introduced several insidious bugs in Siesta, and also seems to affect Nimble.

@belkadan
Copy link
Contributor

cc @jckarter

@jckarter
Copy link
Member

This is intentional, since anything can be coerced to an object now. John should have fixed the runtime inconsistencies in master.

@pcantrell
Copy link
Author

Not quite a dup of SR-2420. I do understand the new Swift 3 behavior that any value type can now be converted to AnyObject. This bug is about that behavior coming into play in situations where the reasonable expectation (and Swift 2 behavior) is to unwrap an optional instead of turning the optional into an object.

In particular, it’s this breaking change that concerns me:

// Swift 2: true
// Swift 3: false (regression!), with “Conditional cast always succeeds” warning
foo === (foo as? AnyObject)

I have trouble imagining someone using as? where the LHS is an optional and not intending to do a a conditional cast on the unwrapped value.

If you disagree, and do want to preserve the current behavior, may I suggest changing the fixit from this:

foo === (foo as AnyObject)

…to this, which preserves the Swift 2 behavior and is almost certainly what’s intended:

foo === (foo as AnyObject?)

@jckarter
Copy link
Member

I see. Yeah, it would be more reasonable to favor bridging through the optional in this case.

@pcantrell
Copy link
Author

This appears to be fixed as of Swift 5.4. Closing.

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

No branches or pull requests

4 participants