Skip to content

Commit 6f3b62c

Browse files
committed
Fix aggregation of imports.
This commit fixes aggregation of imports that have used types. Previously, interfaces weren't being coalesced properly, resulting in duplicate imports of used interfaces that would result in an encoding validation error. The fix is to coalesce interfaces by interface identifier, merging interfaces together during the remapping process. Additionally, this fixes a bug in encoding where any implicitly imported instances were not getting their encoded indexes recorded in the encoding state properly. Also changed the map in the type aggregator to store `Type`s rather than `ItemKind`s, which makes things neater. Fixes #65.
1 parent 2d25c05 commit 6f3b62c

6 files changed

Lines changed: 569 additions & 116 deletions

File tree

crates/wac-graph/src/graph.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ impl<'a> CompositionGraphEncoder<'a> {
13511351
let mut arguments = Vec::new();
13521352
let mut encoded = HashMap::new();
13531353
let mut cache = Default::default();
1354+
let mut checker = SubtypeChecker::new(&mut cache);
13541355

13551356
// Enumerate the instantiation nodes and populate the import types
13561357
for node in self.0.nodes() {
@@ -1379,7 +1380,7 @@ impl<'a> CompositionGraphEncoder<'a> {
13791380
}
13801381

13811382
aggregator = aggregator
1382-
.aggregate(name, package.types(), *kind, &mut cache)
1383+
.aggregate(name, package.types(), *kind, &mut checker)
13831384
.map_err(|e| Error::ImportTypeMergeConflict {
13841385
import: name.clone(),
13851386
source: e,
@@ -1404,6 +1405,11 @@ impl<'a> CompositionGraphEncoder<'a> {
14041405
},
14051406
);
14061407

1408+
// Ensure we insert an instance index into the current scope
1409+
if let ItemKind::Instance(id) = kind {
1410+
state.current.instances.insert(id, index);
1411+
}
1412+
14071413
let prev = encoded.insert(name, (kind.into(), index));
14081414
assert!(prev.is_none());
14091415
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(component
2+
(type (;0;)
3+
(instance
4+
(export (;0;) "x" (type (sub resource)))
5+
)
6+
)
7+
(import "foo:bar/baz" (instance (;0;) (type 0)))
8+
(alias export 0 "x" (type (;1;)))
9+
(type (;2;)
10+
(instance
11+
(alias outer 1 1 (type (;0;)))
12+
(export (;1;) "x" (type (eq 0)))
13+
(type (;2;) (own 1))
14+
(type (;3;) (func (result 2)))
15+
(export (;0;) "f" (func (type 3)))
16+
)
17+
)
18+
(import "foo:bar/qux" (instance (;1;) (type 2)))
19+
(core module (;0;)
20+
(type (;0;) (func (param i32)))
21+
(type (;1;) (func (result i32)))
22+
(type (;2;) (func (param i32 i32 i32 i32) (result i32)))
23+
(import "foo:bar/baz" "[resource-drop]x" (func (;0;) (type 0)))
24+
(import "foo:bar/qux" "f" (func (;1;) (type 1)))
25+
(func (;2;) (type 1) (result i32)
26+
unreachable
27+
)
28+
(func (;3;) (type 2) (param i32 i32 i32 i32) (result i32)
29+
unreachable
30+
)
31+
(memory (;0;) 0)
32+
(export "foo:bar/qux#f" (func 2))
33+
(export "memory" (memory 0))
34+
(export "cabi_realloc" (func 3))
35+
(@producers
36+
(processed-by "wit-component" "0.201.0")
37+
)
38+
)
39+
(alias export 0 "x" (type (;3;)))
40+
(core func (;0;) (canon resource.drop 3))
41+
(core instance (;0;)
42+
(export "[resource-drop]x" (func 0))
43+
)
44+
(alias export 1 "f" (func (;0;)))
45+
(core func (;1;) (canon lower (func 0)))
46+
(core instance (;1;)
47+
(export "f" (func 1))
48+
)
49+
(core instance (;2;) (instantiate 0
50+
(with "foo:bar/baz" (instance 0))
51+
(with "foo:bar/qux" (instance 1))
52+
)
53+
)
54+
(alias core export 2 "memory" (core memory (;0;)))
55+
(alias core export 2 "cabi_realloc" (core func (;2;)))
56+
(alias export 0 "x" (type (;4;)))
57+
(type (;5;) (own 4))
58+
(type (;6;) (func (result 5)))
59+
(alias core export 2 "foo:bar/qux#f" (core func (;3;)))
60+
(func (;1;) (type 6) (canon lift (core func 3)))
61+
(alias export 0 "x" (type (;7;)))
62+
(component (;0;)
63+
(import "import-type-x" (type (;0;) (sub resource)))
64+
(import "import-type-x0" (type (;1;) (eq 0)))
65+
(type (;2;) (own 1))
66+
(type (;3;) (func (result 2)))
67+
(import "import-func-f" (func (;0;) (type 3)))
68+
(export (;4;) "x" (type 0))
69+
(type (;5;) (own 4))
70+
(type (;6;) (func (result 5)))
71+
(export (;1;) "f" (func 0) (func (type 6)))
72+
)
73+
(instance (;2;) (instantiate 0
74+
(with "import-func-f" (func 1))
75+
(with "import-type-x" (type 7))
76+
(with "import-type-x0" (type 4))
77+
)
78+
)
79+
(export (;3;) "foo:bar/qux" (instance 2))
80+
(@producers
81+
(processed-by "wit-component" "0.201.0")
82+
)
83+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package foo:bar;
2+
3+
interface baz {
4+
resource x;
5+
}
6+
7+
interface qux {
8+
use baz.{x};
9+
f: func() -> x;
10+
}
11+
12+
world w {
13+
import qux;
14+
export qux;
15+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
(component
2+
(type (;0;)
3+
(instance
4+
(export (;0;) "x" (type (sub resource)))
5+
)
6+
)
7+
(import "foo:bar/baz" (instance (;0;) (type 0)))
8+
(alias export 0 "x" (type (;1;)))
9+
(type (;2;)
10+
(instance
11+
(alias outer 1 1 (type (;0;)))
12+
(export (;1;) "x" (type (eq 0)))
13+
(type (;2;) (own 1))
14+
(type (;3;) (func (result 2)))
15+
(export (;0;) "f" (func (type 3)))
16+
)
17+
)
18+
(import "foo:bar/qux" (instance (;1;) (type 2)))
19+
(component (;0;)
20+
(type (;0;)
21+
(instance
22+
(export (;0;) "x" (type (sub resource)))
23+
)
24+
)
25+
(import "foo:bar/baz" (instance (;0;) (type 0)))
26+
(alias export 0 "x" (type (;1;)))
27+
(type (;2;)
28+
(instance
29+
(alias outer 1 1 (type (;0;)))
30+
(export (;1;) "x" (type (eq 0)))
31+
(type (;2;) (own 1))
32+
(type (;3;) (func (result 2)))
33+
(export (;0;) "f" (func (type 3)))
34+
)
35+
)
36+
(import "foo:bar/qux" (instance (;1;) (type 2)))
37+
(core module (;0;)
38+
(type (;0;) (func (param i32)))
39+
(type (;1;) (func (result i32)))
40+
(type (;2;) (func (param i32 i32 i32 i32) (result i32)))
41+
(import "foo:bar/baz" "[resource-drop]x" (func (;0;) (type 0)))
42+
(import "foo:bar/qux" "f" (func (;1;) (type 1)))
43+
(func (;2;) (type 1) (result i32)
44+
unreachable
45+
)
46+
(func (;3;) (type 2) (param i32 i32 i32 i32) (result i32)
47+
unreachable
48+
)
49+
(memory (;0;) 0)
50+
(export "foo:bar/qux#f" (func 2))
51+
(export "memory" (memory 0))
52+
(export "cabi_realloc" (func 3))
53+
(@producers
54+
(processed-by "wit-component" "0.201.0")
55+
)
56+
)
57+
(alias export 0 "x" (type (;3;)))
58+
(core func (;0;) (canon resource.drop 3))
59+
(core instance (;0;)
60+
(export "[resource-drop]x" (func 0))
61+
)
62+
(alias export 1 "f" (func (;0;)))
63+
(core func (;1;) (canon lower (func 0)))
64+
(core instance (;1;)
65+
(export "f" (func 1))
66+
)
67+
(core instance (;2;) (instantiate 0
68+
(with "foo:bar/baz" (instance 0))
69+
(with "foo:bar/qux" (instance 1))
70+
)
71+
)
72+
(alias core export 2 "memory" (core memory (;0;)))
73+
(alias core export 2 "cabi_realloc" (core func (;2;)))
74+
(alias export 0 "x" (type (;4;)))
75+
(type (;5;) (own 4))
76+
(type (;6;) (func (result 5)))
77+
(alias core export 2 "foo:bar/qux#f" (core func (;3;)))
78+
(func (;1;) (type 6) (canon lift (core func 3)))
79+
(alias export 0 "x" (type (;7;)))
80+
(component (;0;)
81+
(import "import-type-x" (type (;0;) (sub resource)))
82+
(import "import-type-x0" (type (;1;) (eq 0)))
83+
(type (;2;) (own 1))
84+
(type (;3;) (func (result 2)))
85+
(import "import-func-f" (func (;0;) (type 3)))
86+
(export (;4;) "x" (type 0))
87+
(type (;5;) (own 4))
88+
(type (;6;) (func (result 5)))
89+
(export (;1;) "f" (func 0) (func (type 6)))
90+
)
91+
(instance (;2;) (instantiate 0
92+
(with "import-func-f" (func 1))
93+
(with "import-type-x" (type 7))
94+
(with "import-type-x0" (type 4))
95+
)
96+
)
97+
(export (;3;) "foo:bar/qux" (instance 2))
98+
(@producers
99+
(processed-by "wit-component" "0.201.0")
100+
)
101+
)
102+
(instance $component2 (;2;) (instantiate 0
103+
(with "foo:bar/baz" (instance 0))
104+
(with "foo:bar/qux" (instance 1))
105+
)
106+
)
107+
(alias export $component2 "foo:bar/qux" (instance $alias (;3;)))
108+
(component (;1;)
109+
(type (;0;)
110+
(instance
111+
(export (;0;) "x" (type (sub resource)))
112+
)
113+
)
114+
(import "foo:bar/baz" (instance (;0;) (type 0)))
115+
(alias export 0 "x" (type (;1;)))
116+
(type (;2;)
117+
(instance
118+
(alias outer 1 1 (type (;0;)))
119+
(export (;1;) "x" (type (eq 0)))
120+
(type (;2;) (own 1))
121+
(type (;3;) (func (result 2)))
122+
(export (;0;) "f" (func (type 3)))
123+
)
124+
)
125+
(import "foo:bar/qux" (instance (;1;) (type 2)))
126+
(core module (;0;)
127+
(type (;0;) (func (param i32)))
128+
(type (;1;) (func (result i32)))
129+
(type (;2;) (func (param i32 i32 i32 i32) (result i32)))
130+
(import "foo:bar/baz" "[resource-drop]x" (func (;0;) (type 0)))
131+
(import "foo:bar/qux" "f" (func (;1;) (type 1)))
132+
(func (;2;) (type 1) (result i32)
133+
unreachable
134+
)
135+
(func (;3;) (type 2) (param i32 i32 i32 i32) (result i32)
136+
unreachable
137+
)
138+
(memory (;0;) 0)
139+
(export "foo:bar/qux#f" (func 2))
140+
(export "memory" (memory 0))
141+
(export "cabi_realloc" (func 3))
142+
(@producers
143+
(processed-by "wit-component" "0.201.0")
144+
)
145+
)
146+
(alias export 0 "x" (type (;3;)))
147+
(core func (;0;) (canon resource.drop 3))
148+
(core instance (;0;)
149+
(export "[resource-drop]x" (func 0))
150+
)
151+
(alias export 1 "f" (func (;0;)))
152+
(core func (;1;) (canon lower (func 0)))
153+
(core instance (;1;)
154+
(export "f" (func 1))
155+
)
156+
(core instance (;2;) (instantiate 0
157+
(with "foo:bar/baz" (instance 0))
158+
(with "foo:bar/qux" (instance 1))
159+
)
160+
)
161+
(alias core export 2 "memory" (core memory (;0;)))
162+
(alias core export 2 "cabi_realloc" (core func (;2;)))
163+
(alias export 0 "x" (type (;4;)))
164+
(type (;5;) (own 4))
165+
(type (;6;) (func (result 5)))
166+
(alias core export 2 "foo:bar/qux#f" (core func (;3;)))
167+
(func (;1;) (type 6) (canon lift (core func 3)))
168+
(alias export 0 "x" (type (;7;)))
169+
(component (;0;)
170+
(import "import-type-x" (type (;0;) (sub resource)))
171+
(import "import-type-x0" (type (;1;) (eq 0)))
172+
(type (;2;) (own 1))
173+
(type (;3;) (func (result 2)))
174+
(import "import-func-f" (func (;0;) (type 3)))
175+
(export (;4;) "x" (type 0))
176+
(type (;5;) (own 4))
177+
(type (;6;) (func (result 5)))
178+
(export (;1;) "f" (func 0) (func (type 6)))
179+
)
180+
(instance (;2;) (instantiate 0
181+
(with "import-func-f" (func 1))
182+
(with "import-type-x" (type 7))
183+
(with "import-type-x0" (type 4))
184+
)
185+
)
186+
(export (;3;) "foo:bar/qux" (instance 2))
187+
(@producers
188+
(processed-by "wit-component" "0.201.0")
189+
)
190+
)
191+
(instance $component1 (;4;) (instantiate 1
192+
(with "foo:bar/qux" (instance $alias))
193+
(with "foo:bar/baz" (instance 0))
194+
)
195+
)
196+
(alias export $component1 "foo:bar/qux" (instance $export (;5;)))
197+
(export (;6;) "foo:bar/qux" (instance $export))
198+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "test:foo",
5+
"path": "component.wat"
6+
},
7+
{
8+
"name": "test:bar",
9+
"path": "component.wat"
10+
}
11+
],
12+
"nodes": [
13+
{
14+
"type": "instantiation",
15+
"package": 0
16+
},
17+
{
18+
"type": "instantiation",
19+
"package": 1
20+
},
21+
{
22+
"type": "alias",
23+
"source": 1,
24+
"export": "foo:bar/qux"
25+
},
26+
{
27+
"type": "alias",
28+
"source": 0,
29+
"export": "foo:bar/qux"
30+
}
31+
],
32+
"arguments": [
33+
{
34+
"source": 2,
35+
"target": 0,
36+
"name": "foo:bar/qux"
37+
}
38+
],
39+
"exports": [
40+
{
41+
"node": 3,
42+
"name": "foo:bar/qux"
43+
}
44+
],
45+
"names": [
46+
{
47+
"node": 0,
48+
"name": "component1"
49+
},
50+
{
51+
"node": 1,
52+
"name": "component2"
53+
},
54+
{
55+
"node": 2,
56+
"name": "alias"
57+
},
58+
{
59+
"node": 3,
60+
"name": "export"
61+
}
62+
]
63+
}

0 commit comments

Comments
 (0)