Skip to content

Commit 96843a0

Browse files
authored
Rollup merge of rust-lang#141996 - Daniel-Aaron-Bloom:dollar_crate, r=petrochenkov
Fix `proc_macro::Ident`'s handling of `$crate` This PR is addresses a few minor bugs, all relating to `proc_macro::Ident`'s support for `$crate`. `Ident` currently supports `$crate` (as can be seen in the `mixed-site-span` test), but: * `proc_macro::Symbol::can_be_raw` is out of sync with `rustc_span::Symbol::can_be_raw` * former doesn't cover `$crate` while the latter does cover `kw::DollarCrate` * `Ident::new` rejects `$crate` * This conflicts with the [reference definition](https://doc.rust-lang.org/nightly/reference/macros-by-example.html#r-macro.decl.meta.specifier) of `ident` which includes `$crate`. * This also conflicts with the documentation on [`Display for Ident`](https://doc.rust-lang.org/proc_macro/struct.Ident.html#impl-Display-for-Ident) which says the output "should be losslessly convertible back into the same identifier". This PR fixes the above issues and extends the `mixed-site-span` test to exercise these fixed code paths, as well as validating the different possible spans resolve `$crate` as expected (for both the new and old `$crate` construction code paths).
2 parents 93c5858 + dcb20d7 commit 96843a0

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

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
}

0 commit comments

Comments
 (0)