Skip to content

Commit 2affea5

Browse files
authored
Fix text format outer aliases from recent refactor (#2631)
This commit fixes a regression from #2621 where the resolution of `$foo` names in the text format of components was refactored. Specifically after that refactoring this component no longer produced a valid binary: (component $A (type $a string) (component $B (type (instance (export "a" (type (eq $a))))) (type $b (record (field "x" $a))) ) ) The reason for this is that `$a` is used twice as implicitly-injected outer aliases here, but the higher-depth one comes first. The previous behavior would leave the `$a` name on the injected outer alias type, which then caused the resolution of `(eq $a)` to resolve to the `$a` within the definition of `$B` (despite it being later and invalid to reference), instead of the definition within `$A` which was the target. The fix here is to leave off the name in the injected outer alias. This preserves the behavior pre-#2621 which was accidentally removed, and causes the above component to generate a valid binary instead of an invalid binary. This has various bits and pieces of impact throughout tests which now resolve slightly differently than before, or are missing a `$foo` name, as expected.
1 parent b52f16c commit 2affea5

20 files changed

Lines changed: 269 additions & 251 deletions

File tree

crates/wast/src/component/resolve.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,10 @@ impl<'a> Resolver<'a> {
964964
// When resolution succeeds in a parent then an outer alias
965965
// is automatically inserted here in this component.
966966
let span = idx.span();
967+
let alias_id = gensym::generate(span);
967968
let alias = Alias {
968969
span,
969-
id: Some(id),
970+
id: Some(alias_id),
970971
name: None,
971972
target: AliasTarget::Outer {
972973
outer: Index::Num(depth, span),
@@ -993,6 +994,7 @@ impl<'a> Resolver<'a> {
993994
self.current().register_alias(&alias)?;
994995

995996
self.aliases_to_insert.push(alias);
997+
*idx = Index::Id(alias_id);
996998
break;
997999
}
9981000
}

tests/cli/component-model/alias.wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,12 @@
335335
(canon lower (func $c "f") (core func $lowered))
336336
)
337337
)
338+
339+
(component
340+
(type $a string)
341+
342+
(component
343+
(type (instance (export "a" (type (eq $a)))))
344+
(type $b (record (field "x" $a)))
345+
)
346+
)

tests/cli/dump/alias2.wat.stdout

Lines changed: 112 additions & 115 deletions
Large diffs are not rendered by default.

tests/cli/dump/component-outer-alias.wat.stdout

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
0xc | 41 02 02 03 | [type 1] Component([Alias(Outer { kind: Type, count: 1, index: 0 }), Type(Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) }))])
77
| 02 01 00 01
88
| 40 00 00 00
9-
0x18 | 04 2e | [component 0] inline size
9+
0x18 | 04 16 | [component 0] inline size
1010
0x1a | 00 61 73 6d | version 13 (Component)
1111
| 0d 00 01 00
1212
0x22 | 06 05 | component alias section
@@ -15,59 +15,45 @@
1515
0x29 | 07 05 | component type section
1616
0x2b | 01 | 1 count
1717
0x2c | 40 00 00 00 | [type 1] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
18-
0x30 | 00 16 | custom section
19-
0x32 | 0e 63 6f 6d | name: "component-name"
20-
| 70 6f 6e 65
21-
| 6e 74 2d 6e
22-
| 61 6d 65
23-
0x41 | 01 05 03 | type name section
24-
0x44 | 01 | 1 count
25-
0x45 | 00 01 74 | Naming { index: 0, name: "t" }
26-
0x48 | 04 32 | [component 1] inline size
27-
0x4a | 00 61 73 6d | version 13 (Component)
18+
0x30 | 04 24 | [component 1] inline size
19+
0x32 | 00 61 73 6d | version 13 (Component)
2820
| 0d 00 01 00
29-
0x52 | 06 05 | component alias section
30-
0x54 | 01 | 1 count
31-
0x55 | 03 02 01 00 | alias [type 0] Outer { kind: Type, count: 1, index: 0 }
32-
0x59 | 07 09 | component type section
33-
0x5b | 02 | 2 count
34-
0x5c | 40 00 00 00 | [type 1] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
35-
0x60 | 40 00 00 00 | [type 2] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
36-
0x64 | 00 16 | custom section
37-
0x66 | 0e 63 6f 6d | name: "component-name"
38-
| 70 6f 6e 65
39-
| 6e 74 2d 6e
40-
| 61 6d 65
41-
0x75 | 01 05 03 | type name section
42-
0x78 | 01 | 1 count
43-
0x79 | 00 01 74 | Naming { index: 0, name: "t" }
44-
0x7c | 07 02 | component type section
45-
0x7e | 01 | 1 count
46-
0x7f | 7d | [type 2] Defined(Primitive(U8))
47-
0x80 | 04 33 | [component 2] inline size
48-
0x82 | 00 61 73 6d | version 13 (Component)
21+
0x3a | 06 05 | component alias section
22+
0x3c | 01 | 1 count
23+
0x3d | 03 02 01 00 | alias [type 0] Outer { kind: Type, count: 1, index: 0 }
24+
0x41 | 07 05 | component type section
25+
0x43 | 01 | 1 count
26+
0x44 | 40 00 00 00 | [type 1] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
27+
0x48 | 06 05 | component alias section
28+
0x4a | 01 | 1 count
29+
0x4b | 03 02 01 00 | alias [type 2] Outer { kind: Type, count: 1, index: 0 }
30+
0x4f | 07 05 | component type section
31+
0x51 | 01 | 1 count
32+
0x52 | 40 00 00 02 | [type 3] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(2)) })
33+
0x56 | 07 02 | component type section
34+
0x58 | 01 | 1 count
35+
0x59 | 7d | [type 2] Defined(Primitive(U8))
36+
0x5a | 04 24 | [component 2] inline size
37+
0x5c | 00 61 73 6d | version 13 (Component)
4938
| 0d 00 01 00
50-
0x8a | 06 05 | component alias section
51-
0x8c | 01 | 1 count
52-
0x8d | 03 02 01 02 | alias [type 0] Outer { kind: Type, count: 1, index: 2 }
53-
0x91 | 07 09 | component type section
54-
0x93 | 02 | 2 count
55-
0x94 | 40 00 00 00 | [type 1] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
56-
0x98 | 40 00 00 00 | [type 2] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
57-
0x9c | 00 17 | custom section
58-
0x9e | 0e 63 6f 6d | name: "component-name"
59-
| 70 6f 6e 65
60-
| 6e 74 2d 6e
61-
| 61 6d 65
62-
0xad | 01 06 03 | type name section
63-
0xb0 | 01 | 1 count
64-
0xb1 | 00 02 74 32 | Naming { index: 0, name: "t2" }
65-
0xb5 | 00 1a | custom section
66-
0xb7 | 0e 63 6f 6d | name: "component-name"
39+
0x64 | 06 05 | component alias section
40+
0x66 | 01 | 1 count
41+
0x67 | 03 02 01 02 | alias [type 0] Outer { kind: Type, count: 1, index: 2 }
42+
0x6b | 07 05 | component type section
43+
0x6d | 01 | 1 count
44+
0x6e | 40 00 00 00 | [type 1] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(0)) })
45+
0x72 | 06 05 | component alias section
46+
0x74 | 01 | 1 count
47+
0x75 | 03 02 01 02 | alias [type 2] Outer { kind: Type, count: 1, index: 2 }
48+
0x79 | 07 05 | component type section
49+
0x7b | 01 | 1 count
50+
0x7c | 40 00 00 02 | [type 3] Func(ComponentFuncType { async_: false, params: [], result: Some(Type(2)) })
51+
0x80 | 00 1a | custom section
52+
0x82 | 0e 63 6f 6d | name: "component-name"
6753
| 70 6f 6e 65
6854
| 6e 74 2d 6e
6955
| 61 6d 65
70-
0xc6 | 01 09 03 | type name section
71-
0xc9 | 02 | 2 count
72-
0xca | 00 01 74 | Naming { index: 0, name: "t" }
73-
0xcd | 02 02 74 32 | Naming { index: 2, name: "t2" }
56+
0x91 | 01 09 03 | type name section
57+
0x94 | 02 | 2 count
58+
0x95 | 00 01 74 | Naming { index: 0, name: "t" }
59+
0x98 | 02 02 74 32 | Naming { index: 2, name: "t2" }

