Skip to content

Commit

Permalink
Show SyntaxContext in formatted Span debug output
Browse files Browse the repository at this point in the history
This is only really useful in debug messages, so I've switched to
calling `span_to_string` in any place that causes a `Span` to end up in
user-visible output.
  • Loading branch information
Aaron1011 committed Jun 9, 2020
1 parent fd4b177 commit 8826298
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/librustc_interface/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::fmt;
fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
tls::with_opt(|tcx| {
if let Some(tcx) = tcx {
write!(f, "{}", tcx.sess.source_map().span_to_string(span))
rustc_span::debug_with_source_map(span, f, tcx.sess.source_map())
} else {
rustc_span::default_span_debug(span, f)
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_middle/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
)
} else {
format!("[closure@{:?}]", tcx.hir().span(hir_id))
let span = tcx.hir().span(hir_id);
format!("[closure@{}]", tcx.sess.source_map().span_to_string(span))
};
let mut struct_fmt = fmt.debug_struct(&name);

Expand Down
8 changes: 5 additions & 3 deletions src/librustc_middle/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ pub trait PrettyPrinter<'tcx>:
// FIXME(eddyb) should use `def_span`.
if let Some(did) = did.as_local() {
let hir_id = self.tcx().hir().as_local_hir_id(did);
p!(write("@{:?}", self.tcx().hir().span(hir_id)));
let span = self.tcx().hir().span(hir_id);
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span)));

if substs.as_generator().is_valid() {
let upvar_tys = substs.as_generator().upvar_tys();
Expand Down Expand Up @@ -653,7 +654,8 @@ pub trait PrettyPrinter<'tcx>:
if self.tcx().sess.opts.debugging_opts.span_free_formats {
p!(write("@"), print_def_path(did.to_def_id(), substs));
} else {
p!(write("@{:?}", self.tcx().hir().span(hir_id)));
let span = self.tcx().hir().span(hir_id);
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span)));
}

