bytecodealliance / wit-bindgen

A language binding generator for WebAssembly interface types
Apache License 2.0
1.03k stars 194 forks source link

The `wasmtime-py` generator is unable to handle `i` as a function argument #185

Closed Michael-F-Bryan closed 2 years ago

Michael-F-Bryan commented 2 years ago

While working on bytecodealliance/wit-bindgen#184, I noticed that the wasmtime-py generator will panic if it encounters a function with i as an argument.

Here is a minimal repro.wit.

foo: function(i: u32)

You can generate bindings for wasmtime just fine, but running wit-bindgen wasmtime-py will fail.

$ wit-bindgen wasmtime --import repro.wit
Generating "bindings.rs"

$ RUST_BACKTRACE=full wit-bindgen wasmtime-py --import repro.wit
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "name `i` already defined"', crates/gen-wasmtime-py/src/lib.rs:1364:34
stack backtrace:
   0:     0x557d6db46b7d - std::backtrace_rs::backtrace::libunwind::trace::ha61e02a8334e50f3
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x557d6db46b7d - std::backtrace_rs::backtrace::trace_unsynchronized::hf1f3ac068a02fed8
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x557d6db46b7d - std::sys_common::backtrace::_print_fmt::hcb006b968ad65afb
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x557d6db46b7d - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h84fe8b1815748262
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x557d6db6662c - core::fmt::write::h014a30a115434dd3
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/fmt/mod.rs:1190:17
   5:     0x557d6db43f11 - std::io::Write::write_fmt::h5655b2d07613981b
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/io/mod.rs:1655:15
   6:     0x557d6db484c5 - std::sys_common::backtrace::_print::hae642f87598c6388
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x557d6db484c5 - std::sys_common::backtrace::print::h0cd294671355932d
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x557d6db484c5 - std::panicking::default_hook::{{closure}}::h020e24b237ebbb9d
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:295:22
   9:     0x557d6db48179 - std::panicking::default_hook::h52829553e7be56cc
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:314:9
  10:     0x557d6db48a12 - std::panicking::rust_panic_with_hook::hf7ead38591119d46
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:698:17
  11:     0x557d6db488f7 - std::panicking::begin_panic_handler::{{closure}}::h8872eead3c14ce0e
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:588:13
  12:     0x557d6db47034 - std::sys_common::backtrace::__rust_end_short_backtrace::h87d7482724050f45
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x557d6db48629 - rust_begin_unwind
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:584:5
  14:     0x557d6d57bb53 - core::panicking::panic_fmt::h590a17a50dc4f254
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/panicking.rs:143:14
  15:     0x557d6d57bbe3 - core::result::unwrap_failed::h22c1eeeea34774ea
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/result.rs:1785:5
  16:     0x557d6d72c521 - core::result::Result<T,E>::unwrap::hb35f56a9e22a20d0
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/result.rs:1078:23
  17:     0x557d6d75c15a - wit_bindgen_gen_wasmtime_py::FunctionBindgen::new::h9918a22d5fa04694
                               at /home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-wasmtime-py/src/lib.rs:1364:13
  18:     0x557d6d75634f - <wit_bindgen_gen_wasmtime_py::WasmtimePy as wit_bindgen_gen_core::Generator>::import::h1f877fdb513bcd50
                               at /home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-wasmtime-py/src/lib.rs:982:21
  19:     0x557d6d58f6a4 - wit_bindgen_gen_core::Generator::generate_one::hf0f85176671a0be8
                               at /home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-core/src/lib.rs:141:38
  20:     0x557d6d58edf2 - wit_bindgen_gen_core::Generator::generate_all::h6974d1b00f4d3720
                               at /home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-core/src/lib.rs:153:13
  21:     0x557d6d593d78 - wit_bindgen::main::h70a55c99ce3c894d
                               at /home/consulting/Documents/hotg-ai/wit-bindgen/src/bin/wit-bindgen.rs:105:5
  22:     0x557d6d58be4b - core::ops::function::FnOnce::call_once::h7a677c22ee1d6394
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/ops/function.rs:227:5
  23:     0x557d6d59182e - std::sys_common::backtrace::__rust_begin_short_backtrace::h7869aaf2560ea0d8
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:122:18
  24:     0x557d6d591691 - std::rt::lang_start::{{closure}}::h0c254b92f3e3fab1
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/rt.rs:145:18
  25:     0x557d6db3e48b - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h49a93ac287187682
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/ops/function.rs:259:13
  26:     0x557d6db3e48b - std::panicking::try::do_call::hcd4b3dc4ac4bcb60
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:492:40
  27:     0x557d6db3e48b - std::panicking::try::h1f926ed7469c4f46
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:456:19
  28:     0x557d6db3e48b - std::panic::catch_unwind::h43074872d3bf848f
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panic.rs:137:14
  29:     0x557d6db3e48b - std::rt::lang_start_internal::{{closure}}::he5365026c45e5aef
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/rt.rs:128:48
  30:     0x557d6db3e48b - std::panicking::try::do_call::he452105ced72f1bf
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:492:40
  31:     0x557d6db3e48b - std::panicking::try::hcdf03ff669eae6e7
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:456:19
  32:     0x557d6db3e48b - std::panic::catch_unwind::h5dc9e782dc001027
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panic.rs:137:14
  33:     0x557d6db3e48b - std::rt::lang_start_internal::h2273112e25a5ce64
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/rt.rs:128:20
  34:     0x557d6d591660 - std::rt::lang_start::hbb22de4f6516f790
                               at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/rt.rs:144:17
  35:     0x557d6d596bfc - main
  36:     0x7f2783874310 - __libc_start_call_main
  37:     0x7f27838743c1 - __libc_start_main@GLIBC_2.2.5
  38:     0x557d6d57bde5 - _start
  39:                0x0 - <unknown>

I believe the following lines are to blame:

https://github.com/bytecodealliance/wit-bindgen/blob/f5eed0fb9f1087a2f8889194d0debeeafa789c88/crates/gen-wasmtime-py/src/lib.rs#L1362-L1365

The easiest way to avoid this naming collision would be to switch i (and len and base) to something that isn't a valid parameter name. For example, we could use _i because leading underscores aren't supported (#168).


To help reproduce the error, I installed wit-bindgen from main (f5eed0fb9f1087a2f8889194d0debeeafa789c88) as follows:

$ cd path/to/wit-bindgen
$ git checkout f5eed0fb9f1087a2f8889194d0debeeafa789c88
$ cargo install --debug --path .
alexcrichton commented 2 years ago

https://github.com/bytecodealliance/wasmtime-py/issues/105 is slightly more general than this issue so I'm going to close this in favor of that.