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

Commit 459b67a

Browse files
Rollup merge of #146090 - Kobzol:invisible-origin-eq, r=petrochenkov
Derive `PartialEq` for `InvisibleOrigin` For #145354, we need `PartialEq` for `TokenStream` to "just work". However, due to the special comparison implementation that was used for `InvisibleOrigin`, this wasn't the case. So I derived `PartialEq` for `InvisibleOrigin`, and used the previous special comparison logic only on the single place where it was actually required. r? ``@petrochenkov``
2 parents 4fec477 + a8537ab commit 459b67a

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

‎compiler/rustc_ast/src/token.rs‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ pub enum CommentKind {
2222
Block,
2323
}
2424

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

48-
impl PartialEq for InvisibleOrigin {
49-
#[inline]
50-
fn eq(&self, _other: &InvisibleOrigin) -> bool {
51-
// When we had AST-based nonterminals we couldn't compare them, and the
52-
// old `Nonterminal` type had an `eq` that always returned false,
53-
// resulting in this restriction:
54-
// https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment
55-
// This `eq` emulates that behaviour. We could consider lifting this
56-
// restriction now but there are still cases involving invisible
57-
// delimiters that make it harder than it first appears.
58-
false
59-
}
60-
}
61-
6247
/// Annoyingly similar to `NonterminalKind`, but the slight differences are important.
6348
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
6449
pub enum MetaVarKind {
@@ -142,7 +127,8 @@ impl Delimiter {
142127
}
143128
}
144129

145-
// This exists because `InvisibleOrigin`s should be compared. It is only used for assertions.
130+
// This exists because `InvisibleOrigin`s should not be compared. It is only used for
131+
// assertions.
146132
pub fn eq_ignoring_invisible_origin(&self, other: &Delimiter) -> bool {
147133
match (self, other) {
148134
(Delimiter::Parenthesis, Delimiter::Parenthesis) => true,

‎compiler/rustc_expand/src/mbe/macro_parser.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use std::rc::Rc;
7777

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

0 commit comments

Comments
(0)

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