diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 5083b75174c..83fe39baf1f 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -168,25 +168,21 @@ impl CStore { tcx: TyCtxt<'tcx>, ) -> Result { 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 { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index d30a4ac6a9c..3832fecfee4 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1261,6 +1261,16 @@ impl<'tcx> TyCtxt<'tcx> { feed } + pub fn create_crate_num(self, stable_crate_id: StableCrateId) -> Result { + 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 + 'tcx { // Create a dependency to the red node to be sure we re-execute this when the amount of // definitions change.