Skip to content

Commit 8826298

Browse files
committed
Show SyntaxContext in formatted Span debug output
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.
1 parent fd4b177 commit 8826298

File tree

8 files changed

+75
-47
lines changed

8 files changed

+75
-47
lines changed

src/librustc_interface/callbacks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::fmt;
1818
fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1919
tls::with_opt(|tcx| {
2020
if let Some(tcx) = tcx {
21-
write!(f, "{}", tcx.sess.source_map().span_to_string(span))
21+
rustc_span::debug_with_source_map(span, f, tcx.sess.source_map())
2222
} else {
2323
rustc_span::default_span_debug(span, f)
2424
}

src/librustc_middle/mir/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24492449
tcx.def_path_str_with_substs(def_id.to_def_id(), substs),
24502450
)
24512451
} else {
2452-
format!("[closure@{:?}]", tcx.hir().span(hir_id))
2452+
let span = tcx.hir().span(hir_id);
2453+
format!("[closure@{}]", tcx.sess.source_map().span_to_string(span))
24532454
};
24542455
let mut struct_fmt = fmt.debug_struct(&name);
24552456

src/librustc_middle/ty/print/pretty.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ pub trait PrettyPrinter<'tcx>:
605605
// FIXME(eddyb) should use `def_span`.
606606
if let Some(did) = did.as_local() {
607607
let hir_id = self.tcx().hir().as_local_hir_id(did);
608-
p!(write("@{:?}", self.tcx().hir().span(hir_id)));
608+
let span = self.tcx().hir().span(hir_id);
609+
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span)));
609610

