Skip to content

Commit 2bc67da

Browse files
committed
proc_macro: Accept $crate as an identifier if it comes from the compiler
1 parent 8a8ef26 commit 2bc67da

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/libsyntax_ext/proc_macro_server.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ impl FromInternal<(TokenStream, &'_ ParseSess, &'_ mut Vec<Self>)>
150150
Question => op!('?'),
151151
SingleQuote => op!('\''),
152152

153+
Ident(ident, false) if ident.name == keywords::DollarCrate.name() =>
154+
tt!(Ident::dollar_crate()),
153155
Ident(ident, is_raw) => tt!(Ident::new(ident.name, is_raw)),
154156
Lifetime(ident) => {
155157
let ident = ident.without_first_quote();
@@ -359,6 +361,10 @@ impl Ident {
359361
}
360362
Ident { sym, is_raw, span }
361363
}
364+
fn dollar_crate(span: Span) -> Ident {
365+
// `$crate` is accepted as an ident only if it comes from the compiler.
366+
Ident { sym: keywords::DollarCrate.name(), is_raw: false, span }
367+
}
362368
}
363369

364370
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::TokenStream;
8+
9+
#[proc_macro]
10+
pub fn normalize(input: TokenStream) -> TokenStream {
11+
input.into_iter().collect()
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-pass
2+
// aux-build:dollar-crate.rs
3+
4+
extern crate dollar_crate;
5+
6+
type S = u8;
7+
8+
macro_rules! check { () => {
9+
dollar_crate::normalize! {
10+
type A = $crate::S;
11+
}
12+
}}
13+
14+
check!();
15+
16+
fn main() {}

0 commit comments

Comments
 (0)