@@ -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 ) ]
2726pub 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 ) ]
6449pub enum MetaVarKind {
@@ -114,7 +99,7 @@ impl fmt::Display for MetaVarKind {
11499/// Describes how a sequence of token trees is delimited.
115100/// Cannot use `proc_macro::Delimiter` directly because this
116101/// 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 ) ]
118103pub enum Delimiter {
119104 /// `( ... )`
120105 Parenthesis ,
@@ -130,6 +115,20 @@ pub enum Delimiter {
130115 Invisible ( InvisibleOrigin ) ,
131116}
132117
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+
133132impl Delimiter {
134133 // Should the parser skip these delimiters? Only happens for certain kinds
135134 // of invisible delimiters. Ideally this function will eventually disappear
@@ -142,7 +141,8 @@ impl Delimiter {
142141 }
143142 }
144143
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.
146146 pub fn eq_ignoring_invisible_origin ( & self , other : & Delimiter ) -> bool {
147147 match ( self , other) {
148148 ( Delimiter :: Parenthesis , Delimiter :: Parenthesis ) => true ,
@@ -153,6 +153,18 @@ impl Delimiter {
153153 }
154154 }
155155
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+
156168 pub fn as_open_token_kind ( & self ) -> TokenKind {
157169 match * self {
158170 Delimiter :: Parenthesis => OpenParen ,
0 commit comments