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

@ -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 {

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() {
return "no-location".to_string();
}
@ -415,7 +415,7 @@ 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,
@ -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 {
self.lookup_char_pos(sp.lo()).file.name.clone()
}

View file

@ -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");
}