Clarification for ref.test
instruction
#560
-
According to spec, type system is nominal. At least, spec for 8. If the :ref:`reference type <syntax-reftype>` :math:`\X{rt}_2` :ref:`matches <match-reftype>` :math:`\X{rt}_1`, then: Whereas * Either :math:`\deftype_1` and :math:`\deftype_2` are equal when :ref:`closed <type-closure>`
under context :math:`C`.
* Or:
* Let the :ref:`sub type <syntax-subtype>` :math:`\TSUB~\TFINAL^?~\heaptype^\ast~\comptype`
be the result of :ref:`unrolling <aux-unroll-deftype>` :math:`\deftype_1`.
* Then there must exist a :ref:`heap type <syntax-heaptype>` :math:`\heaptype_i` in :math:`\heaptype^\ast`
that :ref:`matches <match-heaptype>` :math:`\deftype_2`. However, explanation states that dynamic casts are made structurally. Also, I found tests that approves this (namely, this one). I also saw I tried to use
and following code
where I know that at the stack top is So my question is: is it a mistake in spec or bug in two separate WebAssembly implementations? And how would I work around it? Note that using interface Superclass {
readonly __typeMarker__: string
}
interface SubclassA {
readonly __typeMarker__: "A"
}
interface SubclassB {
readonly __typeMarker__: "B"
} but in case of WebAssembly, if I add a such field, it would also be matched structurally, making this trick impossible. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
WebAssembly's type system is not nominal. It is, in fact, based on an iso-recursive structural typing. |
Beta Was this translation helpful? Give feedback.
-
Wasm requires subtyping to be pre-declared (the correctness of which is validated with the match-comptype relation), but that does not imply that it is nominally typed: two types are equivalent if they have the same structure and have declared supertypes that also both have the same structure (which is what the spec quote specifies). The primary purpose of type hierarchies and casts in Wasm is to escape the limitations of Wasm's static type system, rather than to (directly) encode source-level casts. The latter is highly language-dependent, so no single type system built into Wasm would be able to suite more than one specific languages (for example, considering the details of subtyping on arrays, generics, multiple inheritance, interfaces, nominality, etc). A language with a dynamic type mechanism is expected to implement that itself when compiling to Wasm, just like it would have to when compiling to native code. Sometimes Wasm's casts can provide certain shortcuts, but they cannot possibly cover the generality of a languages-specific system. |
Beta Was this translation helpful? Give feedback.
-
The analogous trick in WebAssembly is to put the structurally identical types into the same recursion group, which forces them to be separate types. This only works when the compiler sees the whole program and knows what types to put into the same rec group, though. |
Beta Was this translation helpful? Give feedback.
The analogous trick in WebAssembly is to put the structurally identical types into the same recursion group, which forces them to be separate types. This only works when the compiler sees the whole program and knows what types to put into the same rec group, though.