diff --git a/crates/cli-support/src/wit/mod.rs b/crates/cli-support/src/wit/mod.rs index 1991812f1f3..38402aafe9d 100644 --- a/crates/cli-support/src/wit/mod.rs +++ b/crates/cli-support/src/wit/mod.rs @@ -485,27 +485,7 @@ impl<'a> Context<'a> { let class = class.to_string(); match export.method_kind { decode::MethodKind::Constructor => { - let msg = "Trying to return JS primitive from constructor, return value will be ignored. Use a builder instead (remove `constructor` attribute)."; - match descriptor.ret { - Descriptor::I8 - | Descriptor::U8 - | Descriptor::ClampedU8 - | Descriptor::I16 - | Descriptor::U16 - | Descriptor::I32 - | Descriptor::U32 - | Descriptor::F32 - | Descriptor::F64 - | Descriptor::I64 - | Descriptor::U64 - | Descriptor::Boolean - | Descriptor::Char - | Descriptor::CachedString - | Descriptor::String - | Descriptor::Option(_) - | Descriptor::Unit => bail!(msg), - _ => {} - } + verify_constructor_return(&class, &descriptor.ret)?; AuxExportKind::Constructor(class) } decode::MethodKind::Operation(op) => { @@ -1447,6 +1427,36 @@ impl<'a> Context<'a> { } } +/// Verifies exported constructor return value is not a JS primitive type +fn verify_constructor_return(class: &str, ret: &Descriptor) -> Result<(), Error> { + match ret { + Descriptor::I8 + | Descriptor::U8 + | Descriptor::ClampedU8 + | Descriptor::I16 + | Descriptor::U16 + | Descriptor::I32 + | Descriptor::U32 + | Descriptor::F32 + | Descriptor::F64 + | Descriptor::I64 + | Descriptor::U64 + | Descriptor::Boolean + | Descriptor::Char + | Descriptor::CachedString + | Descriptor::String + | Descriptor::Option(_) + | Descriptor::Enum { .. } + | Descriptor::Unit => { + bail!("The constructor for class `{}` tries to return a JS primitive type, which would cause the return value to be ignored. Use a builder instead (remove the `constructor` attribute).", class); + } + Descriptor::Result(ref d) | Descriptor::Ref(ref d) | Descriptor::RefMut(ref d) => { + verify_constructor_return(class, d) + } + _ => Ok(()), + } +} + /// Extract all of the `Program`s encoded in our custom section. /// /// `program_storage` is used to squirrel away the raw bytes of the custom