1
Fork 0

Split span_to_string into span_to_diagnostic/embeddable_string

This commit is contained in:
Andy Wang 2021-05-03 01:14:25 +01:00
parent 0ac9ca4f88
commit 37dbe868c9
No known key found for this signature in database
GPG key ID: 181B49F9F38F3374
13 changed files with 60 additions and 24 deletions

View file

@ -262,7 +262,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
span, span,
"inconsistent DepNode at `{:?}` for `{}`: \ "inconsistent DepNode at `{:?}` for `{}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_string(span), self.source_map.span_to_diagnostic_string(span),
node_str, node_str,
self.definitions self.definitions
.def_path(self.current_dep_node_owner) .def_path(self.current_dep_node_owner)

View file

@ -2365,7 +2365,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
) )
} else { } else {
let span = tcx.hir().span(hir_id); let span = tcx.hir().span(hir_id);
format!("[closure@{}]", tcx.sess.source_map().span_to_string(span)) format!(
"[closure@{}]",
tcx.sess.source_map().span_to_diagnostic_string(span)
)
}; };
let mut struct_fmt = fmt.debug_struct(&name); let mut struct_fmt = fmt.debug_struct(&name);

View file

@ -667,7 +667,12 @@ pub trait PrettyPrinter<'tcx>:
if let Some(did) = did.as_local() { if let Some(did) = did.as_local() {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
let span = 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))); p!(write(
"@{}",
// This may end up in stderr diagnostics but it may also be emitted
// into MIR. Hence we use the remapped path if available
self.tcx().sess.source_map().span_to_embeddable_string(span)
));
} else { } else {
p!(write("@"), print_def_path(did, substs)); p!(write("@"), print_def_path(did, substs));
} }
@ -702,7 +707,12 @@ pub trait PrettyPrinter<'tcx>:
p!("@", print_def_path(did.to_def_id(), substs)); p!("@", print_def_path(did.to_def_id(), substs));
} else { } else {
let span = 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))); p!(write(
"@{}",
// This may end up in stderr diagnostics but it may also be emitted
// into MIR. Hence we use the remapped path if available
self.tcx().sess.source_map().span_to_embeddable_string(span)
));
} }
} else { } else {
p!(write("@"), print_def_path(did, substs)); p!(write("@"), print_def_path(did, substs));
@ -1407,7 +1417,13 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
if !self.empty_path { if !self.empty_path {
write!(self, "::")?; write!(self, "::")?;
} }
write!(self, "<impl at {}>", self.tcx.sess.source_map().span_to_string(span))?; write!(
self,
"<impl at {}>",
// This may end up in stderr diagnostics but it may also be emitted
// into MIR. Hence we use the remapped path if available
self.tcx.sess.source_map().span_to_embeddable_string(span)
)?;
self.empty_path = false; self.empty_path = false;
return Ok(self); return Ok(self);

View file

@ -76,7 +76,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
for constraint in &constraints { for constraint in &constraints {
let OutlivesConstraint { sup, sub, locations, category } = constraint; let OutlivesConstraint { sup, sub, locations, category } = constraint;
let (name, arg) = match locations { let (name, arg) = match locations {
Locations::All(span) => ("All", tcx.sess.source_map().span_to_string(*span)), Locations::All(span) => {
("All", tcx.sess.source_map().span_to_embeddable_string(*span))
}
Locations::Single(loc) => ("Single", format!("{:?}", loc)), Locations::Single(loc) => ("Single", format!("{:?}", loc)),
}; };
with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?; with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?;

View file

@ -148,8 +148,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
debug!( debug!(
"instrumenting {:?}, fn sig span: {}, body span: {}", "instrumenting {:?}, fn sig span: {}, body span: {}",
def_id, def_id,
source_map.span_to_string(fn_sig_span), source_map.span_to_diagnostic_string(fn_sig_span),
source_map.span_to_string(body_span) source_map.span_to_diagnostic_string(body_span)
); );
let mut graphviz_data = debug::GraphvizData::new(); let mut graphviz_data = debug::GraphvizData::new();
@ -311,8 +311,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
"Calling make_code_region(file_name={}, source_file={:?}, span={}, body_span={})", "Calling make_code_region(file_name={}, source_file={:?}, span={}, body_span={})",
file_name, file_name,
self.source_file, self.source_file,
source_map.span_to_string(span), source_map.span_to_diagnostic_string(span),
source_map.span_to_string(body_span) source_map.span_to_diagnostic_string(body_span)
); );
inject_statement( inject_statement(

View file

@ -445,7 +445,10 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
ty::Tuple(tys) if tys.is_empty() => {} ty::Tuple(tys) if tys.is_empty() => {}
_ => { _ => {
self.push("mir::Constant"); self.push("mir::Constant");
self.push(&format!("+ span: {}", self.tcx.sess.source_map().span_to_string(*span))); self.push(&format!(
"+ span: {}",
self.tcx.sess.source_map().span_to_embeddable_string(*span)
));
if let Some(user_ty) = user_ty { if let Some(user_ty) = user_ty {
self.push(&format!("+ user_ty: {:?}", user_ty)); self.push(&format!("+ user_ty: {:?}", user_ty));
} }
@ -516,7 +519,7 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
} }
fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String { fn comment(tcx: TyCtxt<'_>, SourceInfo { span, scope }: SourceInfo) -> String {
format!("scope {} at {}", scope.index(), tcx.sess.source_map().span_to_string(span)) format!("scope {} at {}", scope.index(), tcx.sess.source_map().span_to_embeddable_string(span))
} }
/// Prints local variables in a scope tree. /// Prints local variables in a scope tree.
@ -617,7 +620,7 @@ fn write_scope_tree(
"{0:1$} // at {2}", "{0:1$} // at {2}",
indented_header, indented_header,
ALIGN, ALIGN,
tcx.sess.source_map().span_to_string(span), tcx.sess.source_map().span_to_embeddable_string(span),
)?; )?;
} else { } else {
writeln!(w, "{}", indented_header)?; writeln!(w, "{}", indented_header)?;
@ -1004,7 +1007,7 @@ fn write_user_type_annotations(
"| {:?}: {:?} at {}", "| {:?}: {:?} at {}",
index.index(), index.index(),
annotation.user_ty, annotation.user_ty,
tcx.sess.source_map().span_to_string(annotation.span) tcx.sess.source_map().span_to_embeddable_string(annotation.span)
)?; )?;
} }
if !body.user_type_annotations.is_empty() { if !body.user_type_annotations.is_empty() {

View file

@ -628,7 +628,7 @@ fn tooltip<'tcx>(
) -> String { ) -> String {
let source_map = tcx.sess.source_map(); let source_map = tcx.sess.source_map();
let mut text = Vec::new(); let mut text = Vec::new();
text.push(format!("{}: {}:", spanview_id, &source_map.span_to_string(span))); text.push(format!("{}: {}:", spanview_id, &source_map.span_to_embeddable_string(span)));
for statement in statements { for statement in statements {
let source_range = source_range_no_file(tcx, &statement.source_info.span); let source_range = source_range_no_file(tcx, &statement.source_info.span);
text.push(format!( text.push(format!(

View file

@ -132,9 +132,9 @@ enum LiveNodeKind {
fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String { fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
let sm = tcx.sess.source_map(); let sm = tcx.sess.source_map();
match lnk { match lnk {
UpvarNode(s) => format!("Upvar node [{}]", sm.span_to_string(s)), UpvarNode(s) => format!("Upvar node [{}]", sm.span_to_diagnostic_string(s)),
ExprNode(s) => format!("Expr node [{}]", sm.span_to_string(s)), ExprNode(s) => format!("Expr node [{}]", sm.span_to_diagnostic_string(s)),
VarDefNode(s) => format!("Var def node [{}]", sm.span_to_string(s)), VarDefNode(s) => format!("Var def node [{}]", sm.span_to_diagnostic_string(s)),
ClosureNode => "Closure node".to_owned(), ClosureNode => "Closure node".to_owned(),
ExitNode => "Exit node".to_owned(), ExitNode => "Exit node".to_owned(),
} }

View file

@ -717,7 +717,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
debug!( debug!(
"visit_body(id={:?}, span={:?}, body.id={:?}, cx.parent={:?})", "visit_body(id={:?}, span={:?}, body.id={:?}, cx.parent={:?})",
owner_id, owner_id,
self.tcx.sess.source_map().span_to_string(body.value.span), self.tcx.sess.source_map().span_to_diagnostic_string(body.value.span),
body_id, body_id,
self.cx.parent self.cx.parent
); );

View file

@ -3347,7 +3347,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: Region) { fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: Region) {
debug!( debug!(
node = ?self.tcx.hir().node_to_string(lifetime_ref.hir_id), node = ?self.tcx.hir().node_to_string(lifetime_ref.hir_id),
span = ?self.tcx.sess.source_map().span_to_string(lifetime_ref.span) span = ?self.tcx.sess.source_map().span_to_diagnostic_string(lifetime_ref.span)
); );
self.map.defs.insert(lifetime_ref.hir_id, def); self.map.defs.insert(lifetime_ref.hir_id, def);

View file

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

View file

@ -406,7 +406,7 @@ impl SourceMap {
} }
} }
pub fn span_to_string(&self, sp: Span) -> String { fn span_to_string(&self, sp: Span, prefer_local: bool) -> String {
if self.files.borrow().source_files.is_empty() && sp.is_dummy() { if self.files.borrow().source_files.is_empty() && sp.is_dummy() {
return "no-location".to_string(); return "no-location".to_string();
} }
@ -415,7 +415,7 @@ impl SourceMap {
let hi = self.lookup_char_pos(sp.hi()); let hi = self.lookup_char_pos(sp.hi());
format!( format!(
"{}:{}:{}: {}:{}", "{}:{}:{}: {}:{}",
lo.file.name.prefer_remapped(), if prefer_local { lo.file.name.prefer_local() } else { lo.file.name.prefer_remapped() },
lo.line, lo.line,
lo.col.to_usize() + 1, lo.col.to_usize() + 1,
hi.line, hi.line,
@ -423,6 +423,18 @@ impl SourceMap {
) )
} }
/// Format the span location suitable for embedding in build artifacts
pub fn span_to_embeddable_string(&self, sp: Span) -> String {
self.span_to_string(sp, false)
}
/// Format the span location to be printed in diagnostics. Must not be emitted
/// to build artifacts as this may leak local file paths. Use span_to_embeddable_string
/// for string suitable for embedding.
pub fn span_to_diagnostic_string(&self, sp: Span) -> String {
self.span_to_string(sp, true)
}
pub fn span_to_filename(&self, sp: Span) -> FileName { pub fn span_to_filename(&self, sp: Span) -> FileName {
self.lookup_char_pos(sp.lo()).file.name.clone() self.lookup_char_pos(sp.lo()).file.name.clone()
} }

View file

@ -193,7 +193,7 @@ fn t8() {
fn t9() { fn t9() {
let sm = init_source_map(); let sm = init_source_map();
let span = Span::with_root_ctxt(BytePos(12), BytePos(23)); let span = Span::with_root_ctxt(BytePos(12), BytePos(23));
let sstr = sm.span_to_string(span); let sstr = sm.span_to_diagnostic_string(span);
assert_eq!(sstr, "blork.rs:2:1: 2:12"); assert_eq!(sstr, "blork.rs:2:1: 2:12");
} }