if substs.as_closure().is_valid() {
Expand Down Expand Up @@ -1362,7 +1364,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
if !self.empty_path {
write!(self, "::")?;
}
write!(self, "<impl at {:?}>", span)?;
write!(self, "<impl at {}>", self.tcx.sess.source_map().span_to_string(span))?;
self.empty_path = false;

return Ok(self);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
infcx: &InferCtxt<'a, 'tcx>,
source: MirSource<'tcx>,
body: &Body<'tcx>,
regioncx: &RegionInferenceContext<'_>,
regioncx: &RegionInferenceContext<'tcx>,
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
) {
if !mir_util::dump_enabled(infcx.tcx, "nll", source.def_id()) {
Expand All @@ -325,7 +325,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
match pass_where {
// Before the CFG, dump out the values for each region variable.
PassWhere::BeforeCFG => {
regioncx.dump_mir(out)?;
regioncx.dump_mir(infcx.tcx, out)?;
writeln!(out, "|")?;

if let Some(closure_region_requirements) = closure_region_requirements {
Expand Down
13 changes: 10 additions & 3 deletions src/librustc_mir/borrow_check/region_infer/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! context internal state.
use super::{OutlivesConstraint, RegionInferenceContext};
use crate::borrow_check::type_check::Locations;
use rustc_infer::infer::NLLRegionVariableOrigin;
use rustc_middle::ty::TyCtxt;
use std::io::{self, Write};

// Room for "'_#NNNNr" before things get misaligned.
Expand All @@ -14,7 +16,7 @@ const REGION_WIDTH: usize = 8;

impl<'tcx> RegionInferenceContext<'tcx> {
/// Write out our state into the `.mir` files.
pub(crate) fn dump_mir(&self, out: &mut dyn Write) -> io::Result<()> {
pub(crate) fn dump_mir(&self, tcx: TyCtxt<'tcx>, out: &mut dyn Write) -> io::Result<()> {
writeln!(out, "| Free Region Mapping")?;

for region in self.regions() {
Expand Down Expand Up @@ -48,7 +50,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {

writeln!(out, "|")?;
writeln!(out, "| Inference Constraints")?;
self.for_each_constraint(&mut |msg| writeln!(out, "| {}", msg))?;
self.for_each_constraint(tcx, &mut |msg| writeln!(out, "| {}", msg))?;

Ok(())
}
Expand All @@ -59,6 +61,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// inference resulted in the values that it did when debugging.
fn for_each_constraint(
&self,
tcx: TyCtxt<'tcx>,
with_msg: &mut dyn FnMut(&str) -> io::Result<()>,
) -> io::Result<()> {
for region in self.definitions.indices() {
Expand All @@ -72,7 +75,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
constraints.sort();
for constraint in &constraints {
let OutlivesConstraint { sup, sub, locations, category } = constraint;
with_msg(&format!("{:?}: {:?} due to {:?} at {:?}", sup, sub, category, locations,))?;
let (name, arg) = match locations {
Locations::All(span) => ("All", tcx.sess.source_map().span_to_string(*span)),
Locations::Single(loc) => ("Single", format!("{:?}", loc)),
};
with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?;
}

Ok(())
Expand Down
22 changes: 16 additions & 6 deletions src/librustc_mir/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn dump_matched_mir_node<'tcx, F>(
}
writeln!(file)?;
extra_data(PassWhere::BeforeCFG, &mut file)?;
write_user_type_annotations(body, &mut file)?;
write_user_type_annotations(tcx, body, &mut file)?;
write_mir_fn(tcx, source, body, &mut extra_data, &mut file)?;
extra_data(PassWhere::AfterCFG, &mut file)?;
};
Expand Down Expand Up @@ -351,7 +351,7 @@ fn write_extra<'tcx, F>(tcx: TyCtxt<'tcx>, write: &mut dyn Write, mut visit_op:
where
F: FnMut(&mut ExtraComments<'tcx>),
{
let mut extra_comments = ExtraComments { _tcx: tcx, comments: vec![] };
let mut extra_comments = ExtraComments { tcx, comments: vec![] };
visit_op(&mut extra_comments);
for comment in extra_comments.comments {
writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
Expand All @@ -360,7 +360,7 @@ where
}

struct ExtraComments<'tcx> {
_tcx: TyCtxt<'tcx>, // don't need it now, but bet we will soon
tcx: TyCtxt<'tcx>,
comments: Vec<String>,
}

Expand All @@ -377,7 +377,7 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
self.super_constant(constant, location);
let Constant { span, user_ty, literal } = constant;
self.push("mir::Constant");
self.push(&format!("+ span: {:?}", span));
self.push(&format!("+ span: {}", self.tcx.sess.source_map().span_to_string(*span)));
if let Some(user_ty) = user_ty {
self.push(&format!("+ user_ty: {:?}", user_ty));
}
Expand Down Expand Up @@ -862,12 +862,22 @@ fn write_mir_sig(
Ok(())
}

fn write_user_type_annotations(body: &Body<'_>, w: &mut dyn Write) -> io::Result<()> {
fn write_user_type_annotations(
tcx: TyCtxt<'_>,
body: &Body<'_>,
w: &mut dyn Write,
) -> io::Result<()> {
if !body.user_type_annotations.is_empty() {
writeln!(w, "| User Type Annotations")?;
}
for (index, annotation) in body.user_type_annotations.iter_enumerated() {
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation.user_ty, annotation.span)?;
writeln!(
w,
"| {:?}: {:?} at {}",
index.index(),
annotation.user_ty,
tcx.sess.source_map().span_to_string(annotation.span)
)?;
}
if !body.user_type_annotations.is_empty() {
writeln!(w, "|")?;
Expand Down
10 changes: 9 additions & 1 deletion src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,18 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
f()
}

pub fn debug_with_source_map(
span: Span,
f: &mut fmt::Formatter<'_>,
source_map: &SourceMap,
) -> fmt::Result {
write!(f, "{} ({:?})", source_map.span_to_string(span), span.ctxt())
}

pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
GLOBALS.with(|globals| {
if let Some(source_map) = &*globals.source_map.borrow() {
write!(f, "{}", source_map.span_to_string(span))
debug_with_source_map(span, f, source_map)
} else {
f.debug_struct("Span")
.field("lo", &span.lo())
Expand Down
Loading

0 comments on commit 8826298

Please sign in to comment.