Skip to content

Commit

Permalink
feat(rust): allow generate unused records and variants (#913)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
oovm committed Mar 31, 2024
1 parent 42f01a6 commit b3d53d3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
9 changes: 9 additions & 0 deletions crates/guest-rust/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ impl Parse for Config {
Opt::PubExportMacro(enable) => {
opts.pub_export_macro = enable.value();
}
Opt::GenerateUnusedTypes(enable) => {
opts.generate_unused_types = enable.value();
}
}
}
} else {
Expand Down Expand Up @@ -231,6 +234,7 @@ mod kw {
syn::custom_keyword!(default_bindings_module);
syn::custom_keyword!(export_macro_name);
syn::custom_keyword!(pub_export_macro);
syn::custom_keyword!(generate_unused_types);
}

#[derive(Clone)]
Expand Down Expand Up @@ -280,6 +284,7 @@ enum Opt {
DefaultBindingsModule(syn::LitStr),
ExportMacroName(syn::LitStr),
PubExportMacro(syn::LitBool),
GenerateUnusedTypes(syn::LitBool),
}

impl Parse for Opt {
Expand Down Expand Up @@ -398,6 +403,10 @@ impl Parse for Opt {
input.parse::<kw::pub_export_macro>()?;
input.parse::<Token![:]>()?;
Ok(Opt::PubExportMacro(input.parse()?))
} else if l.peek(kw::generate_unused_types) {
input.parse::<kw::generate_unused_types>()?;
input.parse::<Token![:]>()?;
Ok(Opt::GenerateUnusedTypes(input.parse()?))
} else {
Err(l.error())
}
Expand Down
5 changes: 5 additions & 0 deletions crates/guest-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@
/// // Disable a workaround to force wasm constructors to be run only once
/// // when exported functions are called.
/// disable_run_ctors_once_workaround: false,
///
/// // Whether to generate unused `record`, `enum`, `variant` types.
/// // By default, they will not be generated unless they are used as input
/// // or return value of a function.
/// generate_unused_types: false,
/// });
/// ```
///
Expand Down
11 changes: 6 additions & 5 deletions crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,12 +1329,13 @@ macro_rules! {macro_name} {{

fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> {
let info = self.info(ty);
// If this type isn't actually used, no need to generate it.
if !info.owned && !info.borrowed {
return Vec::new();
}
let mut result = Vec::new();

if !self.gen.opts.generate_unused_types {
// If this type isn't actually used, no need to generate it.
if !info.owned && !info.borrowed {
return result;
}
}
// Generate one mode for when the type is owned and another for when
// it's borrowed.
let a = self.type_mode_for_id(ty, TypeOwnershipStyle::Owned, "'a");
Expand Down
4 changes: 4 additions & 0 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ pub struct Opts {
/// candidate for being exported outside of the crate.
#[cfg_attr(feature = "clap", arg(long))]
pub pub_export_macro: bool,

/// Whether to generate unused structures, not generated by default (false)
#[cfg_attr(feature = "clap", arg(long))]
pub generate_unused_types: bool,
}

impl Opts {
Expand Down
32 changes: 32 additions & 0 deletions crates/rust/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ mod custom_derives {
use exports::my::inline::blah::Foo;

struct Component;

impl exports::my::inline::blah::Guest for Component {
fn bar(cool: Foo) {
// Check that built in derives that I've added actually work by seeing that this hashes
Expand Down Expand Up @@ -452,3 +453,34 @@ mod with_and_resources {
});
}
}

#[allow(unused)]
mod generate_unused_types {
use exports::foo::bar::component::UnusedEnum;
use exports::foo::bar::component::UnusedRecord;
use exports::foo::bar::component::UnusedVariant;

wit_bindgen::generate!({
inline: "
package foo:bar;
world bindings {
export component;
}
interface component {
variant unused-variant {
%enum(unused-enum),
%record(unused-record)
}
enum unused-enum {
unused
}
record unused-record {
x: u32
}
}
",
generate_unused_types: true,
});
}
18 changes: 18 additions & 0 deletions tests/codegen/allow-unused.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package foo:bar;

world bindings {
export component;
}

interface component {
variant unused-variant {
%enum(unused-enum),
%record(unused-record)
}
enum unused-enum {
unused
}
record unused-record {
x: u32
}
}

0 comments on commit b3d53d3

Please sign in to comment.