1
Fork 0

Isolate CrateNum creation to TyCtxt methods

This commit is contained in:
Oli Scherer 2024-03-26 16:59:14 +00:00
parent fbc9b94064
commit 0025c9cc50
2 changed files with 18 additions and 12 deletions

View file

@ -168,25 +168,21 @@ impl CStore {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
) -> Result<CrateNum, CrateError> { ) -> Result<CrateNum, CrateError> {
assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len()); assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
if let Some(&existing) = let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
tcx.untracked().stable_crate_ids.read().get(&root.stable_crate_id())
{
// Check for (potential) conflicts with the local crate // Check for (potential) conflicts with the local crate
if existing == 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()) } else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
{ {
let crate_name0 = root.name(); let crate_name0 = root.name();
Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1)) CrateError::StableCrateIdCollision(crate_name0, crate_name1)
} else { } 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()); self.metas.push(None);
tcx.untracked().stable_crate_ids.write().insert(root.stable_crate_id(), num); Ok(num)
Ok(num)
}
} }
pub fn has_crate_data(&self, cnum: CrateNum) -> bool { pub fn has_crate_data(&self, cnum: CrateNum) -> bool {

View file

@ -1261,6 +1261,16 @@ impl<'tcx> TyCtxt<'tcx> {
feed 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 { 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 // Create a dependency to the red node to be sure we re-execute this when the amount of
// definitions change. // definitions change.