Skip to content

Commit 90031e8

Browse files
authored
Redefine with in the Rust generator to only apply to imports (#1367)
This commit avoids looking at `--with` for exports, for example, to fix situations where an interface is imported and exported and only the import types should be reused from elsewhere. I'm not aware of any cases where exports want to be reused, so there's effectively no more support for that.
1 parent 7bb0545 commit 90031e8

File tree

9 files changed

+113
-58
lines changed

9 files changed

+113
-58
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ jobs:
152152
- run: cargo test
153153
- run: cargo test -p wit-bindgen-core
154154
- run: cargo test -p wit-bindgen
155+
- run: cargo test -p wit-bindgen-rust
155156
- run: cargo test --workspace --exclude 'wit-bindgen*'
156157
- run: cargo test -p wit-bindgen-rt --all-features
157158
- run: rustup update nightly --no-self-update

crates/rust/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ pub struct Opts {
221221
#[cfg_attr(feature = "clap", arg(long, value_name = "NAME"))]
222222
pub additional_derive_ignore: Vec<String>,
223223

224-
/// Remapping of wit interface and type names to Rust module names and types.
224+
/// Remapping of wit import interface and type names to Rust module names
225+
/// and types.
225226
///
226227
/// Argument must be of the form `k=v` and this option can be passed
227228
/// multiple times or one option can be comma separated, for example
@@ -384,8 +385,13 @@ impl RustWasm {
384385
is_export: bool,
385386
) -> Result<bool> {
386387
let with_name = resolve.name_world_key(name);
387-
let Some(remapping) = self.with.get(&with_name) else {
388-
bail!(MissingWith(with_name));
388+
let remapping = if is_export {
389+
&TypeGeneration::Generate
390+
} else {
391+
match self.with.get(&with_name) {
392+
Some(remapping) => remapping,
393+
None => bail!(MissingWith(with_name)),
394+
}
389395
};
390396
self.generated_types.insert(with_name);
391397
let entry = match remapping {
@@ -1066,8 +1072,9 @@ impl WorldGenerator for RustWasm {
10661072
self.world = Some(world);
10671073

10681074
let world = &resolve.worlds[world];
1069-
// Specify that all imports local to the world's package should be generated
1070-
for (key, item) in world.imports.iter().chain(world.exports.iter()) {
1075+
// Specify that all imports local to the world's package should be
1076+
// generated
1077+
for (key, item) in world.imports.iter() {
10711078
if let WorldItem::Interface { id, .. } = item {
10721079
if resolve.interfaces[*id].package == world.package {
10731080
let name = resolve.name_world_key(key);
@@ -1158,14 +1165,7 @@ impl WorldGenerator for RustWasm {
11581165
let mut to_define = Vec::new();
11591166
for (name, ty_id) in resolve.interfaces[id].types.iter() {
11601167
let full_name = full_wit_type_name(resolve, *ty_id);
1161-
if let Some(type_gen) = self.with.get(&full_name) {
1162-
// skip type definition generation for remapped types
1163-
if type_gen.generated() {
1164-
to_define.push((name, ty_id));
1165-
}
1166-
} else {
1167-
to_define.push((name, ty_id));
1168-
}
1168+
to_define.push((name, ty_id));
11691169
self.generated_types.insert(full_name);
11701170
}
11711171

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package test:inline-and-path;
2+
3+
interface bar {}

crates/test/src/c.rs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ pub struct COpts {
2121

2222
pub struct C;
2323

24-
pub struct Cpp;
25-
2624
/// C/C++-specific configuration of component files
2725
#[derive(Default, Deserialize)]
2826
#[serde(deny_unknown_fields)]
@@ -40,14 +38,6 @@ fn clang(runner: &Runner<'_>) -> PathBuf {
4038
}
4139
}
4240

43-
fn clangpp(runner: &Runner<'_>) -> PathBuf {
44-
let target = &runner.opts.c.c_target;
45-
match &runner.opts.c.wasi_sdk_path {
46-
Some(path) => path.join(format!("bin/{target}-clang++")),
47-
None => format!("{target}-clang++").into(),
48-
}
49-
}
50-
5141
impl LanguageMethods for C {
5242
fn display(&self) -> &str {
5343
"c"
@@ -87,41 +77,6 @@ impl LanguageMethods for C {
8777
}
8878
}
8979

90-
impl LanguageMethods for Cpp {
91-
fn display(&self) -> &str {
92-
"cpp"
93-
}
94-
95-
fn bindgen_name(&self) -> Option<&str> {
96-
Some("c")
97-
}
98-
99-
fn comment_prefix_for_test_config(&self) -> Option<&str> {
100-
Some("//@")
101-
}
102-
103-
fn should_fail_verify(
104-
&self,
105-
name: &str,
106-
config: &crate::config::WitConfig,
107-
args: &[String],
108-
) -> bool {
109-
C.should_fail_verify(name, config, args)
110-
}
111-
112-
fn prepare(&self, runner: &mut Runner<'_>) -> Result<()> {
113-
prepare(runner, clangpp(runner))
114-
}
115-
116-
fn compile(&self, runner: &Runner<'_>, c: &Compile<'_>) -> Result<()> {
117-
compile(runner, c, clangpp(runner))
118-
}
119-
120-
fn verify(&self, runner: &Runner<'_>, v: &Verify<'_>) -> Result<()> {
121-
verify(runner, v, clangpp(runner))
122-
}
123-
}
124-
12580
fn prepare(runner: &mut Runner<'_>, compiler: PathBuf) -> Result<()> {
12681
let cwd = env::current_dir()?;
12782
let dir = cwd.join(&runner.opts.artifacts).join("c");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package example:composition;
2+
3+
let leaf = new test:leaf { ... };
4+
let intermediate = new test:test {
5+
foo: leaf.foo,
6+
...
7+
};
8+
let runner = new test:runner {
9+
foo: intermediate.foo,
10+
...
11+
};
12+
13+
export runner...;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include!(env!("BINDINGS"));
2+
3+
struct Component;
4+
5+
export!(Component);
6+
7+
use crate::exports::my::inline::foo::{Guest, A};
8+
9+
impl Guest for Component {
10+
fn bar(a: A) {
11+
assert_eq!(a.b, 2);
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include!(env!("BINDINGS"));
2+
3+
fn main() {
4+
my::inline::foo::bar(my::inline::foo::A { b: 2 });
5+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//@ args = '--with my:inline/foo=other::my::inline::foo'
2+
3+
#![expect(
4+
unused_imports,
5+
reason = "using `with` is known to produce possibly dead imports"
6+
)]
7+
8+
include!(env!("BINDINGS"));
9+
10+
mod other {
11+
wit_bindgen::generate!({
12+
inline: "
13+
package my:inline;
14+
15+
interface foo {
16+
record a { b: u8 }
17+
18+
bar: func(a: a);
19+
}
20+
21+
world gen {
22+
import foo;
23+
}
24+
",
25+
});
26+
}
27+
28+
struct Component;
29+
30+
export!(Component);
31+
32+
use crate::exports::my::inline::foo::{Guest, A};
33+
use std::any::TypeId;
34+
35+
impl Guest for Component {
36+
fn bar(a: A) {
37+
assert!(TypeId::of::<A>() != TypeId::of::<other::my::inline::foo::A>());
38+
other::my::inline::foo::bar(other::my::inline::foo::A { b: a.b })
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ dependencies = ['test', 'leaf']
2+
//@ wac = 'compose.wac'
3+
4+
package my:inline;
5+
6+
interface foo {
7+
record a {
8+
b: u8,
9+
}
10+
11+
bar: func(a: a);
12+
}
13+
14+
world leaf {
15+
export foo;
16+
}
17+
18+
world test {
19+
import foo;
20+
export foo;
21+
}
22+
23+
world runner {
24+
import foo;
25+
}

0 commit comments

Comments
 (0)