Skip to content

Commit 3276565

Browse files
Daniel BloomDaniel-Aaron-Bloom
authored andcommitted
Add support for $crate to Ident
1 parent 2398bd6 commit 3276565

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

library/proc_macro/src/bridge/symbol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Symbol {
3333
/// Validates and normalizes before converting it to a symbol.
3434
pub(crate) fn new_ident(string: &str, is_raw: bool) -> Self {
3535
// Fast-path: check if this is a valid ASCII identifier
36-
if Self::is_valid_ascii_ident(string.as_bytes()) {
36+
if Self::is_valid_ascii_ident(string.as_bytes()) || string == "$crate" {
3737
if is_raw && !Self::can_be_raw(string) {
3838
panic!("`{}` cannot be a raw identifier", string);
3939
}
@@ -79,7 +79,7 @@ impl Symbol {
7979
// Mimics the behavior of `Symbol::can_be_raw` from `rustc_span`
8080
fn can_be_raw(string: &str) -> bool {
8181
match string {
82-
"_" | "super" | "self" | "Self" | "crate" => false,
82+
"_" | "super" | "self" | "Self" | "crate" | "$crate" => false,
8383
_ => true,
8484
}
8585
}

tests/ui/proc-macro/auxiliary/mixed-site-span.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ pub fn proc_macro_rules(input: TokenStream) -> TokenStream {
1717
TokenTree::from(single_quote),
1818
id("label_use"),
1919
].iter().cloned().collect();
20+
let dollar_crate = id("$crate");
2021
quote!(
22+
use $dollar_crate::proc_macro_rules as _; // OK
23+
type A = $dollar_crate::ItemUse; // ERROR
24+
2125
struct $item_def;
2226
let $local_def = 0;
2327

@@ -29,7 +33,8 @@ pub fn proc_macro_rules(input: TokenStream) -> TokenStream {
2933
let mut dollar_crate = input.into_iter().next().unwrap();
3034
dollar_crate.set_span(Span::mixed_site());
3135
quote!(
32-
type A = $dollar_crate::ItemUse;
36+
use $dollar_crate::proc_macro_rules as _; // OK
37+
type A = $dollar_crate::ItemUse; // ERROR
3338
)
3439
}
3540
}

tests/ui/proc-macro/mixed-site-span.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ fn main() {
1111
'label_use: loop {
1212
let local_use = 1;
1313
proc_macro_rules!();
14-
//~^ ERROR use of undeclared label `'label_use`
14+
//~^ ERROR cannot find type `ItemUse` in crate `$crate`
15+
//~| ERROR use of undeclared label `'label_use`
1516
//~| ERROR cannot find value `local_use` in this scope
1617
ItemDef; // OK
1718
local_def; //~ ERROR cannot find value `local_def` in this scope

tests/ui/proc-macro/mixed-site-span.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ LL | proc_macro_rules!();
66
|
77
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9+
error[E0412]: cannot find type `ItemUse` in crate `$crate`
10+
--> $DIR/mixed-site-span.rs:13:9
11+
|
12+
LL | proc_macro_rules!();
13+
| ^^^^^^^^^^^^^^^^^^^ not found in `$crate`
14+
|
15+
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: consider importing this struct
17+
|
18+
LL + use ItemUse;
19+
|
20+
921
error[E0425]: cannot find value `local_use` in this scope
1022
--> $DIR/mixed-site-span.rs:13:9
1123
|
@@ -15,20 +27,20 @@ LL | proc_macro_rules!();
1527
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
1628

1729
error[E0425]: cannot find value `local_def` in this scope
18-
--> $DIR/mixed-site-span.rs:17:9
30+
--> $DIR/mixed-site-span.rs:18:9
1931
|
2032
LL | local_def;
2133
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
2234

2335
error[E0412]: cannot find type `ItemUse` in crate `$crate`
24-
--> $DIR/mixed-site-span.rs:24:1
36+
--> $DIR/mixed-site-span.rs:25:1
2537
|
2638
LL | pass_dollar_crate!();
2739
| ^^^^^^^^^^^^^^^^^^^^ not found in `$crate`
2840
|
2941
= note: this error originates in the macro `proc_macro_rules` which comes from the expansion of the macro `pass_dollar_crate` (in Nightly builds, run with -Z macro-backtrace for more info)
3042

31-
error: aborting due to 4 previous errors
43+
error: aborting due to 5 previous errors
3244

3345
Some errors have detailed explanations: E0412, E0425, E0426.
3446
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)