@@ -123,7 +123,7 @@ impl Sub for CharPos {
123
123
/// able to use many of the functions on spans in codemap and you cannot assume
124
124
/// that the length of the span = hi - lo; there may be space in the BytePos
125
125
/// range between files.
126
- #[ derive( Clone , Copy , Hash ) ]
126
+ #[ derive( Clone , Copy , Hash , PartialEq , Eq ) ]
127
127
pub struct Span {
128
128
pub lo : BytePos ,
129
129
pub hi : BytePos ,
@@ -151,13 +151,21 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
151
151
impl Span {
152
152
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
153
153
pub fn substitute_dummy ( self , other : Span ) -> Span {
154
- if self == DUMMY_SP { other } else { self }
154
+ if self . source_equal ( & DUMMY_SP ) { other } else { self }
155
155
}
156
156
157
157
pub fn contains ( self , other : Span ) -> bool {
158
158
self . lo <= other. lo && other. hi <= self . hi
159
159
}
160
160
161
+ /// Return true if the spans are equal with regards to the source text.
162
+ ///
163
+ /// Use this instead of `==` when either span could be generated code,
164
+ /// and you only care that they point to the same bytes of source text.
165
+ pub fn source_equal ( & self , other : & Span ) -> bool {
166
+ self . lo == other. lo && self . hi == other. hi
167
+ }
168
+
161
169
/// Returns `Some(span)`, a union of `self` and `other`, on overlap.
162
170
pub fn merge ( self , other : Span ) -> Option < Span > {
163
171
if self . expn_id != other. expn_id {
@@ -192,15 +200,6 @@ pub struct Spanned<T> {
192
200
pub span : Span ,
193
201
}
194
202
195
- impl PartialEq for Span {
196
- fn eq ( & self , other : & Span ) -> bool {
197
- return ( * self ) . lo == ( * other) . lo && ( * self ) . hi == ( * other) . hi ;
198
- }
199
- fn ne ( & self , other : & Span ) -> bool { !( * self ) . eq ( other) }
200
- }
201
-
202
- impl Eq for Span { }
203
-
204
203
impl Encodable for Span {
205
204
fn encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
206
205
s. emit_struct ( "Span" , 2 , |s| {
@@ -940,7 +939,7 @@ impl CodeMap {
940
939
}
941
940
942
941
pub fn span_to_string ( & self , sp : Span ) -> String {
943
- if self . files . borrow ( ) . is_empty ( ) && sp == DUMMY_SP {
942
+ if self . files . borrow ( ) . is_empty ( ) && sp. source_equal ( & DUMMY_SP ) {
944
943
return "no-location" . to_string ( ) ;
945
944
}
946
945
@@ -1307,7 +1306,7 @@ impl CodeMap {
1307
1306
expninfo. map_or ( /* hit the top level */ true , |info| {
1308
1307
1309
1308
let span_comes_from_this_expansion =
1310
- info. callee . span . map_or ( span == info. call_site , |mac_span| {
1309
+ info. callee . span . map_or ( span. source_equal ( & info. call_site ) , |mac_span| {
1311
1310
mac_span. contains ( span)
1312
1311
} ) ;
1313
1312
0 commit comments