Skip to content

Commit 5417b45

Browse files
committed
Use local and remapped paths where appropriate
1 parent fb4f643 commit 5417b45

File tree

28 files changed

+85
-74
lines changed

28 files changed

+85
-74
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ pub fn expand_file(
6161

6262
let topmost = cx.expansion_cause().unwrap_or(sp);
6363
let loc = cx.source_map().lookup_char_pos(topmost.lo());
64-
base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string())))
64+
base::MacEager::expr(
65+
cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())),
66+
)
6567
}
6668

6769
pub fn expand_stringify(

compiler/rustc_codegen_cranelift/src/common.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
334334
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
335335
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
336336
let const_loc = self.tcx.const_caller_location((
337-
rustc_span::symbol::Symbol::intern(&caller.file.name.to_string()),
337+
rustc_span::symbol::Symbol::intern(
338+
&caller.file.name.prefer_remapped().to_string_lossy(),
339+
),
338340
caller.line as u32,
339341
caller.col_display as u32 + 1,
340342
));

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn line_program_add_file(
8787
filename => {
8888
let dir_id = line_program.default_directory();
8989
let dummy_file_name = LineString::new(
90-
filename.to_string().into_bytes(),
90+
filename.prefer_remapped().to_string().into_bytes(),
9191
line_program.encoding(),
9292
line_strings,
9393
);

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,10 @@ fn hex_encode(data: &[u8]) -> String {
759759
}
760760

761761
pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
762-
debug!("file_metadata: file_name: {}", source_file.name);
762+
debug!("file_metadata: file_name: {:?}", source_file.name);
763763

764764
let hash = Some(&source_file.src_hash);
765-
let file_name = Some(source_file.name.to_string());
765+
let file_name = Some(source_file.name.prefer_remapped().to_string());
766766
let directory = if source_file.is_real_file() && !source_file.is_imported() {
767767
Some(cx.sess().working_dir.to_string_lossy(false).to_string())
768768
} else {

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11441144
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
11451145
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
11461146
let const_loc = tcx.const_caller_location((
1147-
Symbol::intern(&caller.file.name.to_string()),
1147+
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
11481148
caller.line as u32,
11491149
caller.col_display as u32 + 1,
11501150
));

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ impl AnnotateSnippetEmitterWriter {
126126
}
127127
// owned: line source, line index, annotations
128128
type Owned = (String, usize, Vec<crate::snippet::Annotation>);
129-
let origin = primary_lo.file.name.to_string();
129+
let filename = primary_lo.file.name.prefer_local();
130+
let origin = filename.to_string_lossy();
130131
let annotated_files: Vec<Owned> = annotated_files
131132
.into_iter()
132133
.flat_map(|annotated_file| {

compiler/rustc_errors/src/emitter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ impl EmitterWriter {
13091309
buffer_msg_line_offset,
13101310
&format!(
13111311
"{}:{}:{}",
1312-
loc.file.name,
1312+
loc.file.name.prefer_local(),
13131313
sm.doctest_offset_line(&loc.file.name, loc.line),
13141314
loc.col.0 + 1,
13151315
),
@@ -1323,7 +1323,7 @@ impl EmitterWriter {
13231323
0,
13241324
&format!(
13251325
"{}:{}:{}: ",
1326-
loc.file.name,
1326+
loc.file.name.prefer_local(),
13271327
sm.doctest_offset_line(&loc.file.name, loc.line),
13281328
loc.col.0 + 1,
13291329
),
@@ -1347,12 +1347,12 @@ impl EmitterWriter {
13471347
};
13481348
format!(
13491349
"{}:{}{}",
1350-
annotated_file.file.name,
1350+
annotated_file.file.name.prefer_local(),
13511351
sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index),
13521352
col
13531353
)
13541354
} else {
1355-
annotated_file.file.name.to_string()
1355+
format!("{}", annotated_file.file.name.prefer_local())
13561356
};
13571357
buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn);
13581358
for _ in 0..max_line_num_len {

compiler/rustc_errors/src/json.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl DiagnosticSpan {
468468
});
469469

470470
DiagnosticSpan {
471-
file_name: start.file.name.to_string(),
471+
file_name: start.file.name.prefer_local().to_string(),
472472
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
473473
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
474474
line_start: start.line,

compiler/rustc_expand/src/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,10 @@ impl<'a> ExtCtxt<'a> {
10801080
other => {
10811081
return Err(self.struct_span_err(
10821082
span,
1083-
&format!("cannot resolve relative path in non-file source `{}`", other),
1083+
&format!(
1084+
"cannot resolve relative path in non-file source `{}`",
1085+
other.prefer_local()
1086+
),
10841087
));
10851088
}
10861089
};

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
365365
FileName::Real(name) => name
366366
.into_local_path()
367367
.expect("attempting to resolve a file path in an external file"),
368-
other => PathBuf::from(other.to_string()),
368+
other => PathBuf::from(other.prefer_local().to_string()),
369369
};
370370
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
371371
self.cx.root_path = dir_path.clone();

compiler/rustc_expand/src/proc_macro_server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl server::SourceFile for Rustc<'_> {
627627
.to_str()
628628
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
629629
.to_string(),
630-
_ => file.name.to_string(),
630+
_ => file.name.prefer_local().to_string(),
631631
}
632632
}
633633
fn is_real(&mut self, file: &Self::SourceFile) -> bool {

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1604,13 +1604,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16041604
match (&terr, expected == found) {
16051605
(TypeError::Sorts(values), extra) => {
16061606
let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1607-
(true, ty::Opaque(def_id, _)) => format!(
1608-
" (opaque type at {})",
1609-
self.tcx
1607+
(true, ty::Opaque(def_id, _)) => {
1608+
let pos = self
1609+
.tcx
16101610
.sess
16111611
.source_map()
1612-
.mk_substr_filename(self.tcx.def_span(*def_id)),
1613-
),
1612+
.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1613+
format!(
1614+
" (opaque type at <{}:{}:{}>)",
1615+
pos.file.name.prefer_local(),
1616+
pos.line,
1617+
pos.col.to_usize() + 1,
1618+
)
1619+
}
16141620
(true, _) => format!(" ({})", ty.sort_string(self.tcx)),
16151621
(false, _) => "".to_string(),
16161622
};

compiler/rustc_interface/src/passes.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use rustc_session::output::{filename_for_input, filename_for_metadata};
3636
use rustc_session::search_paths::PathKind;
3737
use rustc_session::Session;
3838
use rustc_span::symbol::{Ident, Symbol};
39-
use rustc_span::FileName;
4039
use rustc_trait_selection::traits;
4140
use rustc_typeck as typeck;
4241
use tracing::{info, warn};
@@ -575,14 +574,7 @@ fn write_out_deps(
575574
.iter()
576575
.filter(|fmap| fmap.is_real_file())
577576
.filter(|fmap| !fmap.is_imported())
578-
.map(|fmap| {
579-
escape_dep_filename(&match &fmap.name {
580-
FileName::Real(real) => {
581-
real.local_path().unwrap_or(real.stable_name()).display().to_string()
582-
}
583-
_ => fmap.name.to_string(),
584-
})
585-
})
577+
.map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string()))
586578
.collect();
587579

588580
if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend {

compiler/rustc_mir/src/interpret/eval_context.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,13 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
263263
}
264264
if !self.span.is_dummy() {
265265
let lo = tcx.sess.source_map().lookup_char_pos(self.span.lo());
266-
write!(f, " at {}:{}:{}", lo.file.name, lo.line, lo.col.to_usize() + 1)?;
266+
write!(
267+
f,
268+
" at {}:{}:{}",
269+
lo.file.name.prefer_local(),
270+
lo.line,
271+
lo.col.to_usize() + 1
272+
)?;
267273
}
268274
Ok(())
269275
})

compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
106106
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
107107
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
108108
(
109-
Symbol::intern(&caller.file.name.to_string()),
109+
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
110110
u32::try_from(caller.line).unwrap(),
111111
u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(),
112112
)

compiler/rustc_mir/src/transform/coverage/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
290290
let tcx = self.tcx;
291291
let source_map = tcx.sess.source_map();
292292
let body_span = self.body_span;
293-
let file_name = Symbol::intern(&self.source_file.name.to_string());
293+
let file_name = Symbol::intern(&self.source_file.name.prefer_remapped().to_string_lossy());
294294

295295
let mut bcb_counters = IndexVec::from_elem_n(None, self.basic_coverage_blocks.num_nodes());
296296
for covspan in coverage_spans {

compiler/rustc_parse/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ pub fn maybe_file_to_stream(
188188
override_span: Option<Span>,
189189
) -> Result<(TokenStream, Vec<lexer::UnmatchedBrace>), Vec<Diagnostic>> {
190190
let src = source_file.src.as_ref().unwrap_or_else(|| {
191-
sess.span_diagnostic
192-
.bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
191+
sess.span_diagnostic.bug(&format!(
192+
"cannot lex `source_file` without source: {}",
193+
source_file.name.prefer_local()
194+
));
193195
});
194196

195197
let (token_trees, unmatched_braces) =

compiler/rustc_save_analysis/src/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ impl<'tcx> DumpVisitor<'tcx> {
11121112
name: String::new(),
11131113
qualname,
11141114
span,
1115-
value: filename.to_string(),
1115+
value: filename.prefer_remapped().to_string(),
11161116
children,
11171117
parent: None,
11181118
decl_id: None,

compiler/rustc_save_analysis/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'tcx> SaveContext<'tcx> {
8080
let end = sm.lookup_char_pos(span.hi());
8181

8282
SpanData {
83-
file_name: start.file.name.to_string().into(),
83+
file_name: start.file.name.prefer_remapped().to_string().into(),
8484
byte_start: span.lo().0,
8585
byte_end: span.hi().0,
8686
line_start: Row::new_one_indexed(start.line as u32),
@@ -290,7 +290,7 @@ impl<'tcx> SaveContext<'tcx> {
290290
name: item.ident.to_string(),
291291
qualname,
292292
span: self.span_from_span(item.ident.span),
293-
value: filename.to_string(),
293+
value: filename.prefer_remapped().to_string(),
294294
parent: None,
295295
children: m
296296
.item_ids

compiler/rustc_save_analysis/src/span_utils.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ impl<'a> SpanUtils<'a> {
3434
.to_string()
3535
}
3636
}
37-
// If the file name was remapped, we assume the user
38-
// configured it the way they wanted to, so use that directly
39-
FileName::Real(RealFileName::Remapped { local_path: _, virtual_name }) => {
40-
virtual_name.display().to_string()
41-
}
42-
filename => filename.to_string(),
37+
filename => filename.prefer_remapped().to_string(),
4338
}
4439
}
4540

compiler/rustc_span/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ impl FileName {
305305
}
306306
}
307307

308+
pub fn prefer_remapped(&self) -> FileNameDisplay<'_> {
309+
FileNameDisplay { inner: self, prefer_local: false }
310+
}
311+
312+
// This may include transient local filesystem information.
313+
// Must not be embedded in build outputs.
314+
pub fn prefer_local(&self) -> FileNameDisplay<'_> {
315+
FileNameDisplay { inner: self, prefer_local: true }
316+
}
317+
308318
pub fn macro_expansion_source_code(src: &str) -> FileName {
309319
let mut hasher = StableHasher::new();
310320
src.hash(&mut hasher);

compiler/rustc_span/src/source_map.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,6 @@ impl SourceMap {
369369
source_file
370370
}
371371

372-
pub fn mk_substr_filename(&self, sp: Span) -> String {
373-
let pos = self.lookup_char_pos(sp.lo());
374-
format!("<{}:{}:{}>", pos.file.name, pos.line, pos.col.to_usize() + 1)
375-
}
376-
377372
// If there is a doctest offset, applies it to the line.
378373
pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize {
379374
match file {
@@ -420,7 +415,7 @@ impl SourceMap {
420415
let hi = self.lookup_char_pos(sp.hi());
421416
format!(
422417
"{}:{}:{}: {}:{}",
423-
lo.file.name,
418+
lo.file.name.prefer_remapped(),
424419
lo.line,
425420
lo.col.to_usize() + 1,
426421
hi.line,

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl ExternalCrate {
9393

9494
crate fn src_root(&self, tcx: TyCtxt<'_>) -> PathBuf {
9595
match self.src(tcx) {
96-
FileName::Real(ref p) => match p.local_path().parent() {
96+
FileName::Real(ref p) => match p.local_path_if_available().parent() {
9797
Some(p) => p.to_path_buf(),
9898
None => PathBuf::new(),
9999
},

src/librustdoc/doctest.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl Collector {
840840
if !item_path.is_empty() {
841841
item_path.push(' ');
842842
}
843-
format!("{} - {}(line {})", filename, item_path, line)
843+
format!("{} - {}(line {})", filename.prefer_local(), item_path, line)
844844
}
845845

846846
crate fn set_position(&mut self, position: Span) {
@@ -891,15 +891,16 @@ impl Tester for Collector {
891891
local_path.to_path_buf()
892892
} else {
893893
// Somehow we got the filename from the metadata of another crate, should never happen
894-
PathBuf::from(r"doctest.rs")
894+
unreachable!("doctest from a different crate");
895895
}
896896
}
897897
_ => PathBuf::from(r"doctest.rs"),
898898
};
899899

900900
// For example `module/file.rs` would become `module_file_rs`
901901
let file = filename
902-
.to_string()
902+
.prefer_local()
903+
.to_string_lossy()
903904
.chars()
904905
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
905906
.collect::<String>();

src/librustdoc/html/render/context.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
376376
} = options;
377377

378378
let src_root = match krate.src {
379-
FileName::Real(ref p) => {
380-
if let Some(local_path) = p.local_path() {
381-
match local_path.parent() {
382-
Some(p) => p.to_path_buf(),
383-
None => PathBuf::new(),
384-
}
385-
} else {
386-
// Somehow we got the filename from the metadata of another crate, should never happen
387-
PathBuf::new()
388-
}
389-
}
379+
FileName::Real(ref p) => match p.local_path_if_available().parent() {
380+
Some(p) => p.to_path_buf(),
381+
None => PathBuf::new(),
382+
},
390383
_ => PathBuf::new(),
391384
};
392385
// If user passed in `--playground-url` arg, we fill in crate name here

src/librustdoc/html/sources.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ impl DocFolder for SourceCollector<'_, '_> {
5656
Err(e) => {
5757
self.scx.tcx.sess.span_err(
5858
item.span(self.scx.tcx).inner(),
59-
&format!("failed to render source code for `{}`: {}", filename, e),
59+
&format!(
60+
"failed to render source code for `{}`: {}",
61+
filename.prefer_local(),
62+
e
63+
),
6064
);
6165
false
6266
}
@@ -80,7 +84,7 @@ impl SourceCollector<'_, 'tcx> {
8084
if let Some(local_path) = file.local_path() {
8185
local_path.to_path_buf()
8286
} else {
83-
return Ok(());
87+
unreachable!("only the current crate should have sources emitted");
8488
}
8589
}
8690
_ => return Ok(()),
@@ -119,7 +123,7 @@ impl SourceCollector<'_, 'tcx> {
119123
href.push_str(&fname.to_string_lossy());
120124

121125
let title = format!("{} - source", src_fname.to_string_lossy());
122-
let desc = format!("Source of the Rust file `{}`.", filename);
126+
let desc = format!("Source of the Rust file `{}`.", filename.prefer_remapped());
123127
let page = layout::Page {
124128
title: &title,
125129
css_class: "source",

0 commit comments

Comments
 (0)