Skip to content

Commit

Permalink
Split span_to_string into span_to_diagnostic/embeddable_string
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeuw committed May 10, 2021
1 parent 0ac9ca4 commit 37dbe86
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
span,
"inconsistent DepNode at `{:?}` for `{}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_string(span),
self.source_map.span_to_diagnostic_string(span),
node_str,
self.definitions
.def_path(self.current_dep_node_owner)
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
)
} else {
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);

Expand Down
22 changes: 19 additions & 3 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,12 @@ pub trait PrettyPrinter<'tcx>:
if let Some(did) = did.as_local() {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(did);
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 {
p!(write("@"), print_def_path(did, substs));
}
Expand Down Expand Up @@ -702,7 +707,12 @@ pub trait PrettyPrinter<'tcx>:
p!("@", print_def_path(did.to_def_id(), substs));
} else {
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 {
p!(write("@"), print_def_path(did, substs));
Expand Down Expand Up @@ -1407,7 +1417,13 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
if !self.empty_path {
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;

return Ok(self);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir/src/borrow_check/region_infer/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
for constraint in &constraints {
let OutlivesConstraint { sup, sub, locations, category } = constraint;
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)),
};
with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?;
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir/src/transform/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
debug!(
"instrumenting {:?}, fn sig span: {}, body span: {}",
def_id,
source_map.span_to_string(fn_sig_span),
source_map.span_to_string(body_span)
source_map.span_to_diagnostic_string(fn_sig_span),
source_map.span_to_diagnostic_string(body_span)
);

let mut graphviz_data = debug::GraphvizData::new();
Expand Down Expand Up @@ -311,8 +311,8 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
"Calling make_code_region(file_name={}, source_file={:?}, span={}, body_span={})",
file_name,
self.source_file,
source_map.span_to_string(span),
source_map.span_to_string(body_span)
source_map.span_to_diagnostic_string(span),
source_map.span_to_diagnostic_string(body_span)
);

inject_statement(
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_mir/src/util/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
ty::Tuple(tys) if tys.is_empty() => {}
_ => {
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 {
self.push(&format!("+ user_ty: {:?}", user_ty));
}
Expand Down Expand Up @@ -516,7 +519,7 @@ impl Visitor<'tcx> for ExtraComments<'tcx> {
}

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.
Expand Down Expand Up @@ -617,7 +620,7 @@ fn write_scope_tree(
"{0:1$} // at {2}",
indented_header,
ALIGN,
tcx.sess.source_map().span_to_string(span),
tcx.sess.source_map().span_to_embeddable_string(span),
)?;
} else {
writeln!(w, "{}", indented_header)?;
Expand Down Expand Up @@ -1004,7 +1007,7 @@ fn write_user_type_annotations(
"| {:?}: {:?} at {}",
index.index(),
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() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/util/spanview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ fn tooltip<'tcx>(
) -> String {
let source_map = tcx.sess.source_map();
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 {
let source_range = source_range_no_file(tcx, &statement.source_info.span);
text.push(format!(
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ enum LiveNodeKind {
fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_>) -> String {
let sm = tcx.sess.source_map();
match lnk {
UpvarNode(s) => format!("Upvar node [{}]", sm.span_to_string(s)),
ExprNode(s) => format!("Expr node [{}]", sm.span_to_string(s)),
VarDefNode(s) => format!("Var def 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_diagnostic_string(s)),
VarDefNode(s) => format!("Var def node [{}]", sm.span_to_diagnostic_string(s)),
ClosureNode => "Closure node".to_owned(),
ExitNode => "Exit node".to_owned(),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {
debug!(
"visit_body(id={:?}, span={:?}, body.id={:?}, cx.parent={:?})",
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,
self.cx.parent
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3347,7 +3347,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: Region) {
debug!(
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);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ pub fn debug_with_source_map(
f: &mut fmt::Formatter<'_>,
source_map: &SourceMap,
) -> 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 {
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
return "no-location".to_string();
}
Expand All @@ -415,14 +415,26 @@ impl SourceMap {
let hi = self.lookup_char_pos(sp.hi());
format!(
"{}:{}:{}: {}:{}",
lo.file.name.prefer_remapped(),
if prefer_local { lo.file.name.prefer_local() } else { lo.file.name.prefer_remapped() },
lo.line,
lo.col.to_usize() + 1,
hi.line,
hi.col.to_usize() + 1,
)
}

/// 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 {
self.lookup_char_pos(sp.lo()).file.name.clone()
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_span/src/source_map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn t8() {
fn t9() {
let sm = init_source_map();
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");
}
Expand Down

0 comments on commit 37dbe86

Please sign in to comment.