Skip to content

Commit fb4f643

Browse files
committed
Revamp RealFileName public methods
1 parent f8e55da commit fb4f643

File tree

7 files changed

+53
-41
lines changed

7 files changed

+53
-41
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn line_program_add_file(
6666
) -> FileId {
6767
match &file.name {
6868
FileName::Real(path) => {
69-
let (dir_path, file_name) = split_path_dir_and_file(path.stable_name());
69+
let (dir_path, file_name) = split_path_dir_and_file(path.remapped_path_if_available());
7070
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
7171
let file_name = osstr_as_utf8_bytes(file_name);
7272

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'tcx> DebugContext<'tcx> {
6464
// FIXME: how to get version when building out of tree?
6565
// Normally this would use option_env!("CFG_VERSION").
6666
let producer = format!("cg_clif (rustc {})", "unknown version");
67-
let comp_dir = tcx.sess.working_dir.stable_name().to_string_lossy().into_owned();
67+
let comp_dir = tcx.sess.working_dir.to_string_lossy(false).into_owned();
6868
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
6969
Some(path) => {
7070
let name = path.to_string_lossy().into_owned();

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
764764
let hash = Some(&source_file.src_hash);
765765
let file_name = Some(source_file.name.to_string());
766766
let directory = if source_file.is_real_file() && !source_file.is_imported() {
767-
Some(cx.sess().working_dir.stable_name().to_string_lossy().to_string())
767+
Some(cx.sess().working_dir.to_string_lossy(false).to_string())
768768
} else {
769769
// If the path comes from an upstream crate we assume it has been made
770770
// independent of the compiler's working directory one way or another.
@@ -992,7 +992,7 @@ pub fn compile_unit_metadata(
992992
let producer = format!("clang LLVM ({})", rustc_producer);
993993

994994
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
995-
let work_dir = tcx.sess.working_dir.stable_name().to_string_lossy();
995+
let work_dir = tcx.sess.working_dir.to_string_lossy(false);
996996
let flags = "\0";
997997
let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory;
998998
let split_name = if tcx.sess.target_can_use_split_dwarf() {

compiler/rustc_metadata/src/rmeta/encoder.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -490,22 +490,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
490490
FileName::Real(ref realname) => {
491491
let mut adapted = (**source_file).clone();
492492
adapted.name = FileName::Real(match realname {
493-
RealFileName::LocalPath(local_path) => {
494-
// Prepend path of working directory onto local path.
495-
// because relative paths are potentially relative to a
496-
// wrong directory.
493+
RealFileName::LocalPath(path_to_file) => {
494+
// Prepend path of working directory onto potentially
495+
// relative paths, because they could become relative
496+
// to a wrong directory.
497497
let working_dir = &self.tcx.sess.working_dir;
498-
if let RealFileName::LocalPath(absolute) = working_dir {
499-
// If working_dir has not been remapped, then we emit a
500-
// LocalPath variant as it's likely to be a valid path
501-
RealFileName::LocalPath(Path::new(absolute).join(local_path))
502-
} else {
503-
// If working_dir has been remapped, then we emit
504-
// Remapped variant as the expanded path won't be valid
505-
RealFileName::Remapped {
506-
local_path: None,
507-
virtual_name: Path::new(working_dir.stable_name())
508-
.join(local_path),
498+
match working_dir {
499+
RealFileName::LocalPath(absolute) => {
500+
// If working_dir has not been remapped, then we emit a
501+
// LocalPath variant as it's likely to be a valid path
502+
RealFileName::LocalPath(
503+
Path::new(absolute).join(path_to_file),
504+
)
505+
}
506+
RealFileName::Remapped { local_path: _, virtual_name } => {
507+
// If working_dir has been remapped, then we emit
508+
// Remapped variant as the expanded path won't be valid
509+
RealFileName::Remapped {
510+
local_path: None,
511+
virtual_name: Path::new(virtual_name)
512+
.join(path_to_file),
513+
}
509514
}
510515
}
511516
}

compiler/rustc_save_analysis/src/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'tcx> DumpVisitor<'tcx> {
190190
};
191191

192192
let data = CompilationOptions {
193-
directory: self.tcx.sess.working_dir.stable_name().into(),
193+
directory: self.tcx.sess.working_dir.remapped_path_if_available().into(),
194194
program,
195195
arguments,
196196
output: self.save_ctxt.compilation_output(crate_name),

compiler/rustc_save_analysis/src/span_utils.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ impl<'a> SpanUtils<'a> {
2626
.display()
2727
.to_string()
2828
} else {
29-
self.sess.working_dir.stable_name().join(&path).display().to_string()
29+
self.sess
30+
.working_dir
31+
.remapped_path_if_available()
32+
.join(&path)
33+
.display()
34+
.to_string()
3035
}
3136
}
3237
// If the file name was remapped, we assume the user

compiler/rustc_span/src/lib.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<S: Encoder> Encodable<S> for RealFileName {
172172
impl RealFileName {
173173
/// Returns the path suitable for reading from the file system on the local host,
174174
/// if this information exists.
175-
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
175+
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
176176
pub fn local_path(&self) -> Option<&Path> {
177177
match self {
178178
RealFileName::LocalPath(p) => Some(p),
@@ -184,39 +184,41 @@ impl RealFileName {
184184

185185
/// Returns the path suitable for reading from the file system on the local host,
186186
/// if this information exists.
187-
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
187+
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
188188
pub fn into_local_path(self) -> Option<PathBuf> {
189189
match self {
190190
RealFileName::LocalPath(p) => Some(p),
191191
RealFileName::Remapped { local_path: p, virtual_name: _ } => p,
192192
}
193193
}
194194

195-
/// Returns the path suitable for embedding into build artifacts. Note that
196-
/// a remapped path will not correspond to a valid file system path; see
197-
/// `local_path()` for something that is more likely to return paths into the
198-
/// local host file system.
199-
pub fn stable_name(&self) -> &Path {
195+
/// Returns the path suitable for embedding into build artifacts. This would still
196+
/// be a local path if it has not been remapped. A remapped path will not correspond
197+
/// to a valid file system path: see `local_path_if_available()` for something that
198+
/// is more likely to return paths into the local host file system.
199+
pub fn remapped_path_if_available(&self) -> &Path {
200200
match self {
201201
RealFileName::LocalPath(p)
202202
| RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
203203
}
204204
}
205205

206-
fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
207-
use RealFileName::*;
206+
/// Returns the path suitable for reading from the file system on the local host,
207+
/// if this information exists. Otherwise returns the remapped name.
208+
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
209+
pub fn local_path_if_available(&self) -> &Path {
210+
match self {
211+
RealFileName::LocalPath(path)
212+
| RealFileName::Remapped { local_path: None, virtual_name: path }
213+
| RealFileName::Remapped { local_path: Some(path), virtual_name: _ } => path,
214+
}
215+
}
216+
217+
pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
208218
if prefer_local {
209-
match self {
210-
LocalPath(path)
211-
| Remapped { local_path: None, virtual_name: path }
212-
| Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(),
213-
}
219+
self.local_path_if_available().to_string_lossy()
214220
} else {
215-
match self {
216-
LocalPath(path) | Remapped { local_path: _, virtual_name: path } => {
217-
path.to_string_lossy()
218-
}
219-
}
221+
self.remapped_path_if_available().to_string_lossy()
220222
}
221223
}
222224
}
@@ -1358,7 +1360,7 @@ impl<D: Decoder> Decodable<D> for SourceFile {
13581360

13591361
impl fmt::Debug for SourceFile {
13601362
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1361-
write!(fmt, "SourceFile({})", self.name)
1363+
write!(fmt, "SourceFile({:?})", self.name)
13621364
}
13631365
}
13641366

0 commit comments

Comments
 (0)