未验证 提交 80fce369 编写于 作者: D Dylan DPC 提交者: GitHub

Rollup merge of #73012 - Aaron1011:feature/span-debug-ctxt, r=matthewjasper

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.
......@@ -18,7 +18,7 @@
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)
}
......
......@@ -2449,7 +2449,8 @@ fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
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);
......
......@@ -605,7 +605,8 @@ fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error>
// 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();
......@@ -653,7 +654,8 @@ fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error>
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() {
......@@ -1362,7 +1364,7 @@ fn print_def_path(
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);
......
......@@ -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()) {
......@@ -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 {
......
......@@ -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.
......@@ -14,7 +16,7 @@
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() {
......@@ -48,7 +50,7 @@ pub(crate) fn dump_mir(&self, out: &mut dyn Write) -> io::Result<()> {
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(())
}
......@@ -59,6 +61,7 @@ pub(crate) fn dump_mir(&self, out: &mut dyn Write) -> io::Result<()> {
/// 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() {
......@@ -72,7 +75,11 @@ fn for_each_constraint(
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(())
......
......@@ -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)?;
};
......@@ -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)?;
......@@ -360,7 +360,7 @@ fn write_extra<'tcx, F>(tcx: TyCtxt<'tcx>, write: &mut dyn Write, mut visit_op:
}
struct ExtraComments<'tcx> {
_tcx: TyCtxt<'tcx>, // don't need it now, but bet we will soon
tcx: TyCtxt<'tcx>,
comments: Vec<String>,
}
......@@ -377,7 +377,7 @@ fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
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));
}
......@@ -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, "|")?;
......
......@@ -726,10 +726,18 @@ fn drop(&mut self) {
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())
......
TokenStream [Ident { ident: "ident", span: $DIR/dump-debug-span-debug.rs:9:5: 9:10 }, Ident { ident: "r#ident", span: $DIR/dump-debug-span-debug.rs:10:5: 10:12 }, Punct { ch: ',', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:11:5: 11:6 }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 }, Punct { ch: '>', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:12:7: 12:8 }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/dump-debug-span-debug.rs:13:5: 13:7 }, Group { delimiter: Bracket, stream: TokenStream [Ident { ident: "_", span: $DIR/dump-debug-span-debug.rs:14:6: 14:7 }], span: $DIR/dump-debug-span-debug.rs:14:5: 14:8 }, Literal { kind: Integer, symbol: "0", suffix: None, span: $DIR/dump-debug-span-debug.rs:17:5: 17:6 }, Literal { kind: Float, symbol: "1.0", suffix: None, span: $DIR/dump-debug-span-debug.rs:18:5: 18:8 }, Literal { kind: Str, symbol: "S", suffix: None, span: $DIR/dump-debug-span-debug.rs:19:5: 19:8 }, Literal { kind: ByteStr, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:20:5: 20:9 }, Literal { kind: StrRaw(0), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:21:5: 21:9 }, Literal { kind: StrRaw(2), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:22:5: 22:13 }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:23:5: 23:11 }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:24:5: 24:15 }, Literal { kind: Char, symbol: "C", suffix: None, span: $DIR/dump-debug-span-debug.rs:25:5: 25:8 }, Literal { kind: Byte, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:26:5: 26:9 }, Literal { kind: Integer, symbol: "0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:29:5: 29:7 }, Literal { kind: Float, symbol: "1.0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:30:5: 30:9 }, Literal { kind: Str, symbol: "S", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:31:5: 31:9 }, Literal { kind: ByteStr, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:32:5: 32:10 }, Literal { kind: StrRaw(0), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:33:5: 33:10 }, Literal { kind: StrRaw(2), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:34:5: 34:14 }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:35:5: 35:12 }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:36:5: 36:16 }, Literal { kind: Char, symbol: "C", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:37:5: 37:9 }, Literal { kind: Byte, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:38:5: 38:10 }]
TokenStream [Ident { ident: "ident", span: $DIR/dump-debug-span-debug.rs:9:5: 9:10 (#0) }, Ident { ident: "r#ident", span: $DIR/dump-debug-span-debug.rs:10:5: 10:12 (#0) }, Punct { ch: ',', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:11:5: 11:6 (#0) }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 (#0) }, Punct { ch: '=', spacing: Joint, span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 (#0) }, Punct { ch: '>', spacing: Alone, span: $DIR/dump-debug-span-debug.rs:12:7: 12:8 (#0) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: $DIR/dump-debug-span-debug.rs:13:5: 13:7 (#0) }, Group { delimiter: Bracket, stream: TokenStream [Ident { ident: "_", span: $DIR/dump-debug-span-debug.rs:14:6: 14:7 (#0) }], span: $DIR/dump-debug-span-debug.rs:14:5: 14:8 (#0) }, Literal { kind: Integer, symbol: "0", suffix: None, span: $DIR/dump-debug-span-debug.rs:17:5: 17:6 (#0) }, Literal { kind: Float, symbol: "1.0", suffix: None, span: $DIR/dump-debug-span-debug.rs:18:5: 18:8 (#0) }, Literal { kind: Str, symbol: "S", suffix: None, span: $DIR/dump-debug-span-debug.rs:19:5: 19:8 (#0) }, Literal { kind: ByteStr, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:20:5: 20:9 (#0) }, Literal { kind: StrRaw(0), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:21:5: 21:9 (#0) }, Literal { kind: StrRaw(2), symbol: "R", suffix: None, span: $DIR/dump-debug-span-debug.rs:22:5: 22:13 (#0) }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:23:5: 23:11 (#0) }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: None, span: $DIR/dump-debug-span-debug.rs:24:5: 24:15 (#0) }, Literal { kind: Char, symbol: "C", suffix: None, span: $DIR/dump-debug-span-debug.rs:25:5: 25:8 (#0) }, Literal { kind: Byte, symbol: "B", suffix: None, span: $DIR/dump-debug-span-debug.rs:26:5: 26:9 (#0) }, Literal { kind: Integer, symbol: "0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:29:5: 29:7 (#0) }, Literal { kind: Float, symbol: "1.0", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:30:5: 30:9 (#0) }, Literal { kind: Str, symbol: "S", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:31:5: 31:9 (#0) }, Literal { kind: ByteStr, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:32:5: 32:10 (#0) }, Literal { kind: StrRaw(0), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:33:5: 33:10 (#0) }, Literal { kind: StrRaw(2), symbol: "R", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:34:5: 34:14 (#0) }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:35:5: 35:12 (#0) }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:36:5: 36:16 (#0) }, Literal { kind: Char, symbol: "C", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:37:5: 37:9 (#0) }, Literal { kind: Byte, symbol: "B", suffix: Some("q"), span: $DIR/dump-debug-span-debug.rs:38:5: 38:10 (#0) }]
TokenStream [
Ident {
ident: "ident",
span: $DIR/dump-debug-span-debug.rs:9:5: 9:10,
span: $DIR/dump-debug-span-debug.rs:9:5: 9:10 (#0),
},
Ident {
ident: "r#ident",
span: $DIR/dump-debug-span-debug.rs:10:5: 10:12,
span: $DIR/dump-debug-span-debug.rs:10:5: 10:12 (#0),
},
Punct {
ch: ',',
spacing: Alone,
span: $DIR/dump-debug-span-debug.rs:11:5: 11:6,
span: $DIR/dump-debug-span-debug.rs:11:5: 11:6 (#0),
},
Punct {
ch: '=',
spacing: Joint,
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7,
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 (#0),
},
Punct {
ch: '=',
spacing: Joint,
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7,
span: $DIR/dump-debug-span-debug.rs:12:5: 12:7 (#0),
},
Punct {
ch: '>',
spacing: Alone,
span: $DIR/dump-debug-span-debug.rs:12:7: 12:8,
span: $DIR/dump-debug-span-debug.rs:12:7: 12:8 (#0),
},
Group {
delimiter: Parenthesis,
stream: TokenStream [],
span: $DIR/dump-debug-span-debug.rs:13:5: 13:7,
span: $DIR/dump-debug-span-debug.rs:13:5: 13:7 (#0),
},
Group {
delimiter: Bracket,
stream: TokenStream [
Ident {
ident: "_",
span: $DIR/dump-debug-span-debug.rs:14:6: 14:7,
span: $DIR/dump-debug-span-debug.rs:14:6: 14:7 (#0),
},
],
span: $DIR/dump-debug-span-debug.rs:14:5: 14:8,
span: $DIR/dump-debug-span-debug.rs:14:5: 14:8 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:17:5: 17:6,
span: $DIR/dump-debug-span-debug.rs:17:5: 17:6 (#0),
},
Literal {
kind: Float,
symbol: "1.0",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:18:5: 18:8,
span: $DIR/dump-debug-span-debug.rs:18:5: 18:8 (#0),
},
Literal {
kind: Str,
symbol: "S",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:19:5: 19:8,
span: $DIR/dump-debug-span-debug.rs:19:5: 19:8 (#0),
},
Literal {
kind: ByteStr,
symbol: "B",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:20:5: 20:9,
span: $DIR/dump-debug-span-debug.rs:20:5: 20:9 (#0),
},
Literal {
kind: StrRaw(0),
symbol: "R",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:21:5: 21:9,
span: $DIR/dump-debug-span-debug.rs:21:5: 21:9 (#0),
},
Literal {
kind: StrRaw(2),
symbol: "R",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:22:5: 22:13,
span: $DIR/dump-debug-span-debug.rs:22:5: 22:13 (#0),
},
Literal {
kind: ByteStrRaw(0),
symbol: "BR",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:23:5: 23:11,
span: $DIR/dump-debug-span-debug.rs:23:5: 23:11 (#0),
},
Literal {
kind: ByteStrRaw(2),
symbol: "BR",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:24:5: 24:15,
span: $DIR/dump-debug-span-debug.rs:24:5: 24:15 (#0),
},
Literal {
kind: Char,
symbol: "C",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:25:5: 25:8,
span: $DIR/dump-debug-span-debug.rs:25:5: 25:8 (#0),
},
Literal {
kind: Byte,
symbol: "B",
suffix: None,
span: $DIR/dump-debug-span-debug.rs:26:5: 26:9,
span: $DIR/dump-debug-span-debug.rs:26:5: 26:9 (#0),
},
Literal {
kind: Integer,
symbol: "0",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:29:5: 29:7,
span: $DIR/dump-debug-span-debug.rs:29:5: 29:7 (#0),
},
Literal {
kind: Float,
symbol: "1.0",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:30:5: 30:9,
span: $DIR/dump-debug-span-debug.rs:30:5: 30:9 (#0),
},
Literal {
kind: Str,
symbol: "S",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:31:5: 31:9,
span: $DIR/dump-debug-span-debug.rs:31:5: 31:9 (#0),
},
Literal {
kind: ByteStr,
symbol: "B",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:32:5: 32:10,
span: $DIR/dump-debug-span-debug.rs:32:5: 32:10 (#0),
},
Literal {
kind: StrRaw(0),
symbol: "R",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:33:5: 33:10,
span: $DIR/dump-debug-span-debug.rs:33:5: 33:10 (#0),
},
Literal {
kind: StrRaw(2),
symbol: "R",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:34:5: 34:14,
span: $DIR/dump-debug-span-debug.rs:34:5: 34:14 (#0),
},
Literal {
kind: ByteStrRaw(0),
symbol: "BR",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:35:5: 35:12,
span: $DIR/dump-debug-span-debug.rs:35:5: 35:12 (#0),
},
Literal {
kind: ByteStrRaw(2),
symbol: "BR",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:36:5: 36:16,
span: $DIR/dump-debug-span-debug.rs:36:5: 36:16 (#0),
},
Literal {
kind: Char,
symbol: "C",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:37:5: 37:9,
span: $DIR/dump-debug-span-debug.rs:37:5: 37:9 (#0),
},
Literal {
kind: Byte,
symbol: "B",
suffix: Some("q"),
span: $DIR/dump-debug-span-debug.rs:38:5: 38:10,
span: $DIR/dump-debug-span-debug.rs:38:5: 38:10 (#0),
},
]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册