-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Macros from the "log" crate conflict with #[wasm_bindgen] #158
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
Comments
This is actually a regression in nightly-2018-04-15 and not related to wasm_bindgen or log specifically. I had this exact error with a different proc macro emitting a println, though the setup was to complicated to make a proper bug report out of it. Code to ReproduceTrying to this narrow down in wasm_bindgen has eventually led to this repository with a complete minimized example without wasm_bindgen. The relevant code boils down to the following: The proc macro: #![feature(proc_macro)]
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn noop_derive(_attr: TokenStream, input: TokenStream) -> TokenStream {
// It is required to generate a new token stream; Just returning the
// input will pass
let item: syn::Item = syn::parse(input).unwrap();
quote!(#item).into()
} The function using it: #![feature(proc_macro)]
extern crate nightly_regression_derive;
use nightly_regression_derive::noop_derive;
pub struct Foo {
x: u32,
}
#[noop_derive]
pub fn info_print(message: &str) {
println!("{}", message);
Foo { x: 42 };
} Commands to reproduceIn the repository there's
Expanded codeThe exanded code shows that message is in scope despite the compiler saying otherwise. The generated code is identical for the two nightlies, despite rustfmt differing in the inserted whitespace. It is identical for nightly-2018-04-14 and nightly-2018-04-15 ignoring a newline after the struct.
#![feature(prelude_import)]
#![no_std]
#![feature(proc_macro)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
extern crate nightly_regression_derive;
use nightly_regression_derive::noop_derive;
pub struct Foo {
x: u32,
}
pub fn info_print(message: &str) {
::io::_print(::std::fmt::Arguments::new_v1_formatted(
&["", "\n"],
&match (&message,) {
(__arg0,) => [::std::fmt::ArgumentV1::new(
__arg0,
::std::fmt::Display::fmt,
)],
},
&[::std::fmt::rt::v1::Argument {
position: ::std::fmt::rt::v1::Position::At(0usize),
format: ::std::fmt::rt::v1::FormatSpec {
fill: ' ',
align: ::std::fmt::rt::v1::Alignment::Unknown,
flags: 0u32,
precision: ::std::fmt::rt::v1::Count::Implied,
width: ::std::fmt::rt::v1::Count::Implied,
},
}],
));
Foo { x: 42 };
} Further investigationThe function or method must contain the instantiation of a struct with at least one public field. Example without a public field that passes#![feature(proc_macro)]extern crate nightly_regression_derive; use nightly_regression_derive::noop_derive; pub struct Foo; #[noop_derive] The function or method must contain a println or log using a local variable. Example with a const as parameter to println that passes#![feature(proc_macro)]extern crate nightly_regression_derive; use nightly_regression_derive::noop_derive; const MESSAGE: &str = "Hello World"; pub struct Foo { #[noop_derive] The order of the prinln and the instantiation and other in between statements make no differnce. Example with reorder fields and other statements that fails#![feature(proc_macro)]extern crate nightly_regression_derive; use nightly_regression_derive::noop_derive; pub struct Foo { #[noop_derive] The crate type has no influence (I tested the implicit, cdylib and dylib) It must be a function or method emitted by a proc macro. The proc macro must return a newly generated TokenStream and not the input. I.e. Proc macro that makes the code pass. The generated code is identical ignoring some newlines.#![feature(proc_macro)]
extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;
use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn noop_derive(_attr: TokenStream, input: TokenStream) -> TokenStream {
input
} |
Thanks for the report! This should be fixed by rust-lang/rust#50152 which will hopefully be in the next nightly! |
This program:
Fails to compile:
The
log
macros are unable to see any of the surrounding variables, but as far as I can tell this only happens inside a#[wasm_bindgen]
annotated function that returns a#[wasm_bindgen]
annotated struct. If the function returns nothing oru32
or evenString
then it compiles successfully.The text was updated successfully, but these errors were encountered: