-
Notifications
You must be signed in to change notification settings - Fork 74
Casting null #152
Comments
Bumped into this in Kotlin compiler. It would be real nice to have a version of Also, in second example above, value of |
I does not see any sense that I can't cast a NULL value to another nullable value. This is also not consistence because a null value required an type for |
The use case I care most about is, e.g. downcasting from (the wasm representation of) |
Hm, I see the reasoning, but there is a trade-off. It would change the typing from
to
This would be a potential extra cost in contexts where the producer knows out of band that the argument can't be null. It would be forced to insert an additional Another, more theoretical problem I have is about coherent semantics. We unfortunately were forced to remove the nullref type and introduce type-indexed null values, due to WebAssembly/reference-types#87. I always feared that would bite us, and it does here. Consider:
Even in cases where |
Seems reasonable, and matches what I've heard from toolchain authors: both null-permitting and null-rejecting casts are useful. |
Closing via #161. |
Currently,
ref.cast
is spec'd to trap on null, and always return a non-null reference. For some languages, e.g. Java, casts of a null succeed. Thus to implement this behavior, user code must insert a branch checking for null first. To avoid making that check for null redundant with the null check inref.cast
,br_on_null
is a natural fit, since it refines the type to be non-null in the fallthrough. Butbr_on_null
pops the null value off of the operand stack, so we end up with a sequence like:Or another option:
(note the above are simplified, omitting the RTT values)
Instead both of these sequences could be simplified if we change the semantics of
ref_cast
. For example, ifref_cast
has a type immediate that is allowed to be a nullable type, then the nullability could be used to indicate whethernull
is allowed to succeed, or should trap.The text was updated successfully, but these errors were encountered: