Order for testing subtyping between modules #7
Description
This is an issue for the design proposed in #3 (assumed to be merged in the near-ish future) where I'm curious how the subtyping relationship works between modules. Talking with @lukewagner it sounds like import strings are going to be taken into account, but this raises a question about what to do with imports that import from the same string? For example I'm curious if this module is intended to validate:
(module
(module $a
(import "anything_here" (module (;$a.b;)
(import "" (instance (;$a.1;)
(export "bar" (func))
(export "foo" (func))
))
(import "" (instance (;$a.2;)
(export "foo" (func))
))
))
)
(module $b
(import "" (instance (;$b.1;)
(export "bar" (func))
))
(import "" (instance (;$b.2;)
(export "foo" (func))
))
)
(instance (instantiate $a (module $b)))
)
A similar question would be if the imports of $b
were swapped, would it still validate?
I think one possible way to test if $a
is a subtype of $b
would be to:
- Build a map from import string to number of items imported from that import string of
$b
- For each import name
n
of$a
, look up its import string in this map. - If
n
exists in$b
with only one item imported, then the subtyping check recurses (switching order for covariance/contravariance?) - If
n
exists in$b
, but with multiple items imported, then the first unused import (so far) of$b
is tested against the import. - If
n
doesn't exist in$b
it could do the same as above (check the first unused import) or return an error.
The thinking is that on collisions (multiple imports from the same name) implementations would fall back to order-based comparisons of types. Otherwise you'd do name-based comparisons. This would allow, in general, imports to get reordered or deleted and you wouldn't break code which depends on you (as it would still assert your older type). If, however, all your import names were the same string then it'd be stricter.
I'm curious though if others have thoughts about how this might work? Or how to handle the subtyping relationship between modules with respect to their imports and multiple imports from the same name?