Skip to content

Commit e5132af

Browse files
authored
Extract "world elaboration" to a separate function (#1800)
* Extract "world elaboration" to a separate function This commit extracts the process of elaborating a worlds imports/exports to a dedicated function. This function ensures the transitive closure of all imports/exports are listed in the world in proper topograhical order. This additionally validates that the world has a coherent definition given WIT today, notably that imports don't accidentally try to use exported types. This functionality was all present previously during the process of taking a WIT document and creating `Resolve`. This moves the logic around and refactors it slightly given its new surroundings. This reorders a few imports/exports in worlds from what was previously present but the underlying meaning of worlds should be the same. The goal of this commit is to enable `Resolve`-mutating transformations to not need to preserve all these invariants each time a world is modified. Instead a world can be modified and then this function can be called to "clean up" the world afterwards. * Simplify the `Resolve::importize` operation This commit updates the `importize` operation to be a much simpler "just move the exports to the imports" operation where care is only taken to preserve imported types and to additionally error on overlap of imported types and exported functions/interfaces. The previous `elaborate_world` method helps make this method much simpler than before.
1 parent 5a83828 commit e5132af

19 files changed

Lines changed: 397 additions & 455 deletions

crates/wit-component/tests/interfaces/resources.wat

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,6 @@
100100
(type (;0;)
101101
(component
102102
(type (;0;)
103-
(instance
104-
(export (;0;) "a" (type (sub resource)))
105-
(type (;1;) (own 0))
106-
(type (;2;) (func (result 1)))
107-
(export (;0;) "[constructor]a" (func (type 2)))
108-
(type (;3;) (func))
109-
(export (;1;) "[static]a.b" (func (type 3)))
110-
(type (;4;) (borrow 0))
111-
(type (;5;) (func (param "self" 4)))
112-
(export (;2;) "[method]a.c" (func (type 5)))
113-
)
114-
)
115-
(import "anon" (instance (;0;) (type 0)))
116-
(type (;1;)
117103
(instance
118104
(export (;0;) "bar" (type (sub resource)))
119105
(export (;1;) "t" (type (eq 0)))
@@ -131,14 +117,14 @@
131117
(export (;3;) "a" (func (type 9)))
132118
)
133119
)
134-
(import "foo:bar/foo" (instance (;1;) (type 1)))
135-
(alias export 1 "bar" (type (;2;)))
136-
(alias export 1 "t" (type (;3;)))
137-
(type (;4;)
120+
(import "foo:bar/foo" (instance (;0;) (type 0)))
121+
(alias export 0 "bar" (type (;1;)))
122+
(alias export 0 "t" (type (;2;)))
123+
(type (;3;)
138124
(instance
139-
(alias outer 1 2 (type (;0;)))
125+
(alias outer 1 1 (type (;0;)))
140126
(export (;1;) "bar" (type (eq 0)))
141-
(alias outer 1 3 (type (;2;)))
127+
(alias outer 1 2 (type (;2;)))
142128
(export (;3;) "t" (type (eq 2)))
143129
(type (;4;) (own 1))
144130
(type (;5;) (func (param "x" 4) (result 4)))
@@ -148,15 +134,29 @@
148134
(export (;1;) "b" (func (type 7)))
149135
)
150136
)
151-
(import "foo:bar/baz" (instance (;2;) (type 4)))
152-
(alias export 2 "bar" (type (;5;)))
153-
(import "bar" (type (;6;) (eq 5)))
154-
(import "a" (type (;7;) (sub resource)))
155-
(type (;8;) (own 7))
137+
(import "foo:bar/baz" (instance (;1;) (type 3)))
138+
(alias export 1 "bar" (type (;4;)))
139+
(import "bar" (type (;5;) (eq 4)))
140+
(import "a" (type (;6;) (sub resource)))
141+
(type (;7;)
142+
(instance
143+
(export (;0;) "a" (type (sub resource)))
144+
(type (;1;) (own 0))
145+
(type (;2;) (func (result 1)))
146+
(export (;0;) "[constructor]a" (func (type 2)))
147+
(type (;3;) (func))
148+
(export (;1;) "[static]a.b" (func (type 3)))
149+
(type (;4;) (borrow 0))
150+
(type (;5;) (func (param "self" 4)))
151+
(export (;2;) "[method]a.c" (func (type 5)))
152+
)
153+
)
154+
(import "anon" (instance (;2;) (type 7)))
155+
(type (;8;) (own 6))
156156
(type (;9;) (func (result 8)))
157157
(import "[constructor]a" (func (;0;) (type 9)))
158158
(export (;1;) "x" (func (type 9)))
159-
(type (;10;) (own 6))
159+
(type (;10;) (own 5))
160160
(type (;11;) (func (result 10)))
161161
(export (;2;) "y" (func (type 11)))
162162
)

crates/wit-component/tests/interfaces/resources.wit.print

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ interface implicit-own-handles2 {
5050
}
5151

5252
world some-world {
53+
import foo;
54+
import baz;
5355
import anon: interface {
5456
resource a {
5557
constructor();
5658
b: static func();
5759
c: func();
5860
}
5961
}
60-
import foo;
61-
import baz;
6262
use baz.{bar};
6363

6464
resource a {

crates/wit-component/tests/interfaces/world-top-level.wat

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
(component
44
(type (;0;)
55
(component
6-
(type (;0;)
6+
(type (;0;) (func))
7+
(import "foo" (func (;0;) (type 0)))
8+
(type (;1;) (func (param "arg" u32)))
9+
(import "bar" (func (;1;) (type 1)))
10+
(type (;2;)
711
(instance)
812
)
9-
(import "some-interface" (instance (;0;) (type 0)))
10-
(type (;1;) (func))
11-
(import "foo" (func (;0;) (type 1)))
12-
(type (;2;) (func (param "arg" u32)))
13-
(import "bar" (func (;1;) (type 2)))
14-
(export (;2;) "foo2" (func (type 1)))
13+
(import "some-interface" (instance (;0;) (type 2)))
14+
(export (;2;) "foo2" (func (type 0)))
1515
(type (;3;) (func (result u32)))
1616
(export (;3;) "bar2" (func (type 3)))
1717
(type (;4;)

crates/wit-component/tests/interfaces/world-top-level.wit.print

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package foo:foo;
22

33
world foo {
4-
import some-interface: interface {
5-
}
64
import foo: func();
75
import bar: func(arg: u32);
6+
import some-interface: interface {
7+
}
88

99
export foo2: func();
1010
export bar2: func() -> u32;

0 commit comments

Comments
 (0)