Disambiguate between SourceFiles from different crates even if they have the same path.

This commit is contained in:
Michael Woerister 2021-06-16 16:48:06 +02:00
parent da5c911b5f
commit 47327145e3

View File

@ -117,25 +117,42 @@ impl FileLoader for RealFileLoader {
}
}
// This is a `SourceFile` identifier that is used to correlate `SourceFile`s between
// subsequent compilation sessions (which is something we need to do during
// incremental compilation).
/// This is a [SourceFile] identifier that is used to correlate source files between
/// subsequent compilation sessions (which is something we need to do during
/// 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)]
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
// StableSourceFileId, perhaps built atop source_file.name_hash.
impl 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();
name.hash(&mut hasher);
StableSourceFileId(hasher.finish())
StableSourceFileId { file_name_hash: hasher.finish(), cnum }
}
}
@ -274,7 +291,7 @@ impl SourceMap {
// be empty, so the working directory will be used.
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) {
Some(lrc_sf) => lrc_sf,
@ -288,6 +305,10 @@ impl SourceMap {
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();
files.source_files.push(source_file.clone());