Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed Mar 31, 2024
2 parents 4f6082d + b3d53d3 commit bf39591
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 @@ -116,6 +116,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 @@ -235,6 +238,7 @@ mod kw {
syn::custom_keyword!(export_macro_name);
syn::custom_keyword!(pub_export_macro);
syn::custom_keyword!(wasm64);
syn::custom_keyword!(generate_unused_types);
}

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

impl Parse for Opt {
Expand Down Expand Up @@ -406,6 +411,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 @@ -1354,12 +1354,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 @@ -186,6 +186,10 @@ pub struct Opts {
/// Generate code for 64bit wasm
#[cfg_attr(feature = "clap", arg(long))]
pub wasm64: 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 bf39591

Please sign in to comment.