Skip to content

Commit a26b0b9

Browse files
committed
add trait impls to proc_macro::Ident
1 parent 5413f7d commit a26b0b9

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

library/proc_macro/src/bridge/mod.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
#![deny(unsafe_code)]
1010

11-
use std::hash::Hash;
11+
use std::hash::{Hash, Hasher};
1212
use std::ops::{Bound, Range};
1313
use std::sync::Once;
1414
use std::{fmt, marker, mem, panic, thread};
@@ -449,13 +449,42 @@ pub struct Punct<Span> {
449449

450450
compound_traits!(struct Punct<Span> { ch, joint, span });
451451

452-
#[derive(Copy, Clone, Eq, PartialEq)]
452+
#[derive(Copy, Clone)]
453453
pub struct Ident<Span, Symbol> {
454454
pub sym: Symbol,
455455
pub is_raw: bool,
456456
pub span: Span,
457457
}
458458

459+
impl<Span, Symbol: fmt::Display, T> PartialEq<T> for Ident<Span, Symbol>
460+
where
461+
Symbol: PartialEq<str>,
462+
T: AsRef<str> + ?Sized,
463+
{
464+
fn eq(&self, other: &T) -> bool {
465+
self.to_string() == other.as_ref()
466+
}
467+
}
468+
469+
impl<Span, Symbol: Hash> Hash for Ident<Span, Symbol> {
470+
fn hash<H: Hasher>(&self, state: &mut H) {
471+
self.sym.hash(state);
472+
self.is_raw.hash(state);
473+
}
474+
}
475+
476+
/// Prints the identifier as a string that should be losslessly convertible back
477+
/// into the same identifier.
478+
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
479+
impl<Span, Symbol: fmt::Display> fmt::Display for Ident<Span, Symbol> {
480+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481+
if self.is_raw {
482+
f.write_str("r#")?;
483+
}
484+
fmt::Display::fmt(&self.sym, f)
485+
}
486+
}
487+
459488
compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span });
460489

461490
#[derive(Clone, Eq, PartialEq)]

library/proc_macro/src/bridge/symbol.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
1212
use std::cell::RefCell;
1313
use std::num::NonZero;
14+
use std::{cmp, str};
1415

1516
use super::*;
1617

1718
/// Handle for a symbol string stored within the Interner.
18-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
19+
#[derive(Copy, Clone)]
1920
pub struct Symbol(NonZero<u32>);
2021

2122
impl !Send for Symbol {}
@@ -96,6 +97,38 @@ impl fmt::Display for Symbol {
9697
}
9798
}
9899

100+
impl PartialEq<Self> for Symbol {
101+
fn eq(&self, other: &Self) -> bool {
102+
self.0 == other.0
103+
}
104+
}
105+
106+
impl PartialEq<str> for Symbol {
107+
fn eq(&self, other: &str) -> bool {
108+
self.with(|s| s == other)
109+
}
110+
}
111+
112+
impl Eq for Symbol {}
113+
114+
impl Hash for Symbol {
115+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
116+
self.0.hash(state);
117+
}
118+
}
119+
120+
impl PartialOrd for Symbol {
121+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
122+
Some(self.cmp(other))
123+
}
124+
}
125+
126+
impl Ord for Symbol {
127+
fn cmp(&self, other: &Self) -> cmp::Ordering {
128+
self.with(|s| other.with(|o| s.cmp(o)))
129+
}
130+
}
131+
99132
impl<S> Encode<S> for Symbol {
100133
fn encode(self, w: &mut Writer, s: &mut S) {
101134
self.with(|sym| sym.encode(w, s))

library/proc_macro/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ impl PartialEq<Punct> for char {
10321032
}
10331033

10341034
/// An identifier (`ident`).
1035-
#[derive(Clone)]
1035+
#[derive(Clone, Hash)]
10361036
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10371037
pub struct Ident(bridge::Ident<bridge::client::Span, bridge::client::Symbol>);
10381038

@@ -1097,10 +1097,7 @@ impl Ident {
10971097
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
10981098
impl fmt::Display for Ident {
10991099
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1100-
if self.0.is_raw {
1101-
f.write_str("r#")?;
1102-
}
1103-
fmt::Display::fmt(&self.0.sym, f)
1100+
self.0.fmt(f)
11041101
}
11051102
}
11061103

@@ -1114,6 +1111,13 @@ impl fmt::Debug for Ident {
11141111
}
11151112
}
11161113

1114+
#[stable(feature = "proc_macro_ident_impls", since = "CURRENT_RUSTC_VERSION")]
1115+
impl<T: AsRef<str> + ?Sized> PartialEq<T> for Ident {
1116+
fn eq(&self, other: &T) -> bool {
1117+
self.0 == other
1118+
}
1119+
}
1120+
11171121
/// A literal string (`"hello"`), byte string (`b"hello"`), C string (`c"hello"`),
11181122
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
11191123
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).

src/tools/clippy/tests/ui/auxiliary/proc_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl Expander {
465465
} else if let TT::Punct(p) = &input.tt
466466
&& p.as_char() == '!'
467467
&& let TT::Ident(name) = &tt
468-
&& name.to_string() == "inline"
468+
&& *name == "inline"
469469
{
470470
let g = expect_tt(
471471
input.iter.next(),

0 commit comments

Comments
 (0)