Skip to content

Runtime optional casts. #204

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
merged 3 commits into from
Dec 10, 2015
Merged

Runtime optional casts. #204

merged 3 commits into from
Dec 10, 2015

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Dec 4, 2015

@jckarter Let me know if you foresee any problem with this change in cast behavior.

let ssx : SomeClass?? = sx
print(anyToAny(ssx, Optional<SomeClass>.self)! === x)
print(anyToAny(x, Optional<Optional<SomeClass>>.self)!! === x)
print(anyToAnyOrNil(ni, Int.self) == nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

print? Shouldn't it use expectEqual?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. I decided to move these all to an Interpreter test case, rewrote them all as prints, then changed my mind.

@jckarter jckarter assigned jckarter and atrick and unassigned jckarter Dec 4, 2015
@atrick atrick assigned gribozavr and atrick and unassigned atrick and gribozavr Dec 5, 2015
Reuses the enum metadata layout and builder because most of the logic is
also required for Optional (generic arg and payload). We may want to
optimize this at some point (Optional doesn't have a Parent), but I
don't see much opportunity.

Note that with this approach there will be no change in metadata layout.
Changing the kind still breaks the ABI of course.

Also leaves the MirrorData summary string as "(Enum Value)". We should
consider changing it.
There was previously no way to detect a type that is nominally
Optional at runtime. The standard library, namely OutputStream, needs
to handle Optionals specially in order to cirumvent conversion to the
Optional's wrapped type. This should be done with conditional
conformance, but until that feature is available, Builtin.isOptional
will serve as a useful crutch.
Fixes <rdar://23122310> Runtime dynamic casts...

This makes runtime dynamic casts consistent with language rules, and
consequently makes specialization of generic code consistent with an
equivalent nongeneric implementation.

The runtime now supports casts from Optional<T> to U. Naturally the
cast fails on nil source, but otherwise succeeds if T is convertible to
U.

When casting T to Optional<U> the runtime succeeds whenever T is
convertible to U and simply wraps the result in an Optional.

To greatly simplify the runtime, I am assuming that
target-type-specific runtime cast entry points
(e.g. swift_dynamicCastClass) are never invoked with an optional
source. This assumption is valid for the following reasons. At the
language level optionals must be unwrapped before downcasting (via
as[?!]), so we only need to worry about SIL and IR lowering.  This
implementation assumes (with asserts) that:

- SIL promotion from an address cast to a value casts should only happen
  when the source is nonoptional. Handling optional unwrapping in SIL
  would be too complicated because we need to check for Optional's own
  conformances. (I added a test case to ensure this promotion does not
  happen). This is not an issue for unchecked_ref_cast, which
  implicitly unwraps optionals, so we can promote those!

- IRGen lowers unchecked_ref_cast (Builtin.castReference) directly to
  a bitcast (will be caught by asserts).

- IRGen continues to emit the generic dynamicCast entry point for
  address-casts (will be caught by asserts).
atrick added a commit that referenced this pull request Dec 10, 2015
@atrick atrick merged commit 405f9f5 into swiftlang:master Dec 10, 2015
@atrick atrick deleted the optionalcast branch July 7, 2017 00:20
slavapestov pushed a commit to slavapestov/swift that referenced this pull request Nov 27, 2018
…-data-copy

Fixes incorrect behavior of DispatchData.copyBytes() when the start …
slavapestov pushed a commit to slavapestov/swift that referenced this pull request Nov 27, 2018
…-data-copy

Fixes incorrect behavior of DispatchData.copyBytes() when the start …

Signed-off-by: Daniel A. Steffen <[email protected]>
dabelknap pushed a commit to dabelknap/swift that referenced this pull request Jan 18, 2019
maldahleh pushed a commit to maldahleh/swift that referenced this pull request Oct 26, 2020
[Gardening] Update all Python scripting to conform to the style guidelines enforced by the black code-formatting tool.
freak4pc pushed a commit to freak4pc/swift that referenced this pull request Sep 28, 2022
For JSQCoreDataKit drop building Swift 3, and update to tip for Swift 4
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.

4 participants