-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-proc-macrosArea: Procedural macrosArea: Procedural macrosC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
I tried this code:
// macros/src/lib.rs
use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;
#[proc_macro_derive(use_self)]
pub fn use_self(input: TokenStream) -> TokenStream {
let item: syn::ItemEnum = parse_macro_input!(input);
let ident = item.ident;
let var0 = item.variants[0].ident.clone();
TokenStream::from(quote! {
#[automatically_derived]
impl #ident {
fn foo() -> Self {
#ident::#var0
}
}
})
}
// src/lib.rs
use macros::use_self;
#[derive(use_self)]
pub enum Direction {
East,
}
Currently, the Span::ctxt()
of Direction::East
in the expansion is equal to SyntaxCtxt::root()
. But this is obviously wrong because ::
is inserted by the macro.
This would be because the span of ::
is ignored in
rust/compiler/rustc_parse/src/parser/path.rs
Lines 149 to 157 in d68f7a2
let lo = self.token.span; | |
let mut segments = Vec::new(); | |
let mod_sep_ctxt = self.token.span.ctxt(); | |
if self.eat(&token::ModSep) { | |
segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt))); | |
} | |
self.parse_path_segments(&mut segments, style)?; | |
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None }) |
Metadata
Metadata
Assignees
Labels
A-proc-macrosArea: Procedural macrosArea: Procedural macrosC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.