]> git.lizzy.rs Git - rust.git/commitdiff
Spans now derive equality
authorDaniel Campbell <campbelldj@hotmail.com>
Fri, 29 Jan 2016 06:33:14 +0000 (19:33 +1300)
committerDaniel Campbell <campbelldj@hotmail.com>
Sun, 31 Jan 2016 21:18:50 +0000 (10:18 +1300)
src/librustc_resolve/check_unused.rs
src/libsyntax/ast_util.rs
src/libsyntax/codemap.rs
src/libsyntax/errors/emitter.rs

index 7f740f9c03335a26d127c2b22096ef298f774ce2..178e2a4d1bc7882d816188cebf605b148cb579d2 100644 (file)
@@ -117,7 +117,7 @@ fn visit_item(&mut self, item: &hir::Item) {
         // whether they're used or not. Also ignore imports with a dummy span
         // because this means that they were generated in some fashion by the
         // compiler and we don't need to consider them.
-        if item.vis == hir::Public || item.span == DUMMY_SP {
+        if item.vis == hir::Public || item.span.source_equal(&DUMMY_SP) {
             return;
         }
 
index ba4d1e2193e5f0f49be54071dd73dca53783f282..03dc25e1b3c2bd3617cb8e0449dd39a6f055397f 100644 (file)
@@ -356,7 +356,7 @@ pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
 // since I'm using this to replace ==, it seems appropriate
 // to compare the span, global, etc. fields as well.
 pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {
-    (a.span == b.span)
+    (a.span.source_equal(&b.span))
     && (a.global == b.global)
     && (segments_name_eq(&a.segments[..], &b.segments[..]))
 }
index ca01623fef90ffeb0dcdc8cd1e7ebf6b90d70d91..9557310f318e9b4f3ef4cc9d04126c408a77eec3 100644 (file)
@@ -123,7 +123,7 @@ fn sub(self, rhs: CharPos) -> CharPos {
 /// able to use many of the functions on spans in codemap and you cannot assume
 /// that the length of the span = hi - lo; there may be space in the BytePos
 /// range between files.
-#[derive(Clone, Copy, Hash)]
+#[derive(Clone, Copy, Hash, PartialEq, Eq)]
 pub struct Span {
     pub lo: BytePos,
     pub hi: BytePos,
@@ -151,13 +151,21 @@ pub struct MultiSpan {
 impl Span {
     /// Returns `self` if `self` is not the dummy span, and `other` otherwise.
     pub fn substitute_dummy(self, other: Span) -> Span {
-        if self == DUMMY_SP { other } else { self }
+        if self.source_equal(&DUMMY_SP) { other } else { self }
     }
 
     pub fn contains(self, other: Span) -> bool {
         self.lo <= other.lo && other.hi <= self.hi
     }
 
+    /// Return true if the spans are equal with regards to the source text.
+    ///
+    /// Use this instead of `==` when either span could be generated code,
+    /// and you only care that they point to the same bytes of source text.
+    pub fn source_equal(&self, other: &Span) -> bool {
+        self.lo == other.lo && self.hi == other.hi
+    }
+
     /// Returns `Some(span)`, a union of `self` and `other`, on overlap.
     pub fn merge(self, other: Span) -> Option<Span> {
         if self.expn_id != other.expn_id {
@@ -192,15 +200,6 @@ pub struct Spanned<T> {
     pub span: Span,
 }
 
-impl PartialEq for Span {
-    fn eq(&self, other: &Span) -> bool {
-        return (*self).lo == (*other).lo && (*self).hi == (*other).hi;
-    }
-    fn ne(&self, other: &Span) -> bool { !(*self).eq(other) }
-}
-
-impl Eq for Span {}
-
 impl Encodable for Span {
     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
         s.emit_struct("Span", 2, |s| {
@@ -940,7 +939,7 @@ pub fn lookup_char_pos_adj(&self, pos: BytePos) -> LocWithOpt {
     }
 
     pub fn span_to_string(&self, sp: Span) -> String {
-        if self.files.borrow().is_empty() && sp == DUMMY_SP {
+        if self.files.borrow().is_empty() && sp.source_equal(&DUMMY_SP) {
             return "no-location".to_string();
         }
 
@@ -1307,7 +1306,7 @@ pub fn span_allows_unstable(&self, span: Span) -> bool {
                 expninfo.map_or(/* hit the top level */ true, |info| {
 
                     let span_comes_from_this_expansion =
-                        info.callee.span.map_or(span == info.call_site, |mac_span| {
+                        info.callee.span.map_or(span.source_equal(&info.call_site), |mac_span| {
                             mac_span.contains(span)
                         });
 
index c1239bfd66db82042d5f9b3e393e2cdd86999516..7e0e17423de8c36ae782e09a1a6aca67989b2ce8 100644 (file)
@@ -10,7 +10,7 @@
 
 use self::Destination::*;
 
-use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, DUMMY_SP, Pos, Span, MultiSpan};
+use codemap::{self, COMMAND_LINE_SP, DUMMY_SP, Pos, Span, MultiSpan};
 use diagnostics;
 
 use errors::{Level, RenderSpan, CodeSuggestion, DiagnosticBuilder};
@@ -175,9 +175,7 @@ fn emit_(&mut self,
         let msp = rsp.span();
         let bounds = msp.to_span_bounds();
 
-        // We cannot check equality directly with COMMAND_LINE_SP
-        // since PartialEq is manually implemented to ignore the ExpnId
-        let ss = if bounds.expn_id == COMMAND_LINE_EXPN {
+        let ss = if bounds == COMMAND_LINE_SP {
             "<command line option>".to_string()
         } else if let EndSpan(_) = *rsp {
             let span_end = Span { lo: bounds.hi, hi: bounds.hi, expn_id: bounds.expn_id};
@@ -606,7 +604,7 @@ fn print_macro_backtrace(&mut self,
             };
 
             // Don't print recursive invocations
-            if span != last_span {
+            if !span.source_equal(&last_span) {
                 let mut diag_string = macro_decl_name;
                 if let Some(def_site_span) = def_site_span {
                     diag_string.push_str(&format!(" (defined in {})",