610611
if substs.as_generator().is_valid() {
611612
let upvar_tys = substs.as_generator().upvar_tys();
@@ -653,7 +654,8 @@ pub trait PrettyPrinter<'tcx>:
653654
if self.tcx().sess.opts.debugging_opts.span_free_formats {
654655
p!(write("@"), print_def_path(did.to_def_id(), substs));
655656
} else {
656-
p!(write("@{:?}", self.tcx().hir().span(hir_id)));
657+
let span = self.tcx().hir().span(hir_id);
658+
p!(write("@{}", self.tcx().sess.source_map().span_to_string(span)));
657659
}
658660

659661
if substs.as_closure().is_valid() {
@@ -1362,7 +1364,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
13621364
if !self.empty_path {
13631365
write!(self, "::")?;
13641366
}
1365-
write!(self, "<impl at {:?}>", span)?;
1367+
write!(self, "<impl at {}>", self.tcx.sess.source_map().span_to_string(span))?;
13661368
self.empty_path = false;
13671369

13681370
return Ok(self);

src/librustc_mir/borrow_check/nll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
314314
infcx: &InferCtxt<'a, 'tcx>,
315315
source: MirSource<'tcx>,
316316
body: &Body<'tcx>,
317-
regioncx: &RegionInferenceContext<'_>,
317+
regioncx: &RegionInferenceContext<'tcx>,
318318
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
319319
) {
320320
if !mir_util::dump_enabled(infcx.tcx, "nll", source.def_id()) {
@@ -325,7 +325,7 @@ pub(super) fn dump_mir_results<'a, 'tcx>(
325325
match pass_where {
326326
// Before the CFG, dump out the values for each region variable.
327327
PassWhere::BeforeCFG => {
328-
regioncx.dump_mir(out)?;
328+
regioncx.dump_mir(infcx.tcx, out)?;
329329
writeln!(out, "|")?;
330330

331331
if let Some(closure_region_requirements) = closure_region_requirements {

src/librustc_mir/borrow_check/region_infer/dump_mir.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//! context internal state.
55
66
use super::{OutlivesConstraint, RegionInferenceContext};
7+
use crate::borrow_check::type_check::Locations;
78
use rustc_infer::infer::NLLRegionVariableOrigin;
9+
use rustc_middle::ty::TyCtxt;
810
use std::io::{self, Write};
911

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

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

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

4951
writeln!(out, "|")?;
5052
writeln!(out, "| Inference Constraints")?;
51-
self.for_each_constraint(&mut |msg| writeln!(out, "| {}", msg))?;
53+
self.for_each_constraint(tcx, &mut |msg| writeln!(out, "| {}", msg))?;
5254

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

7885
Ok(())

src/librustc_mir/util/pretty.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn dump_matched_mir_node<'tcx, F>(
135135
}
136136
writeln!(file)?;
137137
extra_data(PassWhere::BeforeCFG, &mut file)?;
138-
write_user_type_annotations(body, &mut file)?;
138+
write_user_type_annotations(tcx, body, &mut file)?;
139139
write_mir_fn(tcx, source, body, &mut extra_data, &mut file)?;
140140
extra_data(PassWhere::AfterCFG, &mut file)?;
141141
};
@@ -351,7 +351,7 @@ fn write_extra<'tcx, F>(tcx: TyCtxt<'tcx>, write: &mut dyn Write, mut visit_op:
351351
where
352352
F: FnMut(&mut ExtraComments<'tcx>),
353353
{
354-
let mut extra_comments = ExtraComments { _tcx: tcx, comments: vec![] };
354+
let mut extra_comments = ExtraComments { tcx, comments: vec![] };
355355
visit_op(&mut extra_comments);
356356
for comment in extra_comments.comments {
357357
writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
@@ -360,7 +360,7 @@ where
360360
}
361361

362362
struct ExtraComments<'tcx> {
363-
_tcx: TyCtxt<'tcx>, // don't need it now, but bet we will soon
363+
tcx: TyCtxt<'tcx>,
364364
comments: Vec<String>,
365365
}
366366

@@ -377,7 +377,7 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
377377
self.super_constant(constant, location);
378378
let Constant { span, user_ty, literal } = constant;
379379
self.push("mir::Constant");
380-
self.push(&format!("+ span: {:?}", span));
380+
self.push(&format!("+ span: {}", self.tcx.sess.source_map().span_to_string(*span)));
381381
if let Some(user_ty) = user_ty {
382382
self.push(&format!("+ user_ty: {:?}", user_ty));
383383
}
@@ -862,12 +862,22 @@ fn write_mir_sig(
862862
Ok(())
863863
}
864864

865-
fn write_user_type_annotations(body: &Body<'_>, w: &mut dyn Write) -> io::Result<()> {
865+
fn write_user_type_annotations(
866+
tcx: TyCtxt<'_>,
867+
body: &Body<'_>,
868+
w: &mut dyn Write,
869+
) -> io::Result<()> {
866870
if !body.user_type_annotations.is_empty() {
867871
writeln!(w, "| User Type Annotations")?;
868872
}
869873
for (index, annotation) in body.user_type_annotations.iter_enumerated() {
870-
writeln!(w, "| {:?}: {:?} at {:?}", index.index(), annotation.user_ty, annotation.span)?;
874+
writeln!(
875+
w,
876+
"| {:?}: {:?} at {}",
877+
index.index(),
878+
annotation.user_ty,
879+
tcx.sess.source_map().span_to_string(annotation.span)
880+
)?;
871881
}
872882
if !body.user_type_annotations.is_empty() {
873883
writeln!(w, "|")?;

src/librustc_span/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,18 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
726726
f()
727727
}
728728

729+
pub fn debug_with_source_map(
730+
span: Span,
731+
f: &mut fmt::Formatter<'_>,
732+
source_map: &SourceMap,
733+
) -> fmt::Result {
734+
write!(f, "{} ({:?})", source_map.span_to_string(span), span.ctxt())
735+
}
736+
729737
pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
730738
GLOBALS.with(|globals| {
731739
if let Some(source_map) = &*globals.source_map.borrow() {
732-
write!(f, "{}", source_map.span_to_string(span))
740+
debug_with_source_map(span, f, source_map)
733741
} else {
734742
f.debug_struct("Span")
735743
.field("lo", &span.lo())

0 commit comments

Comments
 (0)