Skip to content

Commit d3b1118

Browse files
rylevalexcrichton
andauthored
Do type remapping through component instances (#1086)
Co-authored-by: Alex Crichton <alex@alexcrichton.com>
1 parent 1f5170f commit d3b1118

File tree

3 files changed

+116
-14
lines changed

3 files changed

+116
-14
lines changed

crates/wasmparser/src/validator/types.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,20 +2693,7 @@ impl<'a> SubtypeCx<'a> {
26932693
// satisfied. This is then used afterwards when performing
26942694
// type substitution to remap all component-local types to
26952695
// those that were provided in the imports.
2696-
match (expected, actual) {
2697-
(
2698-
ComponentEntityType::Type {
2699-
created: expected, ..
2700-
},
2701-
ComponentEntityType::Type {
2702-
created: actual, ..
2703-
},
2704-
) => {
2705-
let prev = type_map.insert(expected, actual);
2706-
assert!(prev.is_none());
2707-
}
2708-
_ => {}
2709-
}
2696+
self.register_type_renamings(actual, expected, &mut type_map);
27102697
continue;
27112698
}
27122699
Err(e) => e,
@@ -3009,6 +2996,40 @@ impl<'a> SubtypeCx<'a> {
30092996
bail!(offset, "expected primitive `{b}` found primitive `{a}`")
30102997
}
30112998
}
2999+
3000+
fn register_type_renamings(
3001+
&self,
3002+
actual: ComponentEntityType,
3003+
expected: ComponentEntityType,
3004+
type_map: &mut HashMap<TypeId, TypeId>,
3005+
) {
3006+
match (expected, actual) {
3007+
(
3008+
ComponentEntityType::Type {
3009+
created: expected, ..
3010+
},
3011+
ComponentEntityType::Type {
3012+
created: actual, ..
3013+
},
3014+
) => {
3015+
let prev = type_map.insert(expected, actual);
3016+
assert!(prev.is_none());
3017+
}
3018+
(ComponentEntityType::Instance(expected), ComponentEntityType::Instance(actual)) => {
3019+
let actual = self.a[actual].as_component_instance_type().unwrap();
3020+
for (name, expected) in self.b[expected]
3021+
.as_component_instance_type()
3022+
.unwrap()
3023+
.exports
3024+
.iter()
3025+
{
3026+
let actual = actual.exports[name];
3027+
self.register_type_renamings(actual, *expected, type_map);
3028+
}
3029+
}
3030+
_ => {}
3031+
}
3032+
}
30123033
}
30133034

30143035
/// A helper typed used purely during subtyping as part of `SubtypeCx`.

tests/local/component-model/type-export-restrictions.wast

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,41 @@
474474
(export "f" (type $f))
475475
)
476476
"type not valid to be used as export")
477+
478+
(component
479+
(type (;0;)
480+
(instance
481+
(type (;0;) (enum "qux"))
482+
(export (;1;) "baz" (type (eq 0)))
483+
(type (;2;) (record (field "bar" 1) ))
484+
(export (;3;) "foo" (type (eq 2)))
485+
)
486+
)
487+
(import (interface "demo:component/types") (instance (;0;) (type 0)))
488+
(component
489+
(type (;0;)
490+
(instance
491+
(type (;0;) (enum "qux"))
492+
(export (;1;) "baz" (type (eq 0)))
493+
(type (;2;) (record (field "bar" 1) ))
494+
(export (;3;) "foo" (type (eq 2)))
495+
)
496+
)
497+
(import (interface "demo:component/types") (instance (;0;) (type 0)))
498+
(component (;0;)
499+
(type (;0;) (enum "qux"))
500+
(import "import-type-baz" (type (;1;) (eq 0)))
501+
(type (;2;) (record (field "bar" 1) ))
502+
(import "import-type-bar" (type (;3;) (eq 2)))
503+
(export (;4;) "foo" (type 3))
504+
)
505+
(instance (;1;) (instantiate 0
506+
(with "import-type-baz" (type 0 "baz"))
507+
(with "import-type-bar" (type 0 "foo"))
508+
)
509+
)
510+
(export (;0;) (interface "demo:component/types") (instance 1))
511+
)
512+
(instance (instantiate 0 (with "demo:component/types" (instance 0))))
513+
(export (interface "demo:component/types") (instance 1 "demo:component/types"))
514+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(component
2+
(type (;0;)
3+
(instance
4+
(type (;0;) (enum "qux"))
5+
(export (;1;) "baz" (type (eq 0)))
6+
(type (;2;) (record (field "bar" 1)))
7+
(export (;3;) "foo" (type (eq 2)))
8+
)
9+
)
10+
(import (interface "demo:component/types") (instance (;0;) (type 0)))
11+
(component (;0;)
12+
(type (;0;)
13+
(instance
14+
(type (;0;) (enum "qux"))
15+
(export (;1;) "baz" (type (eq 0)))
16+
(type (;2;) (record (field "bar" 1)))
17+
(export (;3;) "foo" (type (eq 2)))
18+
)
19+
)
20+
(import (interface "demo:component/types") (instance (;0;) (type 0)))
21+
(component (;0;)
22+
(type (;0;) (enum "qux"))
23+
(import "import-type-baz" (type (;1;) (eq 0)))
24+
(type (;2;) (record (field "bar" 1)))
25+
(import "import-type-bar" (type (;3;) (eq 2)))
26+
(export (;4;) "foo" (type 3))
27+
)
28+
(alias export 0 "baz" (type (;1;)))
29+
(alias export 0 "foo" (type (;2;)))
30+
(instance (;1;) (instantiate 0
31+
(with "import-type-baz" (type 1))
32+
(with "import-type-bar" (type 2))
33+
)
34+
)
35+
(export (;2;) (interface "demo:component/types") (instance 1))
36+
)
37+
(instance (;1;) (instantiate 0
38+
(with "demo:component/types" (instance 0))
39+
)
40+
)
41+
(alias export 1 "demo:component/types" (instance (;2;)))
42+
(export (;3;) (interface "demo:component/types") (instance 2))
43+
)

0 commit comments

Comments
 (0)