Skip to content

Commit d46e840

Browse files
committed
add and use Span.substitute_dummy method
1 parent c115c51 commit d46e840

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

src/librustc/middle/traits/error_reporting.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use middle::ty::{self, ToPredicate, HasTypeFlags, ToPolyTraitRef, TraitRef};
2929
use middle::ty_fold::TypeFoldable;
3030
use std::collections::HashMap;
3131
use std::fmt;
32-
use syntax::codemap::{DUMMY_SP, Span};
32+
use syntax::codemap::Span;
3333
use syntax::attr::{AttributeMethods, AttrMetaMethods};
3434

3535
pub fn report_fulfillment_errors<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
@@ -81,11 +81,7 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
8181
let mut report = None;
8282
for item in infcx.tcx.get_attrs(def_id).iter() {
8383
if item.check_name("rustc_on_unimplemented") {
84-
let err_sp = if item.meta().span == DUMMY_SP {
85-
span
86-
} else {
87-
item.meta().span
88-
};
84+
let err_sp = item.meta().span.substitute_dummy(span);
8985
let def = infcx.tcx.lookup_trait_def(def_id);
9086
let trait_str = def.trait_ref.to_string();
9187
if let Some(ref istring) = item.value_str() {

src/libsyntax/codemap.rs

+7
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
135135
hi: BytePos(0),
136136
expn_id: COMMAND_LINE_EXPN };
137137

138+
impl Span {
139+
/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
140+
pub fn substitute_dummy(self, other: Span) -> Span {
141+
if self == DUMMY_SP { other } else { self }
142+
}
143+
}
144+
138145
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
139146
pub struct Spanned<T> {
140147
pub node: T,

src/libsyntax/ext/tt/macro_rules.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -211,24 +211,16 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
211211
best_fail_spot = sp;
212212
best_fail_msg = (*msg).clone();
213213
},
214-
Error(mut spp, ref msg) => {
215-
if spp == DUMMY_SP {
216-
spp = sp;
217-
}
218-
219-
panic!(cx.span_fatal(spp, &msg[..]))
214+
Error(err_sp, ref msg) => {
215+
panic!(cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]))
220216
}
221217
}
222218
}
223219
_ => cx.bug("non-matcher found in parsed lhses")
224220
}
225221
}
226222

227-
if best_fail_spot == DUMMY_SP {
228-
best_fail_spot = sp;
229-
}
230-
231-
panic!(cx.span_fatal(best_fail_spot, &best_fail_msg[..]));
223+
panic!(cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]));
232224
}
233225

234226
// Note that macro-by-example's input is also matched against a token tree:
@@ -283,12 +275,9 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
283275
arg_reader,
284276
&argument_gram) {
285277
Success(m) => m,
286-
Failure(mut sp, str) | Error(mut sp, str) => {
287-
if sp == DUMMY_SP {
288-
sp = def.span;
289-
}
290-
291-
panic!(cx.parse_sess().span_diagnostic.span_fatal(sp, &str[..]));
278+
Failure(sp, str) | Error(sp, str) => {
279+
panic!(cx.parse_sess().span_diagnostic
280+
.span_fatal(sp.substitute_dummy(def.span), &str[..]));
292281
}
293282
};
294283

0 commit comments

Comments
 (0)