From 00700e5a4a15eeb8acb0479991a56e2c573c0886 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sat, 3 Apr 2021 16:59:31 +0100 Subject: [PATCH] Remove name_was_remapped field from SourceFile as this can now be inferred by RealFileName variants --- compiler/rustc_metadata/src/rmeta/decoder.rs | 4 --- compiler/rustc_metadata/src/rmeta/encoder.rs | 29 ++++++++++++------- compiler/rustc_middle/src/ich/impls_syntax.rs | 2 -- .../rustc_save_analysis/src/span_utils.rs | 8 +++-- compiler/rustc_span/src/lib.rs | 8 ----- compiler/rustc_span/src/source_map.rs | 14 ++++----- compiler/rustc_span/src/source_map/tests.rs | 2 -- 7 files changed, 28 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index ae506849d15f3..4b8cedbc98429 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1692,7 +1692,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, @@ -1707,8 +1706,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // 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(); @@ -1733,7 +1730,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, diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index d055c275299a6..9fb3e7abfbd59 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -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; @@ -485,18 +488,22 @@ 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(); + // Expand all local paths to absolute paths because + // any relative paths are potentially relative to a + // wrong directory. let mut adapted = (**source_file).clone(); - adapted.name = Path::new(&working_dir).join(name).into(); + adapted.name = match name { + RealFileName::Named(local_path) => { + Path::new(&working_dir).join(local_path).into() + } + RealFileName::Virtualized { local_path, virtual_name } => { + FileName::Real(RealFileName::Virtualized { + local_path: Path::new(&working_dir).join(local_path), + virtual_name: virtual_name.clone(), + }) + } + }; adapted.name_hash = { let mut hasher: StableHasher = StableHasher::new(); adapted.name.hash(&mut hasher); diff --git a/compiler/rustc_middle/src/ich/impls_syntax.rs b/compiler/rustc_middle/src/ich/impls_syntax.rs index daf2684207968..358930f116a40 100644 --- a/compiler/rustc_middle/src/ich/impls_syntax.rs +++ b/compiler/rustc_middle/src/ich/impls_syntax.rs @@ -61,7 +61,6 @@ impl<'a> HashStable> for SourceFile { let SourceFile { name: _, // We hash the smaller name_hash instead of this name_hash, - name_was_remapped, cnum, // Do not hash the source as it is not encoded src: _, @@ -76,7 +75,6 @@ impl<'a> HashStable> for SourceFile { } = *self; (name_hash as u64).hash_stable(hcx, hasher); - name_was_remapped.hash_stable(hcx, hasher); src_hash.hash_stable(hcx, hasher); diff --git a/compiler/rustc_save_analysis/src/span_utils.rs b/compiler/rustc_save_analysis/src/span_utils.rs index edcd492577374..d43bb2423018c 100644 --- a/compiler/rustc_save_analysis/src/span_utils.rs +++ b/compiler/rustc_save_analysis/src/span_utils.rs @@ -16,8 +16,7 @@ impl<'a> SpanUtils<'a> { pub fn make_filename_string(&self, file: &SourceFile) -> String { match &file.name { - FileName::Real(name) if !file.name_was_remapped => { - let path = name.local_path(); + FileName::Real(RealFileName::Named(path)) => { if path.is_absolute() { self.sess .source_map() @@ -30,8 +29,11 @@ impl<'a> SpanUtils<'a> { self.sess.working_dir.0.join(&path).display().to_string() } } - // If the file name is already remapped, we assume the user + // If the file name was remapped, we assume the user // configured it the way they wanted to, so use that directly + FileName::Real(RealFileName::Virtualized { local_path: _, virtual_name }) => { + virtual_name.display().to_string() + } filename => filename.to_string(), } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index c17f90bbe6c23..2a9c507e3edf8 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1118,8 +1118,6 @@ pub struct SourceFile { /// originate from files has names between angle brackets by convention /// (e.g., ``). pub name: FileName, - /// `true` if the `name` field above has been modified by `--remap-path-prefix`. - pub name_was_remapped: bool, /// The complete source code. pub src: Option>, /// The source code's hash. @@ -1149,7 +1147,6 @@ impl Encodable 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))?; @@ -1224,8 +1221,6 @@ impl Decodable for SourceFile { fn decode(d: &mut D) -> Result { 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 = @@ -1269,7 +1264,6 @@ impl Decodable for SourceFile { let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?; Ok(SourceFile { name, - name_was_remapped, start_pos, end_pos, src: None, @@ -1297,7 +1291,6 @@ impl fmt::Debug for SourceFile { impl SourceFile { pub fn new( name: FileName, - name_was_remapped: bool, mut src: String, start_pos: BytePos, hash_kind: SourceFileHashAlgorithm, @@ -1319,7 +1312,6 @@ impl SourceFile { SourceFile { name, - name_was_remapped, src: Some(Lrc::new(src)), src_hash, external_src: Lock::new(ExternalSource::Unneeded), diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 24ad742177f06..4fa98f3fa7ec8 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -127,17 +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) + StableSourceFileId::new_from_name(&source_file.name) } - fn new_from_pieces(name: &FileName, name_was_remapped: bool) -> StableSourceFileId { + fn new_from_name(name: &FileName) -> StableSourceFileId { let mut hasher = StableHasher::new(); // If name was virtualized, 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_was_remapped.hash(&mut hasher); StableSourceFileId(hasher.finish()) } @@ -276,9 +275,9 @@ impl SourceMap { // Note that filename may not be a valid path, eg it may be `` etc, // but this is okay because the directory determined by `path.pop()` will // be empty, so the working directory will be used. - let (filename, was_remapped) = self.path_mapping.map_filename_prefix(&filename); + let (filename, _) = self.path_mapping.map_filename_prefix(&filename); - let file_id = StableSourceFileId::new_from_pieces(&filename, was_remapped); + 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, @@ -287,7 +286,6 @@ impl SourceMap { let source_file = Lrc::new(SourceFile::new( filename, - was_remapped, src, Pos::from_usize(start_pos), self.hash_kind, @@ -311,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, @@ -346,11 +343,10 @@ impl SourceMap { nc.pos = nc.pos + start_pos; } - let (filename, name_is_remapped) = self.path_mapping.map_filename_prefix(&filename); + let (filename, _) = self.path_mapping.map_filename_prefix(&filename); let source_file = Lrc::new(SourceFile { name: filename, - name_was_remapped: name_was_remapped || name_is_remapped, src: None, src_hash, external_src: Lock::new(ExternalSource::Foreign { diff --git a/compiler/rustc_span/src/source_map/tests.rs b/compiler/rustc_span/src/source_map/tests.rs index 7d814f1d82c11..ee87ef0b5e5e7 100644 --- a/compiler/rustc_span/src/source_map/tests.rs +++ b/compiler/rustc_span/src/source_map/tests.rs @@ -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(),