Make local_path in RealFileName::Remapped Option to be removed in exported metadata

This commit is contained in:
Andy Wang 2021-04-09 00:54:51 +01:00 committed by Andy Wang
parent 6720a37042
commit 9e0426d784
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
11 changed files with 123 additions and 52 deletions

View File

@ -1073,7 +1073,9 @@ pub fn resolve_path(
if !path.is_absolute() { if !path.is_absolute() {
let callsite = span.source_callsite(); let callsite = span.source_callsite();
let mut result = match self.source_map().span_to_filename(callsite) { let mut result = match self.source_map().span_to_filename(callsite) {
FileName::Real(name) => name.into_local_path(), FileName::Real(name) => name
.into_local_path()
.expect("attempting to resolve a file path in an external file"),
FileName::DocTest(path, _) => path, FileName::DocTest(path, _) => path,
other => { other => {
return Err(self.struct_span_err( return Err(self.struct_span_err(

View File

@ -362,7 +362,9 @@ pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
// make crate a first class expansion target instead. // make crate a first class expansion target instead.
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
let file_path = match self.cx.source_map().span_to_filename(krate.span) { let file_path = match self.cx.source_map().span_to_filename(krate.span) {
FileName::Real(name) => name.into_local_path(), 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.to_string()),
}; };
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned(); let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();

View File

@ -623,6 +623,7 @@ fn path(&mut self, file: &Self::SourceFile) -> String {
match file.name { match file.name {
FileName::Real(ref name) => name FileName::Real(ref name) => name
.local_path() .local_path()
.expect("attempting to get a file path in an imported file in `proc_macro::SourceFile::path`")
.to_str() .to_str()
.expect("non-UTF8 file path in `proc_macro::SourceFile::path`") .expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
.to_string(), .to_string(),

View File

@ -1676,7 +1676,7 @@ fn imported_source_files(&self, sess: &Session) -> &'a [ImportedSourceFile] {
new_path.display(), new_path.display(),
); );
let new_name = rustc_span::RealFileName::Remapped { let new_name = rustc_span::RealFileName::Remapped {
local_path: new_path, local_path: Some(new_path),
virtual_name, virtual_name,
}; };
*old_name = new_name; *old_name = new_name;

View File

@ -497,9 +497,10 @@ fn encode_source_map(&mut self) -> Lazy<[rustc_span::SourceFile]> {
RealFileName::LocalPath(local_path) => { RealFileName::LocalPath(local_path) => {
Path::new(&working_dir).join(local_path).into() Path::new(&working_dir).join(local_path).into()
} }
RealFileName::Remapped { local_path, virtual_name } => { RealFileName::Remapped { local_path: _, virtual_name } => {
FileName::Real(RealFileName::Remapped { FileName::Real(RealFileName::Remapped {
local_path: Path::new(&working_dir).join(local_path), // We do not want any local path to be exported into metadata
local_path: None,
virtual_name: virtual_name.clone(), virtual_name: virtual_name.clone(),
}) })
} }

View File

@ -115,38 +115,80 @@ pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
// FIXME: We should use this enum or something like it to get rid of the // 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. // use of magic `/rust/1.x/...` paths across the board.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)] #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd)]
#[derive(HashStable_Generic, Decodable, Encodable)] #[derive(HashStable_Generic, Decodable)]
pub enum RealFileName { pub enum RealFileName {
LocalPath(PathBuf), LocalPath(PathBuf),
/// For remapped paths (namely paths into libstd that have been mapped /// 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 /// to the appropriate spot on the local host's file system, and local file
/// system paths that have been remapped with `FilePathMapping`), /// system paths that have been remapped with `FilePathMapping`),
Remapped { Remapped {
/// `local_path` is the (host-dependent) local path to the file. /// `local_path` is the (host-dependent) local path to the file. This is
local_path: PathBuf, /// None if the file was imported from another crate
local_path: Option<PathBuf>,
/// `virtual_name` is the stable path rustc will store internally within /// `virtual_name` is the stable path rustc will store internally within
/// build artifacts. /// build artifacts.
virtual_name: PathBuf, virtual_name: PathBuf,
}, },
} }
impl Hash for RealFileName {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
// To prevent #70924 from happening again we should only hash the
// remapped (virtualized) path if that exists. This is because
// virtualized paths to sysroot crates (/rust/$hash or /rust/$version)
// remain stable even if the corresponding local_path changes
self.remapped_path_if_available().hash(state)
}
}
// This is functionally identical to #[derive(Encodable)], with the exception of
// an added assert statement
impl<S: Encoder> Encodable<S> for RealFileName {
fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
encoder.emit_enum("RealFileName", |encoder| match *self {
RealFileName::LocalPath(ref local_path) => {
encoder.emit_enum_variant("LocalPath", 0, 1, |encoder| {
Ok({
encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?;
})
})
}
RealFileName::Remapped { ref local_path, ref virtual_name } => encoder
.emit_enum_variant("Remapped", 1, 2, |encoder| {
// For privacy and build reproducibility, we must not embed host-dependant path in artifacts
// if they have been remapped by --remap-path-prefix
assert!(local_path.is_none());
Ok({
encoder.emit_enum_variant_arg(0, |encoder| local_path.encode(encoder))?;
encoder.emit_enum_variant_arg(1, |encoder| virtual_name.encode(encoder))?;
})
}),
})
}
}
impl RealFileName { impl RealFileName {
/// Returns the path suitable for reading from the file system on the local host. /// 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 `stable_name()` for that.
pub fn local_path(&self) -> &Path { pub fn local_path(&self) -> Option<&Path> {
match self { match self {
RealFileName::LocalPath(p) RealFileName::LocalPath(p) => Some(p),
| RealFileName::Remapped { local_path: p, virtual_name: _ } => &p, RealFileName::Remapped { local_path: p, virtual_name: _ } => {
p.as_ref().map(PathBuf::as_path)
}
} }
} }
/// Returns the path suitable for reading from the file system on the local host. /// 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 `stable_name()` for that.
pub fn into_local_path(self) -> PathBuf { pub fn into_local_path(self) -> Option<PathBuf> {
match self { match self {
RealFileName::LocalPath(p) RealFileName::LocalPath(p) => Some(p),
| RealFileName::Remapped { local_path: p, virtual_name: _ } => p, RealFileName::Remapped { local_path: p, virtual_name: _ } => p,
} }
} }

View File

@ -133,9 +133,6 @@ pub fn new(source_file: &SourceFile) -> StableSourceFileId {
fn new_from_name(name: &FileName) -> StableSourceFileId { fn new_from_name(name: &FileName) -> StableSourceFileId {
let mut hasher = StableHasher::new(); let mut hasher = StableHasher::new();
// 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); name.hash(&mut hasher);
StableSourceFileId(hasher.finish()) StableSourceFileId(hasher.finish())
@ -954,7 +951,13 @@ pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, Str
} }
pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool { pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
source_file.add_external_src(|| match source_file.name { source_file.add_external_src(|| match source_file.name {
FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(), FileName::Real(ref name) => {
if let Some(local_path) = name.local_path() {
self.file_loader.read_file(local_path).ok()
} else {
None
}
}
_ => None, _ => None,
}) })
} }
@ -999,23 +1002,17 @@ pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) { fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
match file { match file {
FileName::Real(realfile) => { FileName::Real(realfile) => {
// If the file is the Name variant with only local_path, then clearly we want to map that if let RealFileName::LocalPath(local_path) = realfile {
// to a virtual_name let (mapped_path, mapped) = self.map_prefix(local_path.to_path_buf());
// If the file is already remapped, then we want to map virtual_name further let realfile = if mapped {
// but we leave local_path alone RealFileName::Remapped {
let path = realfile.stable_name(); local_path: Some(local_path.clone()),
let (mapped_path, mapped) = self.map_prefix(path.to_path_buf()); virtual_name: mapped_path,
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,
}
} }
} else {
realfile.clone()
}; };
(FileName::Real(mapped_realfile), mapped) (FileName::Real(realfile), mapped)
} else { } else {
unreachable!("attempted to remap an already remapped filename"); unreachable!("attempted to remap an already remapped filename");
} }

View File

@ -852,8 +852,10 @@ fn get_filename(&self) -> FileName {
let filename = source_map.span_to_filename(self.position); let filename = source_map.span_to_filename(self.position);
if let FileName::Real(ref filename) = filename { if let FileName::Real(ref filename) = filename {
if let Ok(cur_dir) = env::current_dir() { if let Ok(cur_dir) = env::current_dir() {
if let Ok(path) = filename.local_path().strip_prefix(&cur_dir) { if let Some(local_path) = filename.local_path() {
return path.to_owned().into(); if let Ok(path) = local_path.strip_prefix(&cur_dir) {
return path.to_owned().into();
}
} }
} }
} }
@ -884,7 +886,14 @@ fn add_test(&mut self, test: String, config: LangString, line: usize) {
} }
let path = match &filename { let path = match &filename {
FileName::Real(path) => path.local_path().to_path_buf(), FileName::Real(path) => {
if let Some(local_path) = path.local_path() {
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")
}
}
_ => PathBuf::from(r"doctest.rs"), _ => PathBuf::from(r"doctest.rs"),
}; };

View File

@ -290,7 +290,7 @@ pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
// We can safely ignore synthetic `SourceFile`s. // We can safely ignore synthetic `SourceFile`s.
let file = match item.span(self.tcx()).filename(self.sess()) { let file = match item.span(self.tcx()).filename(self.sess()) {
FileName::Real(ref path) => path.local_path().to_path_buf(), FileName::Real(ref path) => path.local_path_if_available().to_path_buf(),
_ => return None, _ => return None,
}; };
let file = &file; let file = &file;
@ -376,10 +376,17 @@ fn init(
} = options; } = options;
let src_root = match krate.src { let src_root = match krate.src {
FileName::Real(ref p) => match p.local_path().parent() { FileName::Real(ref p) => {
Some(p) => p.to_path_buf(), if let Some(local_path) = p.local_path() {
None => PathBuf::new(), 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()
}
}
_ => PathBuf::new(), _ => PathBuf::new(),
}; };
// If user passed in `--playground-url` arg, we fill in crate name here // If user passed in `--playground-url` arg, we fill in crate name here

View File

@ -76,7 +76,13 @@ fn sess(&self) -> &'tcx Session {
/// Renders the given filename into its corresponding HTML source file. /// Renders the given filename into its corresponding HTML source file.
fn emit_source(&mut self, filename: &FileName) -> Result<(), Error> { fn emit_source(&mut self, filename: &FileName) -> Result<(), Error> {
let p = match *filename { let p = match *filename {
FileName::Real(ref file) => file.local_path().to_path_buf(), FileName::Real(ref file) => {
if let Some(local_path) = file.local_path() {
local_path.to_path_buf()
} else {
return Ok(());
}
}
_ => return Ok(()), _ => return Ok(()),
}; };
if self.scx.local_sources.contains_key(&*p) { if self.scx.local_sources.contains_key(&*p) {

View File

@ -63,13 +63,17 @@ pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
fn convert_span(&self, span: clean::Span) -> Option<Span> { fn convert_span(&self, span: clean::Span) -> Option<Span> {
match span.filename(self.sess()) { match span.filename(self.sess()) {
rustc_span::FileName::Real(name) => { rustc_span::FileName::Real(name) => {
let hi = span.hi(self.sess()); if let Some(local_path) = name.into_local_path() {
let lo = span.lo(self.sess()); let hi = span.hi(self.sess());
Some(Span { let lo = span.lo(self.sess());
filename: name.into_local_path(), Some(Span {
begin: (lo.line, lo.col.to_usize()), filename: local_path,
end: (hi.line, hi.col.to_usize()), begin: (lo.line, lo.col.to_usize()),
}) end: (hi.line, hi.col.to_usize()),
})
} else {
None
}
} }
_ => None, _ => None,
} }