Skip to content

Commit 1ca522a

Browse files
authored
Fix creating components with only static methods on resources (#1530)
Previously the resource itself wasn't imported which meant that the `[static]` function did not pass validation. This commit updates the liveness of these functions to include the resource that the static function is attached to which ensures that the type is emitted during component construction. Closes #1529
1 parent df542a8 commit 1ca522a

5 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(component
2+
(type (;0;)
3+
(instance
4+
(export (;0;) "a" (type (sub resource)))
5+
(type (;1;) (func))
6+
(export (;0;) "[static]a.f" (func (type 1)))
7+
)
8+
)
9+
(import "foo:bar/x" (instance (;0;) (type 0)))
10+
(core module (;0;)
11+
(type (;0;) (func))
12+
(import "foo:bar/x" "[static]a.f" (func (;0;) (type 0)))
13+
(@producers
14+
(processed-by "wit-component" "$CARGO_PKG_VERSION")
15+
(processed-by "my-fake-bindgen" "123.45")
16+
)
17+
)
18+
(alias export 0 "[static]a.f" (func (;0;)))
19+
(core func (;0;) (canon lower (func 0)))
20+
(core instance (;0;)
21+
(export "[static]a.f" (func 0))
22+
)
23+
(core instance (;1;) (instantiate 0
24+
(with "foo:bar/x" (instance 0))
25+
)
26+
)
27+
(@producers
28+
(processed-by "wit-component" "$CARGO_PKG_VERSION")
29+
)
30+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package root:component;
2+
3+
world root {
4+
import foo:bar/x;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(module
2+
(import "foo:bar/x" "[static]a.f" (func))
3+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package foo:bar;
2+
3+
interface x {
4+
resource a {
5+
f: static func();
6+
}
7+
}
8+
9+
world module {
10+
import x;
11+
}

crates/wit-parser/src/live.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{Function, InterfaceId, Resolve, Type, TypeDefKind, TypeId, WorldId, WorldItem};
1+
use crate::{
2+
Function, FunctionKind, InterfaceId, Resolve, Type, TypeDefKind, TypeId, WorldId, WorldItem,
3+
};
24
use indexmap::IndexSet;
35

46
#[derive(Default)]
@@ -41,6 +43,19 @@ impl LiveTypes {
4143
}
4244

4345
pub fn add_func(&mut self, resolve: &Resolve, func: &Function) {
46+
match func.kind {
47+
// This resource is live as it's attached to a static method but
48+
// it's not guaranteed to be present in either params or results, so
49+
// be sure to attach it here.
50+
FunctionKind::Static(id) => self.add_type_id(resolve, id),
51+
52+
// The resource these are attached to is in the params/results, so
53+
// no need to re-add it here.
54+
FunctionKind::Method(_) | FunctionKind::Constructor(_) => {}
55+
56+
FunctionKind::Freestanding => {}
57+
}
58+
4459
for (_, ty) in func.params.iter() {
4560
self.add_type(resolve, ty);
4661
}

0 commit comments

Comments
 (0)