From 04079190835c04e8c671daef492144dfe06564ab Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sat, 10 Apr 2021 13:48:06 +0100 Subject: Use RealFileName for Session::working_dir as it may also be remapped --- compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/debuginfo') diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index e6fa852155b..ccef9530850 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -764,7 +764,7 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll let hash = Some(&source_file.src_hash); let file_name = Some(source_file.name.to_string()); let directory = if source_file.is_real_file() && !source_file.is_imported() { - Some(cx.sess().working_dir.0.to_string_lossy().to_string()) + Some(cx.sess().working_dir.stable_name().to_string_lossy().to_string()) } else { // If the path comes from an upstream crate we assume it has been made // independent of the compiler's working directory one way or another. @@ -992,7 +992,7 @@ pub fn compile_unit_metadata( let producer = format!("clang LLVM ({})", rustc_producer); let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); - let work_dir = tcx.sess.working_dir.0.to_string_lossy(); + let work_dir = tcx.sess.working_dir.stable_name().to_string_lossy(); let flags = "\0"; let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory; let split_name = if tcx.sess.target_can_use_split_dwarf() { -- cgit 1.4.1-3-g733a5 From fb4f6439f62d4b940bdfab3f78771d76eacab379 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Wed, 14 Apr 2021 14:12:39 +0100 Subject: Revamp RealFileName public methods --- .../src/debuginfo/line_info.rs | 2 +- .../rustc_codegen_cranelift/src/debuginfo/mod.rs | 2 +- .../rustc_codegen_llvm/src/debuginfo/metadata.rs | 4 +-- compiler/rustc_metadata/src/rmeta/encoder.rs | 35 ++++++++++-------- compiler/rustc_save_analysis/src/dump_visitor.rs | 2 +- compiler/rustc_save_analysis/src/span_utils.rs | 7 +++- compiler/rustc_span/src/lib.rs | 42 +++++++++++----------- 7 files changed, 53 insertions(+), 41 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/debuginfo') diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs index 8578ab33ced..b3f700bc467 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs @@ -66,7 +66,7 @@ fn line_program_add_file( ) -> FileId { match &file.name { FileName::Real(path) => { - let (dir_path, file_name) = split_path_dir_and_file(path.stable_name()); + let (dir_path, file_name) = split_path_dir_and_file(path.remapped_path_if_available()); let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str()); let file_name = osstr_as_utf8_bytes(file_name); diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index 10d3651789b..61e54a76f29 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -64,7 +64,7 @@ impl<'tcx> DebugContext<'tcx> { // FIXME: how to get version when building out of tree? // Normally this would use option_env!("CFG_VERSION"). let producer = format!("cg_clif (rustc {})", "unknown version"); - let comp_dir = tcx.sess.working_dir.stable_name().to_string_lossy().into_owned(); + let comp_dir = tcx.sess.working_dir.to_string_lossy(false).into_owned(); let (name, file_info) = match tcx.sess.local_crate_source_file.clone() { Some(path) => { let name = path.to_string_lossy().into_owned(); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index ccef9530850..6fad1996d7e 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -764,7 +764,7 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll let hash = Some(&source_file.src_hash); let file_name = Some(source_file.name.to_string()); let directory = if source_file.is_real_file() && !source_file.is_imported() { - Some(cx.sess().working_dir.stable_name().to_string_lossy().to_string()) + Some(cx.sess().working_dir.to_string_lossy(false).to_string()) } else { // If the path comes from an upstream crate we assume it has been made // independent of the compiler's working directory one way or another. @@ -992,7 +992,7 @@ pub fn compile_unit_metadata( let producer = format!("clang LLVM ({})", rustc_producer); let name_in_debuginfo = name_in_debuginfo.to_string_lossy(); - let work_dir = tcx.sess.working_dir.stable_name().to_string_lossy(); + let work_dir = tcx.sess.working_dir.to_string_lossy(false); let flags = "\0"; let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory; let split_name = if tcx.sess.target_can_use_split_dwarf() { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a3e0dd8e3b9..fa62b664b9e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -490,22 +490,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { FileName::Real(ref realname) => { let mut adapted = (**source_file).clone(); adapted.name = FileName::Real(match realname { - RealFileName::LocalPath(local_path) => { - // Prepend path of working directory onto local path. - // because relative paths are potentially relative to a - // wrong directory. + RealFileName::LocalPath(path_to_file) => { + // Prepend path of working directory onto potentially + // relative paths, because they could become relative + // to a wrong directory. let working_dir = &self.tcx.sess.working_dir; - if let RealFileName::LocalPath(absolute) = working_dir { - // If working_dir has not been remapped, then we emit a - // LocalPath variant as it's likely to be a valid path - RealFileName::LocalPath(Path::new(absolute).join(local_path)) - } else { - // If working_dir has been remapped, then we emit - // Remapped variant as the expanded path won't be valid - RealFileName::Remapped { - local_path: None, - virtual_name: Path::new(working_dir.stable_name()) - .join(local_path), + match working_dir { + RealFileName::LocalPath(absolute) => { + // If working_dir has not been remapped, then we emit a + // LocalPath variant as it's likely to be a valid path + RealFileName::LocalPath( + Path::new(absolute).join(path_to_file), + ) + } + RealFileName::Remapped { local_path: _, virtual_name } => { + // If working_dir has been remapped, then we emit + // Remapped variant as the expanded path won't be valid + RealFileName::Remapped { + local_path: None, + virtual_name: Path::new(virtual_name) + .join(path_to_file), + } } } } diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index ab1b4db6293..06b2e41daf5 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -190,7 +190,7 @@ impl<'tcx> DumpVisitor<'tcx> { }; let data = CompilationOptions { - directory: self.tcx.sess.working_dir.stable_name().into(), + directory: self.tcx.sess.working_dir.remapped_path_if_available().into(), program, arguments, output: self.save_ctxt.compilation_output(crate_name), diff --git a/compiler/rustc_save_analysis/src/span_utils.rs b/compiler/rustc_save_analysis/src/span_utils.rs index bcda3a6d006..3c4d7972380 100644 --- a/compiler/rustc_save_analysis/src/span_utils.rs +++ b/compiler/rustc_save_analysis/src/span_utils.rs @@ -26,7 +26,12 @@ impl<'a> SpanUtils<'a> { .display() .to_string() } else { - self.sess.working_dir.stable_name().join(&path).display().to_string() + self.sess + .working_dir + .remapped_path_if_available() + .join(&path) + .display() + .to_string() } } // If the file name was remapped, we assume the user diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 141ecfd0baf..787fe1d0f78 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -172,7 +172,7 @@ impl Encodable for RealFileName { impl RealFileName { /// Returns the path suitable for reading from the file system on the local host, /// if this information exists. - /// Avoid embedding this in build artifacts; see `stable_name()` for that. + /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that. pub fn local_path(&self) -> Option<&Path> { match self { RealFileName::LocalPath(p) => Some(p), @@ -184,7 +184,7 @@ impl RealFileName { /// Returns the path suitable for reading from the file system on the local host, /// if this information exists. - /// Avoid embedding this in build artifacts; see `stable_name()` for that. + /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that. pub fn into_local_path(self) -> Option { match self { RealFileName::LocalPath(p) => Some(p), @@ -192,31 +192,33 @@ impl RealFileName { } } - /// Returns the path suitable for embedding into build artifacts. Note that - /// a remapped path will not correspond to a valid file system path; see - /// `local_path()` for something that is more likely to return paths into the - /// local host file system. - pub fn stable_name(&self) -> &Path { + /// Returns the path suitable for embedding into build artifacts. This would still + /// be a local path if it has not been remapped. A remapped path will not correspond + /// to a valid file system path: see `local_path_if_available()` for something that + /// is more likely to return paths into the local host file system. + pub fn remapped_path_if_available(&self) -> &Path { match self { RealFileName::LocalPath(p) | RealFileName::Remapped { local_path: _, virtual_name: p } => &p, } } - fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> { - use RealFileName::*; + /// Returns the path suitable for reading from the file system on the local host, + /// if this information exists. Otherwise returns the remapped name. + /// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that. + pub fn local_path_if_available(&self) -> &Path { + match self { + RealFileName::LocalPath(path) + | RealFileName::Remapped { local_path: None, virtual_name: path } + | RealFileName::Remapped { local_path: Some(path), virtual_name: _ } => path, + } + } + + pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> { if prefer_local { - match self { - LocalPath(path) - | Remapped { local_path: None, virtual_name: path } - | Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(), - } + self.local_path_if_available().to_string_lossy() } else { - match self { - LocalPath(path) | Remapped { local_path: _, virtual_name: path } => { - path.to_string_lossy() - } - } + self.remapped_path_if_available().to_string_lossy() } } } @@ -1358,7 +1360,7 @@ impl Decodable for SourceFile { impl fmt::Debug for SourceFile { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(fmt, "SourceFile({})", self.name) + write!(fmt, "SourceFile({:?})", self.name) } } -- cgit 1.4.1-3-g733a5 From 5417b45c2676dcd396f007bd89ed0cd55d085768 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 19 Apr 2021 23:27:02 +0100 Subject: Use local and remapped paths where appropriate --- compiler/rustc_builtin_macros/src/source_util.rs | 4 +++- compiler/rustc_codegen_cranelift/src/common.rs | 4 +++- .../rustc_codegen_cranelift/src/debuginfo/line_info.rs | 2 +- compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs | 4 ++-- compiler/rustc_codegen_ssa/src/mir/block.rs | 2 +- .../rustc_errors/src/annotate_snippet_emitter_writer.rs | 3 ++- compiler/rustc_errors/src/emitter.rs | 8 ++++---- compiler/rustc_errors/src/json.rs | 2 +- compiler/rustc_expand/src/base.rs | 5 ++++- compiler/rustc_expand/src/expand.rs | 2 +- compiler/rustc_expand/src/proc_macro_server.rs | 2 +- compiler/rustc_infer/src/infer/error_reporting/mod.rs | 16 +++++++++++----- compiler/rustc_interface/src/passes.rs | 10 +--------- compiler/rustc_mir/src/interpret/eval_context.rs | 8 +++++++- .../src/interpret/intrinsics/caller_location.rs | 2 +- compiler/rustc_mir/src/transform/coverage/mod.rs | 2 +- compiler/rustc_parse/src/lib.rs | 6 ++++-- compiler/rustc_save_analysis/src/dump_visitor.rs | 2 +- compiler/rustc_save_analysis/src/lib.rs | 4 ++-- compiler/rustc_save_analysis/src/span_utils.rs | 7 +------ compiler/rustc_span/src/lib.rs | 10 ++++++++++ compiler/rustc_span/src/source_map.rs | 7 +------ src/librustdoc/clean/types.rs | 2 +- src/librustdoc/doctest.rs | 7 ++++--- src/librustdoc/html/render/context.rs | 15 ++++----------- src/librustdoc/html/sources.rs | 10 +++++++--- src/librustdoc/passes/calculate_doc_coverage.rs | 4 ++-- src/tools/clippy/clippy_lints/src/macro_use.rs | 9 ++++----- 28 files changed, 85 insertions(+), 74 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/debuginfo') diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs index 4aafcb2fb6d..ccb9f15b0f5 100644 --- a/compiler/rustc_builtin_macros/src/source_util.rs +++ b/compiler/rustc_builtin_macros/src/source_util.rs @@ -61,7 +61,9 @@ pub fn expand_file( let topmost = cx.expansion_cause().unwrap_or(sp); let loc = cx.source_map().lookup_char_pos(topmost.lo()); - base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string()))) + base::MacEager::expr( + cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())), + ) } pub fn expand_stringify( diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs index 92e4435565e..c12d6d0f141 100644 --- a/compiler/rustc_codegen_cranelift/src/common.rs +++ b/compiler/rustc_codegen_cranelift/src/common.rs @@ -334,7 +334,9 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); let const_loc = self.tcx.const_caller_location(( - rustc_span::symbol::Symbol::intern(&caller.file.name.to_string()), + rustc_span::symbol::Symbol::intern( + &caller.file.name.prefer_remapped().to_string_lossy(), + ), caller.line as u32, caller.col_display as u32 + 1, )); diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs index b3f700bc467..9eb06770630 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs @@ -87,7 +87,7 @@ fn line_program_add_file( filename => { let dir_id = line_program.default_directory(); let dummy_file_name = LineString::new( - filename.to_string().into_bytes(), + filename.prefer_remapped().to_string().into_bytes(), line_program.encoding(), line_strings, ); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 6fad1996d7e..1eee1eaa211 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -759,10 +759,10 @@ fn hex_encode(data: &[u8]) -> String { } pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile { - debug!("file_metadata: file_name: {}", source_file.name); + debug!("file_metadata: file_name: {:?}", source_file.name); let hash = Some(&source_file.src_hash); - let file_name = Some(source_file.name.to_string()); + let file_name = Some(source_file.name.prefer_remapped().to_string()); let directory = if source_file.is_real_file() && !source_file.is_imported() { Some(cx.sess().working_dir.to_string_lossy(false).to_string()) } else { diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index fd3f89a2aee..72e9163b88e 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -1144,7 +1144,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo()); let const_loc = tcx.const_caller_location(( - Symbol::intern(&caller.file.name.to_string()), + Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()), caller.line as u32, caller.col_display as u32 + 1, )); diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index 6f365c07f6d..577baec21f0 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -126,7 +126,8 @@ impl AnnotateSnippetEmitterWriter { } // owned: line source, line index, annotations type Owned = (String, usize, Vec); - let origin = primary_lo.file.name.to_string(); + let filename = primary_lo.file.name.prefer_local(); + let origin = filename.to_string_lossy(); let annotated_files: Vec = annotated_files .into_iter() .flat_map(|annotated_file| { diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index a58caf2667b..3443bb5366c 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1309,7 +1309,7 @@ impl EmitterWriter { buffer_msg_line_offset, &format!( "{}:{}:{}", - loc.file.name, + loc.file.name.prefer_local(), sm.doctest_offset_line(&loc.file.name, loc.line), loc.col.0 + 1, ), @@ -1323,7 +1323,7 @@ impl EmitterWriter { 0, &format!( "{}:{}:{}: ", - loc.file.name, + loc.file.name.prefer_local(), sm.doctest_offset_line(&loc.file.name, loc.line), loc.col.0 + 1, ), @@ -1347,12 +1347,12 @@ impl EmitterWriter { }; format!( "{}:{}{}", - annotated_file.file.name, + annotated_file.file.name.prefer_local(), sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index), col ) } else { - annotated_file.file.name.to_string() + format!("{}", annotated_file.file.name.prefer_local()) }; buffer.append(buffer_msg_line_offset + 1, &loc, Style::LineAndColumn); for _ in 0..max_line_num_len { diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index 40277006462..5d175a3ade9 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -468,7 +468,7 @@ impl DiagnosticSpan { }); DiagnosticSpan { - file_name: start.file.name.to_string(), + file_name: start.file.name.prefer_local().to_string(), byte_start: start.file.original_relative_byte_pos(span.lo()).0, byte_end: start.file.original_relative_byte_pos(span.hi()).0, line_start: start.line, diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 5c83d6c7ad5..8a9f3fc668e 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1080,7 +1080,10 @@ impl<'a> ExtCtxt<'a> { other => { return Err(self.struct_span_err( span, - &format!("cannot resolve relative path in non-file source `{}`", other), + &format!( + "cannot resolve relative path in non-file source `{}`", + other.prefer_local() + ), )); } }; diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 03910f4e18d..f5c6bb3db65 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -365,7 +365,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { FileName::Real(name) => name .into_local_path() .expect("attempting to resolve a file path in an external file"), - other => PathBuf::from(other.to_string()), + other => PathBuf::from(other.prefer_local().to_string()), }; let dir_path = file_path.parent().unwrap_or(&file_path).to_owned(); self.cx.root_path = dir_path.clone(); diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index f91c0d83138..dba708ab2cd 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -627,7 +627,7 @@ impl server::SourceFile for Rustc<'_> { .to_str() .expect("non-UTF8 file path in `proc_macro::SourceFile::path`") .to_string(), - _ => file.name.to_string(), + _ => file.name.prefer_local().to_string(), } } fn is_real(&mut self, file: &Self::SourceFile) -> bool { diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index a91bd9ce2ff..dd3fa248bff 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1604,13 +1604,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { match (&terr, expected == found) { (TypeError::Sorts(values), extra) => { let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) { - (true, ty::Opaque(def_id, _)) => format!( - " (opaque type at {})", - self.tcx + (true, ty::Opaque(def_id, _)) => { + let pos = self + .tcx .sess .source_map() - .mk_substr_filename(self.tcx.def_span(*def_id)), - ), + .lookup_char_pos(self.tcx.def_span(*def_id).lo()); + format!( + " (opaque type at <{}:{}:{}>)", + pos.file.name.prefer_local(), + pos.line, + pos.col.to_usize() + 1, + ) + } (true, _) => format!(" ({})", ty.sort_string(self.tcx)), (false, _) => "".to_string(), }; diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 1d3109c8a29..df141f2b7bf 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -36,7 +36,6 @@ use rustc_session::output::{filename_for_input, filename_for_metadata}; use rustc_session::search_paths::PathKind; use rustc_session::Session; use rustc_span::symbol::{Ident, Symbol}; -use rustc_span::FileName; use rustc_trait_selection::traits; use rustc_typeck as typeck; use tracing::{info, warn}; @@ -575,14 +574,7 @@ fn write_out_deps( .iter() .filter(|fmap| fmap.is_real_file()) .filter(|fmap| !fmap.is_imported()) - .map(|fmap| { - escape_dep_filename(&match &fmap.name { - FileName::Real(real) => { - real.local_path().unwrap_or(real.stable_name()).display().to_string() - } - _ => fmap.name.to_string(), - }) - }) + .map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string())) .collect(); if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend { diff --git a/compiler/rustc_mir/src/interpret/eval_context.rs b/compiler/rustc_mir/src/interpret/eval_context.rs index 2d83d6cfbdc..e9dd7a3fe68 100644 --- a/compiler/rustc_mir/src/interpret/eval_context.rs +++ b/compiler/rustc_mir/src/interpret/eval_context.rs @@ -263,7 +263,13 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> { } if !self.span.is_dummy() { let lo = tcx.sess.source_map().lookup_char_pos(self.span.lo()); - write!(f, " at {}:{}:{}", lo.file.name, lo.line, lo.col.to_usize() + 1)?; + write!( + f, + " at {}:{}:{}", + lo.file.name.prefer_local(), + lo.line, + lo.col.to_usize() + 1 + )?; } Ok(()) }) diff --git a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs index 4dfdc08b875..2b996cf62a3 100644 --- a/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs +++ b/compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs @@ -106,7 +106,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); ( - Symbol::intern(&caller.file.name.to_string()), + Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()), u32::try_from(caller.line).unwrap(), u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(), ) diff --git a/compiler/rustc_mir/src/transform/coverage/mod.rs b/compiler/rustc_mir/src/transform/coverage/mod.rs index eaeb44289cf..1270be5a52e 100644 --- a/compiler/rustc_mir/src/transform/coverage/mod.rs +++ b/compiler/rustc_mir/src/transform/coverage/mod.rs @@ -290,7 +290,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> { let tcx = self.tcx; let source_map = tcx.sess.source_map(); let body_span = self.body_span; - let file_name = Symbol::intern(&self.source_file.name.to_string()); + let file_name = Symbol::intern(&self.source_file.name.prefer_remapped().to_string_lossy()); let mut bcb_counters = IndexVec::from_elem_n(None, self.basic_coverage_blocks.num_nodes()); for covspan in coverage_spans { diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index 905077a48e2..077b19fa959 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -188,8 +188,10 @@ pub fn maybe_file_to_stream( override_span: Option, ) -> Result<(TokenStream, Vec), Vec> { let src = source_file.src.as_ref().unwrap_or_else(|| { - sess.span_diagnostic - .bug(&format!("cannot lex `source_file` without source: {}", source_file.name)); + sess.span_diagnostic.bug(&format!( + "cannot lex `source_file` without source: {}", + source_file.name.prefer_local() + )); }); let (token_trees, unmatched_braces) = diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index 06b2e41daf5..54b6a121585 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -1112,7 +1112,7 @@ impl<'tcx> DumpVisitor<'tcx> { name: String::new(), qualname, span, - value: filename.to_string(), + value: filename.prefer_remapped().to_string(), children, parent: None, decl_id: None, diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index c19c16b88a7..7e60d881d89 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -80,7 +80,7 @@ impl<'tcx> SaveContext<'tcx> { let end = sm.lookup_char_pos(span.hi()); SpanData { - file_name: start.file.name.to_string().into(), + file_name: start.file.name.prefer_remapped().to_string().into(), byte_start: span.lo().0, byte_end: span.hi().0, line_start: Row::new_one_indexed(start.line as u32), @@ -290,7 +290,7 @@ impl<'tcx> SaveContext<'tcx> { name: item.ident.to_string(), qualname, span: self.span_from_span(item.ident.span), - value: filename.to_string(), + value: filename.prefer_remapped().to_string(), parent: None, children: m .item_ids diff --git a/compiler/rustc_save_analysis/src/span_utils.rs b/compiler/rustc_save_analysis/src/span_utils.rs index 3c4d7972380..1947b04f441 100644 --- a/compiler/rustc_save_analysis/src/span_utils.rs +++ b/compiler/rustc_save_analysis/src/span_utils.rs @@ -34,12 +34,7 @@ impl<'a> SpanUtils<'a> { .to_string() } } - // If the file name was remapped, we assume the user - // configured it the way they wanted to, so use that directly - FileName::Real(RealFileName::Remapped { local_path: _, virtual_name }) => { - virtual_name.display().to_string() - } - filename => filename.to_string(), + filename => filename.prefer_remapped().to_string(), } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 787fe1d0f78..458874e4e65 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -305,6 +305,16 @@ impl FileName { } } + pub fn prefer_remapped(&self) -> FileNameDisplay<'_> { + FileNameDisplay { inner: self, prefer_local: false } + } + + // This may include transient local filesystem information. + // Must not be embedded in build outputs. + pub fn prefer_local(&self) -> FileNameDisplay<'_> { + FileNameDisplay { inner: self, prefer_local: true } + } + pub fn macro_expansion_source_code(src: &str) -> FileName { let mut hasher = StableHasher::new(); src.hash(&mut hasher); diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 4e60d071c68..82e6c579be4 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -369,11 +369,6 @@ impl SourceMap { source_file } - pub fn mk_substr_filename(&self, sp: Span) -> String { - let pos = self.lookup_char_pos(sp.lo()); - format!("<{}:{}:{}>", pos.file.name, pos.line, pos.col.to_usize() + 1) - } - // If there is a doctest offset, applies it to the line. pub fn doctest_offset_line(&self, file: &FileName, orig: usize) -> usize { match file { @@ -420,7 +415,7 @@ impl SourceMap { let hi = self.lookup_char_pos(sp.hi()); format!( "{}:{}:{}: {}:{}", - lo.file.name, + lo.file.name.prefer_remapped(), lo.line, lo.col.to_usize() + 1, hi.line, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 33cb11e539b..3999ce7fa2a 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -93,7 +93,7 @@ impl ExternalCrate { crate fn src_root(&self, tcx: TyCtxt<'_>) -> PathBuf { match self.src(tcx) { - FileName::Real(ref p) => match p.local_path().parent() { + FileName::Real(ref p) => match p.local_path_if_available().parent() { Some(p) => p.to_path_buf(), None => PathBuf::new(), }, diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 576f27f8f5c..780f451de59 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -840,7 +840,7 @@ impl Collector { if !item_path.is_empty() { item_path.push(' '); } - format!("{} - {}(line {})", filename, item_path, line) + format!("{} - {}(line {})", filename.prefer_local(), item_path, line) } crate fn set_position(&mut self, position: Span) { @@ -891,7 +891,7 @@ impl Tester for Collector { local_path.to_path_buf() } else { // Somehow we got the filename from the metadata of another crate, should never happen - PathBuf::from(r"doctest.rs") + unreachable!("doctest from a different crate"); } } _ => PathBuf::from(r"doctest.rs"), @@ -899,7 +899,8 @@ impl Tester for Collector { // For example `module/file.rs` would become `module_file_rs` let file = filename - .to_string() + .prefer_local() + .to_string_lossy() .chars() .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }) .collect::(); diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 95f3a32e41f..288c7785bc9 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -376,17 +376,10 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { } = options; let src_root = match krate.src { - FileName::Real(ref p) => { - if let Some(local_path) = p.local_path() { - match local_path.parent() { - Some(p) => p.to_path_buf(), - None => PathBuf::new(), - } - } else { - // Somehow we got the filename from the metadata of another crate, should never happen - PathBuf::new() - } - } + FileName::Real(ref p) => match p.local_path_if_available().parent() { + Some(p) => p.to_path_buf(), + None => PathBuf::new(), + }, _ => PathBuf::new(), }; // If user passed in `--playground-url` arg, we fill in crate name here diff --git a/src/librustdoc/html/sources.rs b/src/librustdoc/html/sources.rs index 9753179b6e1..55bef87496d 100644 --- a/src/librustdoc/html/sources.rs +++ b/src/librustdoc/html/sources.rs @@ -56,7 +56,11 @@ impl DocFolder for SourceCollector<'_, '_> { Err(e) => { self.scx.tcx.sess.span_err( item.span(self.scx.tcx).inner(), - &format!("failed to render source code for `{}`: {}", filename, e), + &format!( + "failed to render source code for `{}`: {}", + filename.prefer_local(), + e + ), ); false } @@ -80,7 +84,7 @@ impl SourceCollector<'_, 'tcx> { if let Some(local_path) = file.local_path() { local_path.to_path_buf() } else { - return Ok(()); + unreachable!("only the current crate should have sources emitted"); } } _ => return Ok(()), @@ -119,7 +123,7 @@ impl SourceCollector<'_, 'tcx> { href.push_str(&fname.to_string_lossy()); let title = format!("{} - source", src_fname.to_string_lossy()); - let desc = format!("Source of the Rust file `{}`.", filename); + let desc = format!("Source of the Rust file `{}`.", filename.prefer_remapped()); let page = layout::Page { title: &title, css_class: "source", diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs index 6c3881811b3..c115f741afc 100644 --- a/src/librustdoc/passes/calculate_doc_coverage.rs +++ b/src/librustdoc/passes/calculate_doc_coverage.rs @@ -119,7 +119,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> { &self .items .iter() - .map(|(k, v)| (k.to_string(), v)) + .map(|(k, v)| (k.prefer_local().to_string(), v)) .collect::>(), ) .expect("failed to convert JSON data to string") @@ -159,7 +159,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> { for (file, &count) in &self.items { if let Some(percentage) = count.percentage() { print_table_record( - &limit_filename_len(file.to_string()), + &limit_filename_len(file.prefer_local().to_string_lossy().into()), count, percentage, count.examples_percentage().unwrap_or(0.), diff --git a/src/tools/clippy/clippy_lints/src/macro_use.rs b/src/tools/clippy/clippy_lints/src/macro_use.rs index ec03daff87b..314bf11e2d6 100644 --- a/src/tools/clippy/clippy_lints/src/macro_use.rs +++ b/src/tools/clippy/clippy_lints/src/macro_use.rs @@ -47,7 +47,7 @@ pub struct MacroRefData { impl MacroRefData { pub fn new(name: String, callee: Span, cx: &LateContext<'_>) -> Self { - let mut path = cx.sess().source_map().span_to_filename(callee).to_string(); + let mut path = cx.sess().source_map().span_to_filename(callee).prefer_local().to_string(); // std lib paths are <::std::module::file type> // so remove brackets, space and type. @@ -96,8 +96,7 @@ impl MacroUseImports { let name = snippet(cx, cx.sess().source_map().span_until_char(call_site, '!'), "_"); if let Some(callee) = span.source_callee() { if !self.collected.contains(&call_site) { - self.mac_refs - .push(MacroRefData::new(name.to_string(), callee.def_site, cx)); + self.mac_refs.push(MacroRefData::new(name.to_string(), callee.def_site, cx)); self.collected.insert(call_site); } } @@ -175,7 +174,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports { .push((*item).to_string()); check_dup.push((*item).to_string()); } - }, + } [root, rest @ ..] => { if rest.iter().all(|item| !check_dup.contains(&(*item).to_string())) { let filtered = rest @@ -199,7 +198,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports { .push(rest.join("::")); check_dup.extend(rest.iter().map(ToString::to_string)); } - }, + } } } } -- cgit 1.4.1-3-g733a5