-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Using $crate with a proc macro #37637
Comments
|
@jseyfried can you clarify how this is fixed by #37614? It isn't obvious from the thread. I see that custom derives no longer return the original item but that doesn't fix other uses of impl B {
pub fn new(a: $crate::A) -> Self {
B { a: A }
}
} |
@dtolnay good point, #37614 won't fix other uses of I think the best solution would be to eliminate /// crate A
pub struct Foo;
#[macro_export]
macro_rules! m { () => {
#[derive(custom)] struct Bar($crate::Foo);
} }
/// crate B
#[macro_reexport(m)] extern crate A;
/// crate C
#[macro_use(m)] extern crate B;
m!();
//^ expands to `#[derive(custom)] struct Bar($crate::Foo);`
// There is no way to eliminate the `$crate` before invoking the custom derive
// since there's no other way to name `Foo`. In this case, I think we'll have to error on |
If we can't solve this today I wonder if we could perhaps provide a more targeted error message? E.g. just print an error saying "sorry, but |
I'll solve this today. |
Fixed in #37645. |
Fix regression involving custom derives on items with `$crate` The regression was introduced in #37213. I believe we cannot make the improvements from #37213 work with the current custom derive setup (c.f. #37637 (comment)) -- we'll have to wait for `TokenStream`'s API to improve. Fixes #37637. r? @nrc
This seems to have regressed again, this time with function-like proc macros: Proc Macro crate: #[proc_macro]
pub fn print_input(input: TokenStream) -> TokenStream {
let source = input.to_string();
println!("{}", source);
input
} Used here: #![feature(use_extern_macros)]
extern crate print_input;
fn hello() {
println!("Hello");
}
macro_rules! print_hello {
() => {
print_input::print_input!($crate::hello())
}
}
fn main() {
print_hello!();
}
|
@tikue I don't think that's a regression -- the fix has only ever applied to Since the fix is fairly invasive and often doesn't work (esp. when used with other macros 2.0), I think we should wait for better |
@jseyfried sounds good, thanks! |
Does using |
It seems that TokenStream::parse() does not understand
$crate
, yet$crate
gets passed as input to proc macros. That doesn't seem fair!Here is a script to reproduce the issue. It creates a proc macro called
noop_derive
which just reparses the input TokenStream. It creates a crate calledrepro
which uses#[derive(Noop)]
from within macro_rules. Expected behavior would be either of the following:$crate
is eliminated before invoking the proc macro, or$crate
is passed to the proc macro and can be parsed by TokenStream.The text was updated successfully, but these errors were encountered: