Skip to content

Commit

Permalink
add and use Span.substitute_dummy method
Browse files Browse the repository at this point in the history
  • Loading branch information
TimNN committed Aug 10, 2015
1 parent c115c51 commit d46e840
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
8 changes: 2 additions & 6 deletions src/librustc/middle/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use middle::ty::{self, ToPredicate, HasTypeFlags, ToPolyTraitRef, TraitRef};
use middle::ty_fold::TypeFoldable;
use std::collections::HashMap;
use std::fmt;
use syntax::codemap::{DUMMY_SP, Span};
use syntax::codemap::Span;
use syntax::attr::{AttributeMethods, AttrMetaMethods};

pub fn report_fulfillment_errors<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
Expand Down Expand Up @@ -81,11 +81,7 @@ fn report_on_unimplemented<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
let mut report = None;
for item in infcx.tcx.get_attrs(def_id).iter() {
if item.check_name("rustc_on_unimplemented") {
let err_sp = if item.meta().span == DUMMY_SP {
span
} else {
item.meta().span
};
let err_sp = item.meta().span.substitute_dummy(span);
let def = infcx.tcx.lookup_trait_def(def_id);
let trait_str = def.trait_ref.to_string();
if let Some(ref istring) = item.value_str() {
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ pub const COMMAND_LINE_SP: Span = Span { lo: BytePos(0),
hi: BytePos(0),
expn_id: COMMAND_LINE_EXPN };

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 }
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub struct Spanned<T> {
pub node: T,
Expand Down
23 changes: 6 additions & 17 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,16 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
best_fail_spot = sp;
best_fail_msg = (*msg).clone();
},
Error(mut spp, ref msg) => {
if spp == DUMMY_SP {
spp = sp;
}

panic!(cx.span_fatal(spp, &msg[..]))
Error(err_sp, ref msg) => {
panic!(cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]))
}
}
}
_ => cx.bug("non-matcher found in parsed lhses")
}
}

if best_fail_spot == DUMMY_SP {
best_fail_spot = sp;
}

panic!(cx.span_fatal(best_fail_spot, &best_fail_msg[..]));
panic!(cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]));
}

// Note that macro-by-example's input is also matched against a token tree:
Expand Down Expand Up @@ -283,12 +275,9 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
arg_reader,
&argument_gram) {
Success(m) => m,
Failure(mut sp, str) | Error(mut sp, str) => {
if sp == DUMMY_SP {
sp = def.span;
}

panic!(cx.parse_sess().span_diagnostic.span_fatal(sp, &str[..]));
Failure(sp, str) | Error(sp, str) => {
panic!(cx.parse_sess().span_diagnostic
.span_fatal(sp.substitute_dummy(def.span), &str[..]));
}
};

Expand Down

0 comments on commit d46e840

Please sign in to comment.