Skip to content

Commit cd59db5

Browse files
authored
Fix bindings generation with unused types in Rust (#530)
* Fix bindings generation with unused types in Rust This commit fixes an issue where if a type was imported into an interface but wasn't actually used anywhere then bindings weren't correctly generated because inference about whether it was owned or borrowed didn't run. The fix here is to skip generating bindings for unused types since they're not actually needed anywhere anyway. If necessary in the future bindings can be forcibly generated but for now this is hopefully largely "just" fixing an edge case. * Ignore the new tests for Go
1 parent 39030f9 commit cd59db5

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

crates/core/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ impl Types {
166166
}
167167
TypeDefKind::Unknown => unreachable!(),
168168
}
169-
self.type_info.insert(ty, info);
169+
let prev = self.type_info.insert(ty, info);
170+
assert!(prev.is_none());
170171
info
171172
}
172173

crates/go/tests/codegen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::process::Command;
77
macro_rules! codegen_test {
88
// TODO: should fix this test
99
(lift_lower_foreign $name:tt $test:tt) => {};
10+
(unused_import $name:tt $test:tt) => {};
1011

1112
($id:ident $name:tt $test:tt) => {
1213
#[test]

crates/rust-lib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ pub trait RustGenerator<'a> {
381381

382382
fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
383383
let info = self.info(ty);
384+
if !info.owned && !info.borrowed {
385+
return Vec::new();
386+
}
384387
let mut result = Vec::new();
385388
let first_mode = if info.owned || !info.borrowed {
386389
TypeMode::Owned

tests/codegen/unused-import.wit

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
interface types {
2+
record r {
3+
a: list<u8>,
4+
}
5+
}
6+
7+
default world the-world {
8+
import foo: interface {
9+
use self.types.{r}
10+
11+
foo: func(data: r)
12+
}
13+
14+
export bar: interface {
15+
use self.types.{r}
16+
}
17+
}

0 commit comments

Comments
 (0)