Skip to content

Commit

Permalink
Isolate CrateNum creation to TyCtxt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 19, 2024
1 parent fbc9b94 commit 0025c9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 8 additions & 12 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,21 @@ impl CStore {
tcx: TyCtxt<'tcx>,
) -> Result<CrateNum, CrateError> {
assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
if let Some(&existing) =
tcx.untracked().stable_crate_ids.read().get(&root.stable_crate_id())
{
let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
// Check for (potential) conflicts with the local crate
if existing == LOCAL_CRATE {
Err(CrateError::SymbolConflictsCurrent(root.name()))
CrateError::SymbolConflictsCurrent(root.name())
} else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
{
let crate_name0 = root.name();
Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1))
CrateError::StableCrateIdCollision(crate_name0, crate_name1)
} else {
Err(CrateError::NotFound(root.name()))
CrateError::NotFound(root.name())
}
} else {
self.metas.push(None);
let num = CrateNum::new(tcx.untracked().stable_crate_ids.read().len());
tcx.untracked().stable_crate_ids.write().insert(root.stable_crate_id(), num);
Ok(num)
}
})?;

self.metas.push(None);
Ok(num)
}

pub fn has_crate_data(&self, cnum: CrateNum) -> bool {
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,16 @@ impl<'tcx> TyCtxt<'tcx> {
feed
}

pub fn create_crate_num(self, stable_crate_id: StableCrateId) -> Result<CrateNum, CrateNum> {
if let Some(&existing) = self.untracked().stable_crate_ids.read().get(&stable_crate_id) {
return Err(existing);
}

let num = CrateNum::new(self.untracked().stable_crate_ids.read().len());
self.untracked().stable_crate_ids.write().insert(stable_crate_id, num);
Ok(num)
}

pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
// Create a dependency to the red node to be sure we re-execute this when the amount of
// definitions change.
Expand Down

0 comments on commit 0025c9c

Please sign in to comment.