Disambiguate between SourceFiles from different crates even if they have the same path.
This commit is contained in:
parent
da5c911b5f
commit
47327145e3
1 changed files with 31 additions and 10 deletions
|
@ -117,25 +117,42 @@ impl FileLoader for RealFileLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a `SourceFile` identifier that is used to correlate `SourceFile`s between
|
/// This is a [SourceFile] identifier that is used to correlate source files between
|
||||||
// subsequent compilation sessions (which is something we need to do during
|
/// subsequent compilation sessions (which is something we need to do during
|
||||||
// incremental compilation).
|
/// incremental compilation).
|
||||||
|
///
|
||||||
|
/// The [StableSourceFileId] also contains the CrateNum of the crate the source
|
||||||
|
/// file was originally parsed for. This way we get two separate entries in
|
||||||
|
/// the [SourceMap] if the same file is part of both the local and an upstream
|
||||||
|
/// crate. Trying to only have one entry for both cases is problematic because
|
||||||
|
/// at the point where we discover that there's a local use of the file in
|
||||||
|
/// addition to the upstream one, we might already have made decisions based on
|
||||||
|
/// the assumption that it's an upstream file. Treating the two files as
|
||||||
|
/// different has no real downsides.
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
|
||||||
pub struct StableSourceFileId(u128);
|
pub struct StableSourceFileId {
|
||||||
|
// A hash of the source file's FileName. This is hash so that it's size
|
||||||
|
// is more predictable than if we included the actual FileName value.
|
||||||
|
file_name_hash: u64,
|
||||||
|
|
||||||
|
// The CrateNum of the crate this source file was originally parsed for.
|
||||||
|
// We cannot include this information in the hash because at the time
|
||||||
|
// of hashing we don't have the context to map from the CrateNum's numeric
|
||||||
|
// value to a StableCrateId.
|
||||||
|
cnum: CrateNum,
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: we need a more globally consistent approach to the problem solved by
|
// FIXME: we need a more globally consistent approach to the problem solved by
|
||||||
// StableSourceFileId, perhaps built atop source_file.name_hash.
|
// StableSourceFileId, perhaps built atop source_file.name_hash.
|
||||||
impl StableSourceFileId {
|
impl StableSourceFileId {
|
||||||
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
|
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
|
||||||
StableSourceFileId::new_from_name(&source_file.name)
|
StableSourceFileId::new_from_name(&source_file.name, source_file.cnum)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_from_name(name: &FileName) -> StableSourceFileId {
|
fn new_from_name(name: &FileName, cnum: CrateNum) -> StableSourceFileId {
|
||||||
let mut hasher = StableHasher::new();
|
let mut hasher = StableHasher::new();
|
||||||
|
|
||||||
name.hash(&mut hasher);
|
name.hash(&mut hasher);
|
||||||
|
StableSourceFileId { file_name_hash: hasher.finish(), cnum }
|
||||||
StableSourceFileId(hasher.finish())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,7 +291,7 @@ impl SourceMap {
|
||||||
// be empty, so the working directory will be used.
|
// be empty, so the working directory will be used.
|
||||||
let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
|
let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
|
||||||
|
|
||||||
let file_id = StableSourceFileId::new_from_name(&filename);
|
let file_id = StableSourceFileId::new_from_name(&filename, LOCAL_CRATE);
|
||||||
|
|
||||||
let lrc_sf = match self.source_file_by_stable_id(file_id) {
|
let lrc_sf = match self.source_file_by_stable_id(file_id) {
|
||||||
Some(lrc_sf) => lrc_sf,
|
Some(lrc_sf) => lrc_sf,
|
||||||
|
@ -288,6 +305,10 @@ impl SourceMap {
|
||||||
self.hash_kind,
|
self.hash_kind,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// Let's make sure the file_id we generated above actually matches
|
||||||
|
// the ID we generate for the SourceFile we just created.
|
||||||
|
debug_assert_eq!(StableSourceFileId::new(&source_file), file_id);
|
||||||
|
|
||||||
let mut files = self.files.borrow_mut();
|
let mut files = self.files.borrow_mut();
|
||||||
|
|
||||||
files.source_files.push(source_file.clone());
|
files.source_files.push(source_file.clone());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue