@@ -22,8 +22,7 @@ pub enum CommentKind {
22
22
Block ,
23
23
}
24
24
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 ) ]
27
26
pub enum InvisibleOrigin {
28
27
// From the expansion of a metavariable in a declarative macro.
29
28
MetaVar ( MetaVarKind ) ,
@@ -45,20 +44,6 @@ impl InvisibleOrigin {
45
44
}
46
45
}
47
46
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
-
62
47
/// Annoyingly similar to `NonterminalKind`, but the slight differences are important.
63
48
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
64
49
pub enum MetaVarKind {
@@ -114,7 +99,7 @@ impl fmt::Display for MetaVarKind {
114
99
/// Describes how a sequence of token trees is delimited.
115
100
/// Cannot use `proc_macro::Delimiter` directly because this
116
101
/// structure should implement some additional traits.
117
- #[ derive( Copy , Clone , Debug , PartialEq , Encodable , Decodable , HashStable_Generic ) ]
102
+ #[ derive( Copy , Clone , Debug , Encodable , Decodable , HashStable_Generic ) ]
118
103
pub enum Delimiter {
119
104
/// `( ... )`
120
105
Parenthesis ,
@@ -130,6 +115,20 @@ pub enum Delimiter {
130
115
Invisible ( InvisibleOrigin ) ,
131
116
}
132
117
118
+ impl PartialEq for Delimiter {
119
+ fn eq ( & self , other : & Self ) -> bool {
120
+ match ( self , other) {
121
+ ( Delimiter :: Parenthesis , Delimiter :: Parenthesis ) => true ,
122
+ ( Delimiter :: Brace , Delimiter :: Brace ) => true ,
123
+ ( Delimiter :: Bracket , Delimiter :: Bracket ) => true ,
124
+ ( Delimiter :: Invisible ( _) , _) | ( _, Delimiter :: Invisible ( _) ) => {
125
+ panic ! ( "Comparing an invisible delimiter using PartialEq" ) ;
126
+ }
127
+ _ => false ,
128
+ }
129
+ }
130
+ }
131
+
133
132
impl Delimiter {
134
133
// Should the parser skip these delimiters? Only happens for certain kinds
135
134
// of invisible delimiters. Ideally this function will eventually disappear
@@ -142,7 +141,8 @@ impl Delimiter {
142
141
}
143
142
}
144
143
145
- // This exists because `InvisibleOrigin`s should be compared. It is only used for assertions.
144
+ // This exists because `InvisibleOrigin`s should not be compared. It is only used for
145
+ // assertions.
146
146
pub fn eq_ignoring_invisible_origin ( & self , other : & Delimiter ) -> bool {
147
147
match ( self , other) {
148
148
( Delimiter :: Parenthesis , Delimiter :: Parenthesis ) => true ,
@@ -153,6 +153,18 @@ impl Delimiter {
153
153
}
154
154
}
155
155
156
+ /// Compare two delimiters while always considering invisible delimiters to NOT be equal
157
+ /// to anything else.
158
+ pub fn eq_special_invisible_origin ( & self , other : & Delimiter ) -> bool {
159
+ match ( self , other) {
160
+ ( Delimiter :: Parenthesis , Delimiter :: Parenthesis ) => true ,
161
+ ( Delimiter :: Brace , Delimiter :: Brace ) => true ,
162
+ ( Delimiter :: Bracket , Delimiter :: Bracket ) => true ,
163
+ ( Delimiter :: Invisible ( _) , _) | ( _, Delimiter :: Invisible ( _) ) => false ,
164
+ _ => false ,
165
+ }
166
+ }
167
+
156
168
pub fn as_open_token_kind ( & self ) -> TokenKind {
157
169
match * self {
158
170
Delimiter :: Parenthesis => OpenParen ,
0 commit comments