-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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).
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
Add composition types
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@jckarter Let me know if you foresee any problem with this change in cast behavior.