Auto merge of #83813 - cbeuw:remap-std, r=michaelwoerister
Fix `--remap-path-prefix` not correctly remapping `rust-src` component paths and unify handling of path mapping with virtualized paths This PR fixes #73167 ("Binaries end up containing path to the rust-src component despite `--remap-path-prefix`") by preventing real local filesystem paths from reaching compilation output if the path is supposed to be remapped. `RealFileName::Named` introduced in #72767 is now renamed as `LocalPath`, because this variant wraps a (most likely) valid local filesystem path. `RealFileName::Devirtualized` is renamed as `Remapped` to be used for remapped path from a real path via `--remap-path-prefix` argument, as well as real path inferred from a virtualized (during compiler bootstrapping) `/rustc/...` path. The `local_path` field is now an `Option<PathBuf>`, as it will be set to `None` before serialisation, so it never reaches any build output. Attempting to serialise a non-`None` `local_path` will cause an assertion faliure. When a path is remapped, a `RealFileName::Remapped` variant is created. The original path is preserved in `local_path` field and the remapped path is saved in `virtual_name` field. Previously, the `local_path` is directly modified which goes against its purpose of "suitable for reading from the file system on the local host". `rustc_span::SourceFile`'s fields `unmapped_path` (introduced by #44940) and `name_was_remapped` (introduced by #41508 when `--remap-path-prefix` feature originally added) are removed, as these two pieces of information can be inferred from the `name` field: if it's anything other than a `FileName::Real(_)`, or if it is a `FileName::Real(RealFileName::LocalPath(_))`, then clearly `name_was_remapped` would've been false and `unmapped_path` would've been `None`. If it is a `FileName::Real(RealFileName::Remapped{local_path, virtual_name})`, then `name_was_remapped` would've been true and `unmapped_path` would've been `Some(local_path)`. cc `@eddyb` who implemented `/rustc/...` path devirtualisation
This commit is contained in:
commit
e1ff91f439
48 changed files with 442 additions and 265 deletions
|
@ -1651,9 +1651,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
if let Some(virtual_dir) = virtual_rust_source_base_dir {
|
||||
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
|
||||
if let rustc_span::FileName::Real(old_name) = name {
|
||||
if let rustc_span::RealFileName::Named(one_path) = old_name {
|
||||
if let Ok(rest) = one_path.strip_prefix(virtual_dir) {
|
||||
let virtual_name = one_path.clone();
|
||||
if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
|
||||
old_name
|
||||
{
|
||||
if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) {
|
||||
let virtual_name = virtual_name.clone();
|
||||
|
||||
// The std library crates are in
|
||||
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
|
||||
|
@ -1689,8 +1691,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
virtual_name.display(),
|
||||
new_path.display(),
|
||||
);
|
||||
let new_name = rustc_span::RealFileName::Devirtualized {
|
||||
local_path: new_path,
|
||||
let new_name = rustc_span::RealFileName::Remapped {
|
||||
local_path: Some(new_path),
|
||||
virtual_name,
|
||||
};
|
||||
*old_name = new_name;
|
||||
|
@ -1710,7 +1712,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
// containing the information we need.
|
||||
let rustc_span::SourceFile {
|
||||
mut name,
|
||||
name_was_remapped,
|
||||
src_hash,
|
||||
start_pos,
|
||||
end_pos,
|
||||
|
@ -1722,11 +1723,34 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
..
|
||||
} = source_file_to_import;
|
||||
|
||||
// If this file is under $sysroot/lib/rustlib/src/ but has not been remapped
|
||||
// during rust bootstrapping by `remap-debuginfo = true`, and the user
|
||||
// wish to simulate that behaviour by -Z simulate-remapped-rust-src-base,
|
||||
// then we change `name` to a similar state as if the rust was bootstrapped
|
||||
// with `remap-debuginfo = true`.
|
||||
// This is useful for testing so that tests about the effects of
|
||||
// `try_to_translate_virtual_to_real` don't have to worry about how the
|
||||
// compiler is bootstrapped.
|
||||
if let Some(virtual_dir) =
|
||||
&sess.opts.debugging_opts.simulate_remapped_rust_src_base
|
||||
{
|
||||
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
|
||||
if let rustc_span::FileName::Real(ref mut old_name) = name {
|
||||
if let rustc_span::RealFileName::LocalPath(local) = old_name {
|
||||
if let Ok(rest) = local.strip_prefix(real_dir) {
|
||||
*old_name = rustc_span::RealFileName::Remapped {
|
||||
local_path: None,
|
||||
virtual_name: virtual_dir.join(rest),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If this file's path has been remapped to `/rustc/$hash`,
|
||||
// we might be able to reverse that (also see comments above,
|
||||
// on `try_to_translate_virtual_to_real`).
|
||||
// FIXME(eddyb) we could check `name_was_remapped` here,
|
||||
// but in practice it seems to be always `false`.
|
||||
try_to_translate_virtual_to_real(&mut name);
|
||||
|
||||
let source_length = (end_pos - start_pos).to_usize();
|
||||
|
@ -1751,7 +1775,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
|||
|
||||
let local_version = sess.source_map().new_imported_source_file(
|
||||
name,
|
||||
name_was_remapped,
|
||||
src_hash,
|
||||
name_hash,
|
||||
source_length,
|
||||
|
|
|
@ -28,9 +28,12 @@ use rustc_middle::ty::codec::TyEncoder;
|
|||
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
|
||||
use rustc_serialize::{opaque, Encodable, Encoder};
|
||||
use rustc_session::config::CrateType;
|
||||
use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext, MacroKind};
|
||||
use rustc_span::symbol::{sym, Ident, Symbol};
|
||||
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext};
|
||||
use rustc_span::{
|
||||
hygiene::{ExpnDataEncodeMode, HygieneEncodeContext, MacroKind},
|
||||
RealFileName,
|
||||
};
|
||||
use rustc_target::abi::VariantIdx;
|
||||
use std::hash::Hash;
|
||||
use std::num::NonZeroUsize;
|
||||
|
@ -466,7 +469,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
let source_map = self.tcx.sess.source_map();
|
||||
let all_source_files = source_map.files();
|
||||
|
||||
let (working_dir, _cwd_remapped) = self.tcx.sess.working_dir.clone();
|
||||
// By replacing the `Option` with `None`, we ensure that we can't
|
||||
// accidentally serialize any more `Span`s after the source map encoding
|
||||
// is done.
|
||||
|
@ -485,18 +487,41 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||
})
|
||||
.map(|(_, source_file)| {
|
||||
let mut adapted = match source_file.name {
|
||||
// This path of this SourceFile has been modified by
|
||||
// path-remapping, so we use it verbatim (and avoid
|
||||
// cloning the whole map in the process).
|
||||
_ if source_file.name_was_remapped => source_file.clone(),
|
||||
|
||||
// Otherwise expand all paths to absolute paths because
|
||||
// any relative paths are potentially relative to a
|
||||
// wrong directory.
|
||||
FileName::Real(ref name) => {
|
||||
let name = name.stable_name();
|
||||
FileName::Real(ref realname) => {
|
||||
let mut adapted = (**source_file).clone();
|
||||
adapted.name = Path::new(&working_dir).join(name).into();
|
||||
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.
|
||||
let working_dir = &self.tcx.sess.working_dir;
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue