Skip to content

Commit aaab14d

Browse files
committed
Auto merge of #31277 - DanielJCampbell:SpanEquality, r=nrc
r? @nrc
2 parents 14dc9fc + e1be504 commit aaab14d

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

src/librustc_resolve/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a, 'b, 'v, 'tcx> Visitor<'v> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
117117
// whether they're used or not. Also ignore imports with a dummy span
118118
// because this means that they were generated in some fashion by the
119119
// compiler and we don't need to consider them.
120-
if item.vis == hir::Public || item.span == DUMMY_SP {
120+
if item.vis == hir::Public || item.span.source_equal(&DUMMY_SP) {
121121
return;
122122
}
123123

src/libsyntax/ast_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub fn pat_is_ident(pat: P<ast::Pat>) -> bool {
356356
// since I'm using this to replace ==, it seems appropriate
357357
// to compare the span, global, etc. fields as well.
358358
pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool {
359-
(a.span == b.span)
359+
(a.span.source_equal(&b.span))
360360
&& (a.global == b.global)
361361
&& (segments_name_eq(&a.segments[..], &b.segments[..]))
362362
}

src/libsyntax/codemap.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -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)]
127127
pub struct Span {
128128
pub lo: BytePos,
129129
pub hi: BytePos,
@@ -151,13 +151,21 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
151151
impl 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-
204203
impl 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

src/libsyntax/errors/emitter.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use self::Destination::*;
1212

13-
use codemap::{self, COMMAND_LINE_SP, COMMAND_LINE_EXPN, DUMMY_SP, Pos, Span, MultiSpan};
13+
use codemap::{self, COMMAND_LINE_SP, DUMMY_SP, Pos, Span, MultiSpan};
1414
use diagnostics;
1515

1616
use errors::{Level, RenderSpan, CodeSuggestion, DiagnosticBuilder};
@@ -175,9 +175,7 @@ impl EmitterWriter {
175175
let msp = rsp.span();
176176
let bounds = msp.to_span_bounds();
177177

178-
// We cannot check equality directly with COMMAND_LINE_SP
179-
// since PartialEq is manually implemented to ignore the ExpnId
180-
let ss = if bounds.expn_id == COMMAND_LINE_EXPN {
178+
let ss = if bounds == COMMAND_LINE_SP {
181179
"<command line option>".to_string()
182180
} else if let EndSpan(_) = *rsp {
183181
let span_end = Span { lo: bounds.hi, hi: bounds.hi, expn_id: bounds.expn_id};
@@ -606,7 +604,7 @@ impl EmitterWriter {
606604
};
607605

608606
// Don't print recursive invocations
609-
if span != last_span {
607+
if !span.source_equal(&last_span) {
610608
let mut diag_string = macro_decl_name;
611609
if let Some(def_site_span) = def_site_span {
612610
diag_string.push_str(&format!(" (defined in {})",

0 commit comments

Comments
 (0)