tests/snapshots/cli/component-model/alias.wast.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@
202202
"line": 325,
203203
"filename": "alias.30.wasm",
204204
"module_type": "binary"
205+
},
206+
{
207+
"type": "module",
208+
"line": 339,
209+
"filename": "alias.31.wasm",
210+
"module_type": "binary"
205211
}
206212
]
207213
}

tests/snapshots/cli/component-model/alias.wast/12.print

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(component $PARENT
22
(type $t (;0;) (func (result string)))
33
(component (;0;)
4-
(alias outer $PARENT $t (type $t (;0;)))
5-
(import "a" (func (;0;) (type $t)))
4+
(alias outer $PARENT $t (type (;0;)))
5+
(import "a" (func (;0;) (type 0)))
66
)
77
(component (;1;)
88
(alias outer $PARENT $t (type $my_type (;0;)))

tests/snapshots/cli/component-model/alias.wast/13.print

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
(component (;0;)
66
(type $c (;0;) (func (result s32)))
77
(component (;0;)
8-
(alias outer 3 $a (type $a (;0;)))
9-
(import "a" (func $a (;0;) (type $a)))
10-
(alias outer 2 $b (type $b (;1;)))
11-
(import "b" (func $b (;1;) (type $b)))
12-
(alias outer 1 $c (type $c (;2;)))
13-
(import "c" (func $c (;2;) (type $c)))
8+
(alias outer 3 $a (type (;0;)))
9+
(import "a" (func $a (;0;) (type 0)))
10+
(alias outer 2 $b (type (;1;)))
11+
(import "b" (func $b (;1;) (type 1)))
12+
(alias outer 1 $c (type (;2;)))
13+
(import "c" (func $c (;2;) (type 2)))
1414
(type (;3;)
1515
(component
1616
(type (;0;) (func (result string)))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(component
2+
(type $a (;0;) string)
3+
(component (;0;)
4+
(type (;0;)
5+
(instance
6+
(alias outer 2 $a (type (;0;)))
7+
(export (;1;) "a" (type (eq 0)))
8+
)
9+
)
10+
(alias outer 1 $a (type (;1;)))
11+
(type $b (;2;) (record (field "x" 1)))
12+
)
13+
)

tests/snapshots/cli/component-model/func.wast/6.print

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(component
22
(type $big (;0;) (func (param "p1" u32) (param "p2" u32) (param "p3" u32) (param "p4" u32) (param "p5" u32) (param "p6" u32) (param "p7" u32) (param "p8" u32) (param "p9" u32) (param "p10" u32) (param "p11" u32) (param "p12" u32) (param "p13" u32) (param "p14" u32) (param "p15" u32) (param "p16" u32) (param "p17" u32) (param "p18" u32) (param "p19" u32) (param "p20" u32)))
33
(component $c (;0;)
4-
(alias outer 1 $big (type $big (;0;)))
5-
(import "big" (func $big (;0;) (type $big)))
4+
(alias outer 1 $big (type (;0;)))
5+
(import "big" (func $big (;0;) (type 0)))
66
(core module $libc (;0;)
77
(memory (;0;) 1)
88
(export "memory" (memory 0))

tests/snapshots/cli/component-model/instance-type.wast/0.print

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,31 @@
4646
(export (;6;) "f" (func (type 6)))
4747
(type (;7;) (func (param "g" s32) (result u32)))
4848
(export (;7;) "g" (func (type 7)))
49-
(export (;8;) "h" (func (type 5)))
50-
(type (;8;)
51-
(component)
52-
)
49+
(alias outer 1 $t (type (;8;)))
50+
(export (;8;) "h" (func (type 8)))
5351
(type (;9;)
5452
(component)
5553
)
56-
(export (;0;) "c1" (component (type 9)))
5754
(type (;10;)
55+
(component)
56+
)
57+
(export (;0;) "c1" (component (type 10)))
58+
(type (;11;)
5859
(component
5960
(type (;0;) (func))
6061
(import "i1" (func (;0;) (type 0)))
6162
)
6263
)
63-
(export (;1;) "c2" (component (type 10)))
64-
(type (;11;)
64+
(export (;1;) "c2" (component (type 11)))
65+
(type (;12;)
6566
(component
6667
(type (;0;) (func))
6768
(export (;0;) "e1" (func (type 0)))
6869
)
6970
)
70-
(export (;2;) "c3" (component (type 11)))
71-
(export (;3;) "c4" (component (type 8)))
72-
(type (;12;)
71+
(export (;2;) "c3" (component (type 12)))
72+
(export (;3;) "c4" (component (type 9)))
73+
(type (;13;)
7374
(component
7475
(type (;0;) (func))
7576
(alias outer 1 0 (type (;1;)))
@@ -85,7 +86,7 @@
8586
(export (;1;) "e2" (component (type 3)))
8687
)
8788
)
88-
(export (;4;) "c5" (component (type 12)))
89+
(export (;4;) "c5" (component (type 13)))
8990
)
9091
)
9192
)

0 commit comments

Comments
 (0)