Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PartialEq for proc_macro::Ident == strings #78634

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
25 changes: 25 additions & 0 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,31 @@ impl fmt::Debug for Ident {
}
}

#[stable(feature = "proc_macro_ident_eq", since = "1.49.0")]
impl PartialEq<str> for Ident {
fn eq(&self, rhs: &str) -> bool {
self.0.eq(rhs)
}
}

#[stable(feature = "proc_macro_ident_eq", since = "1.49.0")]
impl PartialEq<String> for Ident {
fn eq(&self, rhs: &String) -> bool {
<Self as PartialEq<str>>::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<T>,
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
{
fn eq(&self, rhs: &&T) -> bool {
<Self as PartialEq<T>>::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`).
Expand Down
13 changes: 12 additions & 1 deletion library/proc_macro/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(proc_macro_span)]

use proc_macro::LineColumn;
use proc_macro::{Ident, LineColumn};

#[test]
fn test_line_column_ord() {
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some tests for raw identifiers?

}
}