@@ -123,7 +123,7 @@ impl Sub for CharPos {
123123/// able to use many of the functions on spans in codemap and you cannot assume
124124/// that the length of the span = hi - lo; there may be space in the BytePos
125125/// range between files.
126- #[ derive( Clone , Copy , Hash ) ]
126+ #[ derive( Clone , Copy , Hash , PartialEq , Eq ) ]
127127pub struct Span {
128128 pub lo : BytePos ,
129129 pub hi : BytePos ,
@@ -151,13 +151,21 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
151151impl Span {
152152 /// Returns `self` if `self` is not the dummy span, and `other` otherwise.
153153 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 }
155155 }
156156
157157 pub fn contains ( self , other : Span ) -> bool {
158158 self . lo <= other. lo && other. hi <= self . hi
159159 }
160160
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+
161169 /// Returns `Some(span)`, a union of `self` and `other`, on overlap.
162170 pub fn merge ( self , other : Span ) -> Option < Span > {
163171 if self . expn_id != other. expn_id {
@@ -192,15 +200,6 @@ pub struct Spanned<T> {
192200 pub span : Span ,
193201}
194202
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-
204203impl Encodable for Span {
205204 fn encode < S : Encoder > ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
206205 s. emit_struct ( "Span" , 2 , |s| {
@@ -940,7 +939,7 @@ impl CodeMap {
940939 }
941940
942941 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 ) {
944943 return "no-location" . to_string ( ) ;
945944 }
946945
@@ -1307,7 +1306,7 @@ impl CodeMap {
13071306 expninfo. map_or ( /* hit the top level */ true , |info| {
13081307
13091308 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| {
13111310 mac_span. contains ( span)
13121311 } ) ;
13131312
0 commit comments