-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Proc macro hygiene regression #46489
Comments
This also broke perf.rlo (due to breaking servo). |
rust-lang/rustc-perf#171 applies the work around to the servo copy in perf.rlo. |
This seems like correct behaviour. The derive names must be in scope where the macro is defined, not where it is used. I don't recall how derives are looked up though, so I'm not sure if that mechanism works correctly with the proper hygiene here - we should re-open or open another issue if that doesn't work. Stripping the hygiene info as in the work-around (which is actually setting all the hygiene info to the macro use site) is not recommended. |
Urgh, sorry, I misunderstood what is going on here - it seems that we're looking up and expanding the derives fine. Given that the derives (unhygienic) are being expanded inside a hygienic context, I'm not even sure where I would expect them to look for their names. |
I do realize this is a hack that I’d rather was not needed, but this is the only way I’ve found to make this existing code compile with a newer compiler, which unblocks unrelated work. |
This broke rsx. Latest nightly that works is from Dec 1. This doesn't work anymore: let foo = 42;
...
my_proc_macro! { foo } |
A more explicative use case for this in case of rsx would be the following: let stylesheet = ...;
let node = rsx! {
<view style={stylesheet.get(".root")}>
Hello world!
</view>
}; where |
This also breaks my code I have a proc_macro like this: #![feature(proc_macro)]
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::{quote, TokenStream, TokenTree, TokenTreeIter, TokenNode, Delimiter};
fn parse(iter: &mut TokenTreeIter) -> TokenStream {
iter.next();
iter.next();
if let Some(TokenTree{span: _, kind: TokenNode::Group(Delimiter::None, stream)}) = iter.next() {
quote!($stream)
} else {
TokenStream::empty()
}
}
#[proc_macro]
pub fn my_macro(stream: TokenStream) -> TokenStream {
parse(&mut stream.into_iter())
} now I got error[E0425]: cannot find value #![feature(proc_macro)]
extern crate my_macro;
pub use my_macro::my_macro;
macro_rules! m {
($($arg:expr),*) => (
$crate::my_macro!($crate $(, $arg)*)
)
}
fn main() {
let x = 1usize;
let y = m!(&x);
println!("{}", y);
} |
I think I'm hitting this too, here's the simplest repro I could do #![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::TokenStream;
use std::str::FromStr;
#[proc_macro]
pub fn add(token_stream: TokenStream) -> TokenStream {
TokenStream::from_str(&token_stream.to_string().replace(",", "+")).unwrap()
} #![feature(proc_macro)]
extern crate add;
use add::add;
#[test]
fn test_add() {
let a = 1;
let b = 2;
let c = add!(a, b);
assert_eq!(3, c);
}
|
Also hitting this error in my tests of proc_macro. Example : |
I m hinting this too , same like with @parched demo code. |
I'm working on a PR to fix this now. |
…into playground so that a clean compile can be achieved. In response to this bug in the nightly compiler: rust-lang/rust#46489
Is there any workaround for this? One of my libraries is depending on |
@rushmorem In the specific code originally in this issue, I’ve worked around by replacing |
@SimonSapin Thank you. I will try that. |
I wrote function to make macro can use call_site variable, like fn into_call_site_span(stream: TokenStream) -> TokenStream {
let call_site_span = Span::call_site();
let iter = stream.into_iter().map(|mut tree| {
match tree {
TokenTree::Group(g) => {
TokenTree::Group(Group::new(g.delimiter(), into_call_site_span(g.stream())))
}
_ => {
tree.set_span(call_site_span);
tree
}
}
});
TokenStream::from_iter(iter)
} |
@rail44 That's exactly the workaround I needed! Do you mind if I use it? |
@jeremydavis519 Of course. |
Still getting this issue with Proc macro
Calling code:
Error:
|
Triage: I see no use of any nightly exclusive feature, so this seems to be a stable-stable regression. Labeling as such. |
Something in the range bb42071...f9b0897, likely #46343 (CC @jseyfried), broke Servo.
We have a
#[dom_struct]
attribute implemented in adom_struct
crate like this:Each of the derives is defined in a respective crate. The
script
crate depends on all of these crates and uses#[dom_struct]
. Some of the derives generate code that reference items defined inscript
. For example,#[derive(DomObject)]
implements thescript::dom::bindings::refector::DomObject
trait.Since rustc 1.24.0-nightly (f9b0897 2017-12-02), every use of
#[dom_struct]
fails with:I suppose that these errors come from code generated by the derives, and that
{{root}}
refers to the root of thedom_struct
crate where the#[derive(…)]
tokens come from. Indeed, thejs
anddom
are not an cannot be available there, they’re in thescript
crate which depends ondom_struct
.We can work around this by erasing hygiene data in the
#[derive(…)]
tokens:… but this seems like a valid pattern that shouldn’t necessitate such hacks.
The text was updated successfully, but these errors were encountered: