Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly derive Clone on generated types #184

Merged
merged 3 commits into from
Apr 1, 2022

Conversation

Michael-F-Bryan
Copy link
Contributor

Fixes #170.

@Michael-F-Bryan Michael-F-Bryan marked this pull request as ready for review March 31, 2022 18:36
@Michael-F-Bryan
Copy link
Contributor Author

I believe I've implemented this correctly.

Interestingly, when I added is-clone-arg: function(i: is-clone) to the variants.wit codegen test, the test_helpers::codegen_py_import!() macro in the wit-bindgen-gen-wasmtime-py test suite panics with name i already defined. Changing the argument name to a fixed the problem, but I'm curious why it failed. Do we reserve i as a name because it is used as a local variable in the generated code?

Errors and backtrace from wit-bindgen-gen-wasmtime-py test
$ cargo t --package wit-bindgen-gen-wasmtime-py
   Compiling log v0.4.14
   Compiling syn v1.0.86
   Compiling cc v1.0.73
   Compiling gimli v0.26.1
   Compiling object v0.27.1
warning: variant is never constructed: `InvalidCharInString`
  --> crates/parser/src/ast/lex.rs:91:5
   |
91 |     InvalidCharInString(usize, char),
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default
note: `Error` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  --> crates/parser/src/ast/lex.rs:89:25
   |
89 | #[derive(Eq, PartialEq, Debug)]
   |                         ^^^^^
   = note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: variant is never constructed: `InvalidEscape`
  --> crates/parser/src/ast/lex.rs:95:5
   |
95 |     InvalidEscape(usize, char),
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: `Error` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  --> crates/parser/src/ast/lex.rs:89:25
   |
89 | #[derive(Eq, PartialEq, Debug)]
   |                         ^^^^^
   = note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: variant is never constructed: `UnterminatedString`
   --> crates/parser/src/ast/lex.rs:100:5
    |
100 |     UnterminatedString(usize),
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: `Error` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   --> crates/parser/src/ast/lex.rs:89:25
    |
89  | #[derive(Eq, PartialEq, Debug)]
    |                         ^^^^^
    = note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: variant is never constructed: `NewlineInString`
   --> crates/parser/src/ast/lex.rs:101:5
    |
101 |     NewlineInString(usize),
    |     ^^^^^^^^^^^^^^^^^^^^^^
    |
note: `Error` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   --> crates/parser/src/ast/lex.rs:89:25
    |
89  | #[derive(Eq, PartialEq, Debug)]
    |                         ^^^^^
    = note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `wit-parser` (lib) generated 4 warnings
   Compiling wit-bindgen-gen-wasmtime-py v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-wasmtime-py)
   Compiling globset v0.4.8
   Compiling ignore v0.4.18
   Compiling backtrace v0.3.64
   Compiling addr2line v0.17.0
   Compiling thiserror-impl v1.0.30
   Compiling thiserror v1.0.30
   Compiling witx v0.10.0 (https://github.com/alexcrichton/WASI?branch=abi-next#70a8c636)
   Compiling wit-parser v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/parser)
   Compiling wit-bindgen-gen-core v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-core)
   Compiling wit-bindgen-gen-c v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-c)
   Compiling wit-bindgen-gen-spidermonkey v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/gen-spidermonkey)
warning: `wit-parser` (lib) generated 4 warnings (4 duplicates)
warning: field is never read: `ok`
  --> crates/gen-c/src/lib.rs:75:9
   |
75 |         ok: Option<Type>,
   |         ^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default
note: `Scalar` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
  --> crates/gen-c/src/lib.rs:70:10
   |
70 | #[derive(Debug)]
   |          ^^^^^
   = note: this warning originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `wit-bindgen-gen-c` (lib) generated 1 warning
   Compiling test-helpers v0.1.0 (/home/consulting/Documents/hotg-ai/wit-bindgen/crates/test-helpers)
panic:    0: test_helpers::generate_tests::{{closure}}::{{closure}}
             at crates/test-helpers/src/lib.rs:368:38
   1: std::panicking::rust_panic_with_hook
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:702:17
   2: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:588:13
   3: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys_common/backtrace.rs:138:18
   4: rust_begin_unwind
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:584:5
   5: core::panicking::panic_fmt
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/panicking.rs:143:14
   6: core::result::unwrap_failed
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/result.rs:1785:5
   7: core::result::Result<T,E>::unwrap
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/result.rs:1078:23
   8: wit_bindgen_gen_wasmtime_py::FunctionBindgen::new
             at crates/gen-wasmtime-py/src/lib.rs:1364:13
   9: <wit_bindgen_gen_wasmtime_py::WasmtimePy as wit_bindgen_gen_core::Generator>::import
             at crates/gen-wasmtime-py/src/lib.rs:982:21
  10: wit_bindgen_gen_core::Generator::generate_one
             at crates/gen-core/src/lib.rs:141:38
  11: wit_bindgen_gen_core::Generator::generate_all
             at crates/gen-core/src/lib.rs:153:13
  12: test_helpers::generate_tests
             at crates/test-helpers/src/lib.rs:405:9
  13: test_helpers::gen_verify
             at crates/test-helpers/src/lib.rs:486:17
  14: test_helpers::codegen_py_import
             at crates/test-helpers/src/lib.rs:331:5
  15: core::ops::function::FnOnce::call_once
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/ops/function.rs:227:5
  16: proc_macro::bridge::client::Client<fn(proc_macro::TokenStream) .> proc_macro::TokenStream>::expand1::run::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:409:40
  17: proc_macro::bridge::client::run_client::{{closure}}::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:376:26
  18: proc_macro::bridge::scoped_cell::ScopedCell<T>::set::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/scoped_cell.rs:80:33
  19: proc_macro::bridge::scoped_cell::ScopedCell<T>::replace
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/scoped_cell.rs:75:9
  20: proc_macro::bridge::scoped_cell::ScopedCell<T>::set
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/scoped_cell.rs:80:9
  21: proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:324:35
  22: std::thread::local::LocalKey<T>::try_with
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/thread/local.rs:442:16
  23: std::thread::local::LocalKey<T>::with
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/thread/local.rs:418:9
  24: proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:324:9
  25: proc_macro::bridge::client::run_client::{{closure}}
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:369:9
  26: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/core/src/panic/unwind_safe.rs:271:9
  27: std::panicking::try::do_call
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:492:40
  28: __rust_try
  29: std::panicking::try
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panicking.rs:456:19
  30: std::panic::catch_unwind
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/panic.rs:137:14
  31: proc_macro::bridge::client::run_client
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:368:5
  32: proc_macro::bridge::client::Client<fn(proc_macro::TokenStream) .> proc_macro::TokenStream>::expand1::run
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/proc_macro/src/bridge/client.rs:409:13
  33: proc_macro::bridge::server::run_server::<rustc_expand::proc_macro_server::Rustc, proc_macro::bridge::Marked<rustc_ast::tokenstream::TokenStream, proc_macro::bridge::client::TokenStream>, proc_macro::bridge::Marked<rustc_ast::tokenstream::TokenStream, proc_macro::bridge::client::TokenStream>, fn(proc_macro::TokenStream) -> proc_macro::TokenStream, proc_macro::bridge::server::SameThread>
  34: <proc_macro::bridge::client::Client<fn(proc_macro::TokenStream) -> proc_macro::TokenStream>>::run::<rustc_expand::proc_macro_server::Rustc, proc_macro::bridge::server::SameThread>
  35: <rustc_expand::proc_macro::BangProcMacro as rustc_expand::base::ProcMacro>::expand
  36: <rustc_expand::expand::MacroExpander>::fully_expand_fragment
  37: <rustc_expand::expand::MacroExpander>::expand_crate
  38: <rustc_session::session::Session>::time::<core::result::Result<rustc_ast::ast::Crate, rustc_errors::ErrorGuaranteed>, rustc_interface::passes::configure_and_expand::{closure#1}>
  39: rustc_interface::passes::configure_and_expand
  40: <rustc_interface::queries::Queries>::expansion
  41: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorGuaranteed>>
  42: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
  43: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>
  44: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
  45: std::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
  46: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  47: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/alloc/src/boxed.rs:1861:9
      <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/alloc/src/boxed.rs:1861:9
      std::sys::unix::thread::Thread::new::thread_start
             at /rustc/9c06e1ba47e1077725a950e0b5d1870a89c8b536/library/std/src/sys/unix/thread.rs:108:17
  48: start_thread
  49: __clone

error: proc macro panicked
  --> crates/gen-wasmtime-py/tests/codegen.rs:14:5
   |
14 | /     test_helpers::codegen_py_import!(
15 | |         "*.wit"
16 | |
17 | |         // TODO: implement async support
...  |
23 | |         "!host.wit"
24 | |     );
   | |_____^
   |
   = help: message: called `Result::unwrap()` on an `Err` value: "name `i` already defined"

error: could not compile `wit-bindgen-gen-wasmtime-py` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed

Michael-F-Bryan pushed a commit to hotg-ai/wit-files that referenced this pull request Mar 31, 2022
Michael-F-Bryan pushed a commit to hotg-ai/proc-blocks that referenced this pull request Mar 31, 2022
@alexcrichton alexcrichton merged commit f5eed0f into bytecodealliance:main Apr 1, 2022
@alexcrichton
Copy link
Member

Looks good to me!

Can you open an issue for that panic? That looks like a bug...

@Michael-F-Bryan Michael-F-Bryan deleted the variant-should-be-clone branch April 1, 2022 21:30
willemneal pushed a commit to AhaLabs/wit-bindgen that referenced this pull request May 31, 2022
* Added a failing test

* Implement clone where possible

* Arguments can't be called "i"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why does binding for vairant not implement Clone trait?
2 participants