Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Derive PartialEq for InvisibleOrigin #146090

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

Merged
bors merged 1 commit into rust-lang:master from Kobzol:invisible-origin-eq
Sep 4, 2025
Merged
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
20 changes: 3 additions & 17 deletions compiler/rustc_ast/src/token.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub enum CommentKind {
Block,
}

// This type must not implement `Hash` due to the unusual `PartialEq` impl below.
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic)]
#[derive(Copy, Clone, PartialEq, Debug, Encodable, Decodable, HashStable_Generic)]
pub enum InvisibleOrigin {
// From the expansion of a metavariable in a declarative macro.
MetaVar(MetaVarKind),
Expand All @@ -45,20 +44,6 @@ impl InvisibleOrigin {
}
}

impl PartialEq for InvisibleOrigin {
#[inline]
fn eq(&self, _other: &InvisibleOrigin) -> bool {
// When we had AST-based nonterminals we couldn't compare them, and the
// old `Nonterminal` type had an `eq` that always returned false,
// resulting in this restriction:
// https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment
// This `eq` emulates that behaviour. We could consider lifting this
// restriction now but there are still cases involving invisible
// delimiters that make it harder than it first appears.
false
}
}

/// Annoyingly similar to `NonterminalKind`, but the slight differences are important.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
pub enum MetaVarKind {
Expand Down Expand Up @@ -142,7 +127,8 @@ impl Delimiter {
}
}

// This exists because `InvisibleOrigin`s should be compared. It is only used for assertions.
// This exists because `InvisibleOrigin`s should not be compared. It is only used for
// assertions.
pub fn eq_ignoring_invisible_origin(&self, other: &Delimiter) -> bool {
match (self, other) {
(Delimiter::Parenthesis, Delimiter::Parenthesis) => true,
Expand Down
20 changes: 18 additions & 2 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use std::rc::Rc;

pub(crate) use NamedMatch::*;
pub(crate) use ParseResult::*;
use rustc_ast::token::{self, DocComment, NonterminalKind, Token};
use rustc_ast::token::{self, DocComment, NonterminalKind, Token,TokenKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_lint_defs::pluralize;
Expand Down Expand Up @@ -397,7 +397,23 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
{
ident1.name == ident2.name && is_raw1 == is_raw2
} else {
t1.kind == t2.kind
// Note: we SHOULD NOT use `t1.kind == t2.kind` here, and we should instead compare the
// tokens using the special comparison logic below.
// It makes sure that variants containing `InvisibleOrigin` will
// never compare equal to one another.
//
// When we had AST-based nonterminals we couldn't compare them, and the
// old `Nonterminal` type had an `eq` that always returned false,
// resulting in this restriction:
// <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment>
// This comparison logic emulates that behaviour. We could consider lifting this
// restriction now but there are still cases involving invisible
// delimiters that make it harder than it first appears.
match (t1.kind, t2.kind) {
(TokenKind::OpenInvisible(_) | TokenKind::CloseInvisible(_), _)
| (_, TokenKind::OpenInvisible(_) | TokenKind::CloseInvisible(_)) => false,
(a, b) => a == b,
}
}
}

Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /