Skip to content

[Sema] Don't treat archetypes as non-optional when casting to them #13910

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

Merged

Conversation

hamishknight
Copy link
Contributor

When casting an optional value to a (less-optional) archetype, don't dig into the optional as the archetype could be an optional type at runtime.

Resolves SR-4248.

@hamishknight
Copy link
Contributor Author

It's worth noting that this PR doesn't fully fix this FIXME:

  // FIXME: some of this work needs to be delayed until runtime to
  // properly account for archetypes dynamically being optional
  // types.  For example, if we're casting T to NSView?, that
  // should succeed if T=NSObject? and its value is actually nil.

Currently attempting to cast to an optional type from a (less-optional) archetype is a compiler error:

"Cannot downcast from 'T' to a more optional type 'X?'"

I would like a little clarification on the intended rules behind this error though; is the intention to permit such casts for archetype and existential operands, as they could hold optional values at runtime?

@hamishknight
Copy link
Contributor Author

cc. @rjmccall – would you be available to review this?

// bridging operation, or if the destination value type is an
// archetype (and could therefore be an Optional type at runtime),
// leave any extra optionals on the source in place.
if (isBridgeToAnyObject || destValueType->is<ArchetypeType>()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should disallow class-archetypes. We have a function in DynamicCasts.cpp, canDynamicallyBeOptionalType(). It's not quite what you want because it allows existentials, but please extract something in common that can be used by both places. Having a parameter indicating whether we're asking whether the type could be an optional type (what you're asking here) or store an optional type (what DynamicCasts.cpp is asking) would be sensible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very helpful, thanks @rjmccall! I've gone ahead and made those changes, let me know if they're what you had in mind.

@hamishknight hamishknight force-pushed the dont-treat-archetype-as-nonoptional branch from 08760e2 to d6b6969 Compare January 23, 2018 15:48
@rjmccall
Copy link
Contributor

Thanks, that looks good.

@rjmccall
Copy link
Contributor

@swift-ci Please test.

@hamishknight
Copy link
Contributor Author

Any news on this @rjmccall?

@rjmccall
Copy link
Contributor

Yes, that's fine, thanks.

@hamishknight hamishknight force-pushed the dont-treat-archetype-as-nonoptional branch from d6b6969 to ab82049 Compare February 7, 2018 12:52
@hamishknight
Copy link
Contributor Author

@rjmccall Sorry, my last comment was fairly vague; I was just wondering whether we'd be able to get this merged soon? (forgive me, I'm still fairly new to all this)

I've just rebased to resolve a merge conflict with b4b66bc, could you please kick off the CI bot again?

@rjmccall
Copy link
Contributor

rjmccall commented Feb 7, 2018

@swift-ci Please test.

@swift-ci
Copy link
Contributor

swift-ci commented Feb 7, 2018

Build failed
Swift Test Linux Platform
Git Sha - d6b696913a396f2ac082af73662debc4c8ab7e69

@swift-ci
Copy link
Contributor

swift-ci commented Feb 7, 2018

Build failed
Swift Test OS X Platform
Git Sha - d6b696913a396f2ac082af73662debc4c8ab7e69

@rjmccall rjmccall merged commit b140b8c into swiftlang:master Feb 8, 2018
@rjmccall
Copy link
Contributor

rjmccall commented Feb 8, 2018

Thanks!

@hamishknight hamishknight deleted the dont-treat-archetype-as-nonoptional branch February 8, 2018 11:10
hamishknight added a commit to hamishknight/swift that referenced this pull request Sep 10, 2018
…5 mode

In swiftlang#13910, the behaviour of optional-to-archetype casts changed such that we're now more conservative with the unwrapping of the operand at compile time in order to account for the fact that an optional type could be substituted at runtime. This brought such casting behaviour inline with that in a non-generic context, however it wasn't properly gated by Swift version, leading to compatibility issues.

This commit restricts the new behaviour to Swift 5 mode and above.

Resolves SR-8704.
hamishknight added a commit to hamishknight/swift that referenced this pull request Sep 26, 2018
…5 mode

In swiftlang#13910, the behaviour of optional-to-archetype casts changed such that we're now more conservative with the unwrapping of the operand at compile time in order to account for the fact that an optional type could be substituted at runtime. This brought such casting behaviour inline with that in a non-generic context, however it wasn't properly gated by Swift version, leading to compatibility issues.

This commit restricts the new behaviour to Swift 5 mode and above.

Resolves SR-8704.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants