1
Fork 0

Move logic for making potentially remapped paths absolute into helper method.

This commit is contained in:
Michael Woerister 2022-05-05 17:03:51 +02:00
parent 90fce805a3
commit 583880b0ff
2 changed files with 108 additions and 64 deletions

View file

@ -33,18 +33,14 @@ use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_serialize::{opaque, Encodable, Encoder};
use rustc_session::config::CrateType;
use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{
self, DebuggerVisualizerFile, ExternalSource, FileName, SourceFile, Span, SyntaxContext,
};
use rustc_span::{
hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind},
RealFileName,
};
use rustc_target::abi::VariantIdx;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::path::Path;
use tracing::{debug, trace};
pub(super) struct EncodeContext<'a, 'tcx> {
@ -490,6 +486,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// is done.
let required_source_files = self.required_source_files.take().unwrap();
let working_directory = &self.tcx.sess.opts.working_dir;
let adapted = all_source_files
.iter()
.enumerate()
@ -502,66 +500,33 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
(!source_file.is_imported() || self.is_proc_macro)
})
.map(|(_, source_file)| {
let mut adapted = match source_file.name {
FileName::Real(ref realname) => {
let mut adapted = (**source_file).clone();
adapted.name = FileName::Real(match realname {
RealFileName::LocalPath(path_to_file) => {
// Prepend path of working directory onto potentially
// relative paths, because they could become relative
// to a wrong directory.
// We include `working_dir` as part of the crate hash,
// so it's okay for us to use it as part of the encoded
// metadata.
let working_dir = &self.tcx.sess.opts.working_dir;
match working_dir {
RealFileName::LocalPath(absolute) => {
// Although neither working_dir or the file name were subject
// to path remapping, the concatenation between the two may
// be. Hence we need to do a remapping here.
let joined = Path::new(absolute).join(path_to_file);
let (joined, remapped) =
source_map.path_mapping().map_prefix(joined);
if remapped {
RealFileName::Remapped {
local_path: None,
virtual_name: joined,
}
} else {
RealFileName::LocalPath(joined)
}
}
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),
}
}
}
}
RealFileName::Remapped { local_path: _, virtual_name } => {
RealFileName::Remapped {
// We do not want any local path to be exported into metadata
local_path: None,
virtual_name: virtual_name.clone(),
}
}
});
adapted.name_hash = {
let mut hasher: StableHasher = StableHasher::new();
adapted.name.hash(&mut hasher);
hasher.finish::<u128>()
};
Lrc::new(adapted)
}
match source_file.name {
FileName::Real(ref original_file_name) => {
let adapted_file_name =
source_map.path_mapping().to_embeddable_absolute_path(
original_file_name.clone(),
working_directory,
);
if adapted_file_name != *original_file_name {
let mut adapted: SourceFile = (**source_file).clone();
adapted.name = FileName::Real(adapted_file_name);
adapted.name_hash = {
let mut hasher: StableHasher = StableHasher::new();
adapted.name.hash(&mut hasher);
hasher.finish::<u128>()
};
Lrc::new(adapted)
} else {
// Nothing to adapt
source_file.clone()
}
}
// expanded code, not from a file
_ => source_file.clone(),
};
}
})
.map(|mut source_file| {
// We're serializing this `SourceFile` into our crate metadata,
// so mark it as coming from this crate.
// This also ensures that we don't try to deserialize the
@ -569,9 +534,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// dependencies aren't loaded when we deserialize a proc-macro,
// trying to remap the `CrateNum` would fail.
if self.is_proc_macro {
Lrc::make_mut(&mut adapted).cnum = LOCAL_CRATE;
Lrc::make_mut(&mut source_file).cnum = LOCAL_CRATE;
}
adapted
source_file
})
.collect::<Vec<_>>();