Skip to content

Commit

Permalink
Make SpanId formatting transparent.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed May 13, 2020
1 parent 5cb1a18 commit 3e7b0b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/librustc_interface/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use std::fmt;

/// This is a callback from librustc_ast as it cannot access the implicit state
/// in librustc_middle otherwise.
fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn span_debug(span: rustc_span::SpanId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
tls::with_opt(|tcx| {
if let Some(tcx) = tcx {
let span = tcx.reify_span(span);
write!(f, "{}", tcx.sess.source_map().span_to_string(span))
} else {
rustc_span::default_span_debug(span, f)
Expand Down
30 changes: 20 additions & 10 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl Ord for Span {
}
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
#[derive(HashStable_Generic)]
pub enum SpanId {
Span(Span),
Expand Down Expand Up @@ -685,23 +685,33 @@ impl rustc_serialize::UseSpecializedDecodable for Span {
}
}

pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Span")
.field("lo", &span.lo())
.field("hi", &span.hi())
.field("ctxt", &span.ctxt())
.finish()
pub fn default_span_debug(span: SpanId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match span {
SpanId::Span(span) => f
.debug_struct("Span")
.field("lo", &span.lo())
.field("hi", &span.hi())
.field("ctxt", &span.ctxt())
.finish(),
SpanId::DefId(did) => f.debug_tuple("DefId").field(&did).finish(),
}
}

impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*SPAN_DEBUG)(*self, f)
(*SPAN_DEBUG)(SpanId::Span(*self), f)
}
}

impl fmt::Debug for SpanData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt), f)
(*SPAN_DEBUG)(SpanId::Span(Span::new(self.lo, self.hi, self.ctxt)), f)
}
}

impl fmt::Debug for SpanId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*SPAN_DEBUG)(*self, f)
}
}

Expand Down Expand Up @@ -1637,7 +1647,7 @@ pub struct FileLines {
pub lines: Vec<LineInfo>,
}

pub static SPAN_DEBUG: AtomicRef<fn(Span, &mut fmt::Formatter<'_>) -> fmt::Result> =
pub static SPAN_DEBUG: AtomicRef<fn(SpanId, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));

// _____________________________________________________________________________
Expand Down

0 comments on commit 3e7b0b2

Please sign in to comment.