diff --git a/crates/wasmtime/src/func/typed.rs b/crates/wasmtime/src/func/typed.rs index a51d5a379c..211116b73d 100644 --- a/crates/wasmtime/src/func/typed.rs +++ b/crates/wasmtime/src/func/typed.rs @@ -347,17 +347,23 @@ macro_rules! impl_wasm_params { fn typecheck(mut params: impl ExactSizeIterator) -> Result<()> { let mut _n = 0; + $( match params.next() { - Some(t) => $t::typecheck(t)?, - None => bail!("expected {} types, found {}", $n, _n), + Some(t) => { + _n += 1; + $t::typecheck(t)? + }, + None => bail!("expected {} types, found {}", $n, params.len() + _n), } - _n += 1; )* match params.next() { None => Ok(()), - Some(_) => bail!("expected {} types, found {}", $n, params.len() + _n), + Some(_) => { + _n += 1; + bail!("expected {} types, found {}", $n, params.len() + _n) + }, } } diff --git a/tests/all/func.rs b/tests/all/func.rs index b4f025e190..95d75ebf98 100644 --- a/tests/all/func.rs +++ b/tests/all/func.rs @@ -884,3 +884,45 @@ fn wasm_ty_roundtrip() -> Result<(), anyhow::Error> { foo.call(&mut store, (-1, 1, 2.0, -3, 3, 4.0))?; Ok(()) } + +#[test] +fn typed_funcs_count_params_correctly_in_error_messages() -> anyhow::Result<()> { + let mut store = Store::<()>::default(); + let module = Module::new( + store.engine(), + r#" + (module + (func (export "f") (param i32 i32)) + ) + + "#, + )?; + let instance = Instance::new(&mut store, &module, &[])?; + + // Too few parameters. + match instance.get_typed_func::<(), (), _>(&mut store, "f") { + Ok(_) => panic!("should be wrong signature"), + Err(e) => { + let msg = format!("{:?}", e); + assert!(dbg!(msg).contains("expected 0 types, found 2")) + } + } + match instance.get_typed_func::<(i32,), (), _>(&mut store, "f") { + Ok(_) => panic!("should be wrong signature"), + Err(e) => { + let msg = format!("{:?}", e); + assert!(dbg!(msg).contains("expected 1 types, found 2")) + } + } + + // Too many parameters. + match instance.get_typed_func::<(i32, i32, i32), (), _>(&mut store, "f") { + Ok(_) => panic!("should be wrong signature"), + Err(e) => { + let msg = format!("{:?}", e); + assert!(dbg!(msg).contains("expected 3 types, found 2")) + } + } + + Ok(()) +}