1
Fork 0

Rename RealFileName::Named to LocalPath and Devirtualized to Remapped

This commit is contained in:
Andy Wang 2021-04-03 16:59:31 +01:00
parent d66506d353
commit 6720a37042
No known key found for this signature in database
GPG key ID: 181B49F9F38F3374
13 changed files with 82 additions and 132 deletions

View file

@ -113,17 +113,16 @@ pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
// deserialization.
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
// FIXME: Perhaps this should not implement Rustc{Decodable, Encodable}
//
// FIXME: We should use this enum or something like it to get rid of the
// use of magic `/rust/1.x/...` paths across the board.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
#[derive(HashStable_Generic, Decodable, Encodable)]
pub enum RealFileName {
Named(PathBuf),
/// For de-virtualized paths (namely paths into libstd that have been mapped
/// to the appropriate spot on the local host's file system),
Devirtualized {
LocalPath(PathBuf),
/// For remapped paths (namely paths into libstd that have been mapped
/// to the appropriate spot on the local host's file system, and local file
/// system paths that have been remapped with `FilePathMapping`),
Remapped {
/// `local_path` is the (host-dependent) local path to the file.
local_path: PathBuf,
/// `virtual_name` is the stable path rustc will store internally within
@ -137,8 +136,8 @@ impl RealFileName {
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
pub fn local_path(&self) -> &Path {
match self {
RealFileName::Named(p)
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => &p,
RealFileName::LocalPath(p)
| RealFileName::Remapped { local_path: p, virtual_name: _ } => &p,
}
}
@ -146,19 +145,19 @@ impl RealFileName {
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
pub fn into_local_path(self) -> PathBuf {
match self {
RealFileName::Named(p)
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p,
RealFileName::LocalPath(p)
| RealFileName::Remapped { local_path: p, virtual_name: _ } => p,
}
}
/// Returns the path suitable for embedding into build artifacts. Note that
/// a virtualized path will not correspond to a valid file system path; see
/// 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 {
match self {
RealFileName::Named(p)
| RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p,
RealFileName::LocalPath(p)
| RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
}
}
}
@ -214,7 +213,7 @@ impl std::fmt::Display for FileName {
impl From<PathBuf> for FileName {
fn from(p: PathBuf) -> Self {
assert!(!p.to_string_lossy().ends_with('>'));
FileName::Real(RealFileName::Named(p))
FileName::Real(RealFileName::LocalPath(p))
}
}
@ -1124,11 +1123,6 @@ pub struct SourceFile {
/// originate from files has names between angle brackets by convention
/// (e.g., `<anon>`).
pub name: FileName,
/// `true` if the `name` field above has been modified by `--remap-path-prefix`.
pub name_was_remapped: bool,
/// The unmapped path of the file that the source came from.
/// Set to `None` if the `SourceFile` was imported from an external crate.
pub unmapped_path: Option<FileName>,
/// The complete source code.
pub src: Option<Lrc<String>>,
/// The source code's hash.
@ -1158,7 +1152,6 @@ impl<S: Encoder> Encodable<S> for SourceFile {
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct("SourceFile", 8, |s| {
s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?;
s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
@ -1233,8 +1226,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
d.read_struct("SourceFile", 8, |d| {
let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
let name_was_remapped: bool =
d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
let src_hash: SourceFileHash =
d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
let start_pos: BytePos =
@ -1278,8 +1269,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?;
Ok(SourceFile {
name,
name_was_remapped,
unmapped_path: None,
start_pos,
end_pos,
src: None,
@ -1307,8 +1296,6 @@ impl fmt::Debug for SourceFile {
impl SourceFile {
pub fn new(
name: FileName,
name_was_remapped: bool,
unmapped_path: FileName,
mut src: String,
start_pos: BytePos,
hash_kind: SourceFileHashAlgorithm,
@ -1330,8 +1317,6 @@ impl SourceFile {
SourceFile {
name,
name_was_remapped,
unmapped_path: Some(unmapped_path),
src: Some(Lrc::new(src)),
src_hash,
external_src: Lock::new(ExternalSource::Unneeded),

View file

@ -15,11 +15,11 @@ pub use crate::*;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock};
use std::cmp;
use std::convert::TryFrom;
use std::hash::Hash;
use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;
use std::{clone::Clone, cmp};
use std::{convert::TryFrom, unreachable};
use std::fs;
use std::io;
@ -127,30 +127,16 @@ pub struct StableSourceFileId(u128);
// StableSourceFileId, perhaps built atop source_file.name_hash.
impl StableSourceFileId {
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
StableSourceFileId::new_from_pieces(
&source_file.name,
source_file.name_was_remapped,
source_file.unmapped_path.as_ref(),
)
StableSourceFileId::new_from_name(&source_file.name)
}
fn new_from_pieces(
name: &FileName,
name_was_remapped: bool,
unmapped_path: Option<&FileName>,
) -> StableSourceFileId {
fn new_from_name(name: &FileName) -> StableSourceFileId {
let mut hasher = StableHasher::new();
if let FileName::Real(real_name) = name {
// rust-lang/rust#70924: Use the stable (virtualized) name when
// available. (We do not want artifacts from transient file system
// paths for libstd to leak into our build artifacts.)
real_name.stable_name().hash(&mut hasher)
} else {
name.hash(&mut hasher);
}
name_was_remapped.hash(&mut hasher);
unmapped_path.hash(&mut hasher);
// If name was remapped, we need to take both the local path
// and stablised path into account, in case two different paths were
// mapped to the same
name.hash(&mut hasher);
StableSourceFileId(hasher.finish())
}
@ -283,35 +269,15 @@ impl SourceMap {
fn try_new_source_file(
&self,
mut filename: FileName,
filename: FileName,
src: String,
) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
// The path is used to determine the directory for loading submodules and
// include files, so it must be before remapping.
// Note that filename may not be a valid path, eg it may be `<anon>` etc,
// but this is okay because the directory determined by `path.pop()` will
// be empty, so the working directory will be used.
let unmapped_path = filename.clone();
let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
let was_remapped;
if let FileName::Real(real_filename) = &mut filename {
match real_filename {
RealFileName::Named(path_to_be_remapped)
| RealFileName::Devirtualized {
local_path: path_to_be_remapped,
virtual_name: _,
} => {
let mapped = self.path_mapping.map_prefix(path_to_be_remapped.clone());
was_remapped = mapped.1;
*path_to_be_remapped = mapped.0;
}
}
} else {
was_remapped = false;
}
let file_id =
StableSourceFileId::new_from_pieces(&filename, was_remapped, Some(&unmapped_path));
let file_id = StableSourceFileId::new_from_name(&filename);
let lrc_sf = match self.source_file_by_stable_id(file_id) {
Some(lrc_sf) => lrc_sf,
@ -320,8 +286,6 @@ impl SourceMap {
let source_file = Lrc::new(SourceFile::new(
filename,
was_remapped,
unmapped_path,
src,
Pos::from_usize(start_pos),
self.hash_kind,
@ -345,7 +309,6 @@ impl SourceMap {
pub fn new_imported_source_file(
&self,
filename: FileName,
name_was_remapped: bool,
src_hash: SourceFileHash,
name_hash: u128,
source_len: usize,
@ -382,8 +345,6 @@ impl SourceMap {
let source_file = Lrc::new(SourceFile {
name: filename,
name_was_remapped,
unmapped_path: None,
src: None,
src_hash,
external_src: Lock::new(ExternalSource::Foreign {
@ -474,14 +435,6 @@ impl SourceMap {
self.lookup_char_pos(sp.lo()).file.name.clone()
}
pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
self.lookup_char_pos(sp.lo())
.file
.unmapped_path
.clone()
.expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
}
pub fn is_multiline(&self, sp: Span) -> bool {
let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi());
@ -1046,9 +999,26 @@ impl FilePathMapping {
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
match file {
FileName::Real(realfile) => {
let path = realfile.local_path();
let (path, mapped) = self.map_prefix(path.to_path_buf());
(FileName::Real(RealFileName::Named(path)), mapped)
// If the file is the Name variant with only local_path, then clearly we want to map that
// to a virtual_name
// If the file is already remapped, then we want to map virtual_name further
// but we leave local_path alone
let path = realfile.stable_name();
let (mapped_path, mapped) = self.map_prefix(path.to_path_buf());
if mapped {
let mapped_realfile = match realfile {
RealFileName::LocalPath(local_path)
| RealFileName::Remapped { local_path, virtual_name: _ } => {
RealFileName::Remapped {
local_path: local_path.clone(),
virtual_name: mapped_path,
}
}
};
(FileName::Real(mapped_realfile), mapped)
} else {
unreachable!("attempted to remap an already remapped filename");
}
}
other => (other.clone(), false),
}

View file

@ -229,7 +229,6 @@ fn t10() {
let SourceFile {
name,
name_was_remapped,
src_hash,
start_pos,
end_pos,
@ -243,7 +242,6 @@ fn t10() {
let imported_src_file = sm.new_imported_source_file(
name,
name_was_remapped,
src_hash,
name_hash,
(end_pos - start_pos).to_usize(),