diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 4cfb188783ba1..ad903fb39463c 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -513,6 +513,9 @@ impl server::Ident for Rustc<'_> { fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident { Ident::new(self.sess, Symbol::intern(string), is_raw, span) } + fn eq(&mut self, ident: Self::Ident, rhs: &str) -> bool { + ident.sym.as_str() == rhs + } fn span(&mut self, ident: Self::Ident) -> Self::Span { ident.span } diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index c898d483a8ba2..8771486e63f7d 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -101,6 +101,7 @@ macro_rules! with_api { }, Ident { fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident; + fn eq($self: $S::Ident, rhs: &str) -> bool; fn span($self: $S::Ident) -> $S::Span; fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident; }, diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 5a4b69cf6fc1b..428ab3a5ed438 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -923,6 +923,31 @@ impl fmt::Debug for Ident { } } +#[stable(feature = "proc_macro_ident_eq", since = "1.49.0")] +impl PartialEq for Ident { + fn eq(&self, rhs: &str) -> bool { + self.0.eq(rhs) + } +} + +#[stable(feature = "proc_macro_ident_eq", since = "1.49.0")] +impl PartialEq for Ident { + fn eq(&self, rhs: &String) -> bool { + >::eq(self, rhs.as_str()) + } +} + +#[stable(feature = "proc_macro_ident_eq", since = "1.49.0")] +impl<'a, T> PartialEq<&'a T> for Ident +where + T: ?Sized, + Ident: PartialEq, +{ + fn eq(&self, rhs: &&T) -> bool { + >::eq(self, *rhs) + } +} + /// A literal string (`"hello"`), byte string (`b"hello"`), /// character (`'a'`), byte character (`b'a'`), an integer or floating point number /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). diff --git a/library/proc_macro/tests/test.rs b/library/proc_macro/tests/test.rs index 331b330cf29f0..be99bb83bf583 100644 --- a/library/proc_macro/tests/test.rs +++ b/library/proc_macro/tests/test.rs @@ -1,6 +1,6 @@ #![feature(proc_macro_span)] -use proc_macro::LineColumn; +use proc_macro::{Ident, LineColumn}; #[test] fn test_line_column_ord() { @@ -10,3 +10,14 @@ fn test_line_column_ord() { assert!(line0_column0 < line0_column1); assert!(line0_column1 < line1_column0); } + +#[test] +fn test_ident_eq() { + // Good enough if it typechecks, since proc_macro::Ident can't exist in a test. + fn _check(ident: &Ident, string: String) { + let _ = ident == "serde"; + let _ = *ident == "serde"; + let _ = *ident == string; + let _ = *ident == &&&string; + } +}