Skip to content

Commit

Permalink
Remove name_was_remapped field from SourceFile as this can now be inf…
Browse files Browse the repository at this point in the history
…erred by RealFileName variants
  • Loading branch information
cbeuw committed Apr 3, 2021
1 parent ca30ad1 commit 00700e5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 39 deletions.
4 changes: 0 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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,
Expand Down
29 changes: 18 additions & 11 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ impl<'a> HashStable<StableHashingContext<'a>> 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: _,
Expand All @@ -76,7 +75,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
} = *self;

(name_hash as u64).hash_stable(hcx, hasher);
name_was_remapped.hash_stable(hcx, hasher);

src_hash.hash_stable(hcx, hasher);

Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_save_analysis/src/span_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(),
}
}
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,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 complete source code.
pub src: Option<Lrc<String>>,
/// The source code's hash.
Expand Down Expand Up @@ -1149,7 +1147,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))?;
Expand Down Expand Up @@ -1224,8 +1221,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 =
Expand Down Expand Up @@ -1269,7 +1264,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,
start_pos,
end_pos,
src: None,
Expand Down Expand Up @@ -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,
Expand All @@ -1319,7 +1312,6 @@ impl SourceFile {

SourceFile {
name,
name_was_remapped,
src: Some(Lrc::new(src)),
src_hash,
external_src: Lock::new(ExternalSource::Unneeded),
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -276,9 +275,9 @@ impl SourceMap {
// 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 (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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_span/src/source_map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ fn t10() {

let SourceFile {
name,
name_was_remapped,
src_hash,
start_pos,
end_pos,
Expand All @@ -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(),
Expand Down

0 comments on commit 00700e5

Please sign in to comment.