Skip to content

Commit b3d53d3

Browse files
authored
feat(rust): allow generate unused records and variants (#913)
* feat(rust): allow generate unused records and variants * fix: empty record * rename: allow_unused -> generate_unused_types * test(rust): make sure unused types are exported
1 parent 42f01a6 commit b3d53d3

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-5
lines changed

crates/guest-rust/macro/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ impl Parse for Config {
113113
Opt::PubExportMacro(enable) => {
114114
opts.pub_export_macro = enable.value();
115115
}
116+
Opt::GenerateUnusedTypes(enable) => {
117+
opts.generate_unused_types = enable.value();
118+
}
116119
}
117120
}
118121
} else {
@@ -231,6 +234,7 @@ mod kw {
231234
syn::custom_keyword!(default_bindings_module);
232235
syn::custom_keyword!(export_macro_name);
233236
syn::custom_keyword!(pub_export_macro);
237+
syn::custom_keyword!(generate_unused_types);
234238
}
235239

236240
#[derive(Clone)]
@@ -280,6 +284,7 @@ enum Opt {
280284
DefaultBindingsModule(syn::LitStr),
281285
ExportMacroName(syn::LitStr),
282286
PubExportMacro(syn::LitBool),
287+
GenerateUnusedTypes(syn::LitBool),
283288
}
284289

285290
impl Parse for Opt {
@@ -398,6 +403,10 @@ impl Parse for Opt {
398403
input.parse::<kw::pub_export_macro>()?;
399404
input.parse::<Token![:]>()?;
400405
Ok(Opt::PubExportMacro(input.parse()?))
406+
} else if l.peek(kw::generate_unused_types) {
407+
input.parse::<kw::generate_unused_types>()?;
408+
input.parse::<Token![:]>()?;
409+
Ok(Opt::GenerateUnusedTypes(input.parse()?))
401410
} else {
402411
Err(l.error())
403412
}

crates/guest-rust/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@
785785
/// // Disable a workaround to force wasm constructors to be run only once
786786
/// // when exported functions are called.
787787
/// disable_run_ctors_once_workaround: false,
788+
///
789+
/// // Whether to generate unused `record`, `enum`, `variant` types.
790+
/// // By default, they will not be generated unless they are used as input
791+
/// // or return value of a function.
792+
/// generate_unused_types: false,
788793
/// });
789794
/// ```
790795
///

crates/rust/src/interface.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,12 +1329,13 @@ macro_rules! {macro_name} {{
13291329

13301330
fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
13311331
let info = self.info(ty);
1332-
// If this type isn't actually used, no need to generate it.
1333-
if !info.owned && !info.borrowed {
1334-
return Vec::new();
1335-
}
13361332
let mut result = Vec::new();
1337-
1333+
if !self.gen.opts.generate_unused_types {
1334+
// If this type isn't actually used, no need to generate it.
1335+
if !info.owned && !info.borrowed {
1336+
return result;
1337+
}
1338+
}
13381339
// Generate one mode for when the type is owned and another for when
13391340
// it's borrowed.
13401341
let a = self.type_mode_for_id(ty, TypeOwnershipStyle::Owned, "'a");

crates/rust/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ pub struct Opts {
182182
/// candidate for being exported outside of the crate.
183183
#[cfg_attr(feature = "clap", arg(long))]
184184
pub pub_export_macro: bool,
185+
186+
/// Whether to generate unused structures, not generated by default (false)
187+
#[cfg_attr(feature = "clap", arg(long))]
188+
pub generate_unused_types: bool,
185189
}
186190

187191
impl Opts {

crates/rust/tests/codegen.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ mod custom_derives {
340340
use exports::my::inline::blah::Foo;
341341

342342
struct Component;
343+
343344
impl exports::my::inline::blah::Guest for Component {
344345
fn bar(cool: Foo) {
345346
// Check that built in derives that I've added actually work by seeing that this hashes
@@ -452,3 +453,34 @@ mod with_and_resources {
452453
});
453454
}
454455
}
456+
457+
#[allow(unused)]
458+
mod generate_unused_types {
459+
use exports::foo::bar::component::UnusedEnum;
460+
use exports::foo::bar::component::UnusedRecord;
461+
use exports::foo::bar::component::UnusedVariant;
462+
463+
wit_bindgen::generate!({
464+
inline: "
465+
package foo:bar;
466+
467+
world bindings {
468+
export component;
469+
}
470+
471+
interface component {
472+
variant unused-variant {
473+
%enum(unused-enum),
474+
%record(unused-record)
475+
}
476+
enum unused-enum {
477+
unused
478+
}
479+
record unused-record {
480+
x: u32
481+
}
482+
}
483+
",
484+
generate_unused_types: true,
485+
});
486+
}

tests/codegen/allow-unused.wit

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package foo:bar;
2+
3+
world bindings {
4+
export component;
5+
}
6+
7+
interface component {
8+
variant unused-variant {
9+
%enum(unused-enum),
10+
%record(unused-record)
11+
}
12+
enum unused-enum {
13+
unused
14+
}
15+
record unused-record {
16+
x: u32
17+
}
18+
}

0 commit comments

Comments
 (0)