Skip to content

Commit 0025c9c

Browse files
committed
Isolate CrateNum creation to TyCtxt methods
1 parent fbc9b94 commit 0025c9c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

compiler/rustc_metadata/src/creader.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,21 @@ impl CStore {
168168
tcx: TyCtxt<'tcx>,
169169
) -> Result<CrateNum, CrateError> {
170170
assert_eq!(self.metas.len(), tcx.untracked().stable_crate_ids.read().len());
171-
if let Some(&existing) =
172-
tcx.untracked().stable_crate_ids.read().get(&root.stable_crate_id())
173-
{
171+
let num = tcx.create_crate_num(root.stable_crate_id()).map_err(|existing| {
174172
// Check for (potential) conflicts with the local crate
175173
if existing == LOCAL_CRATE {
176-
Err(CrateError::SymbolConflictsCurrent(root.name()))
174+
CrateError::SymbolConflictsCurrent(root.name())
177175
} else if let Some(crate_name1) = self.metas[existing].as_ref().map(|data| data.name())
178176
{
179177
let crate_name0 = root.name();
180-
Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1))
178+
CrateError::StableCrateIdCollision(crate_name0, crate_name1)
181179
} else {
182-
Err(CrateError::NotFound(root.name()))
180+
CrateError::NotFound(root.name())
183181
}
184-
} else {
185-
self.metas.push(None);
186-
let num = CrateNum::new(tcx.untracked().stable_crate_ids.read().len());
187-
tcx.untracked().stable_crate_ids.write().insert(root.stable_crate_id(), num);
188-
Ok(num)
189-
}
182+
})?;
183+
184+
self.metas.push(None);
185+
Ok(num)
190186
}
191187

192188
pub fn has_crate_data(&self, cnum: CrateNum) -> bool {

compiler/rustc_middle/src/ty/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,16 @@ impl<'tcx> TyCtxt<'tcx> {
12611261
feed
12621262
}
12631263

1264+
pub fn create_crate_num(self, stable_crate_id: StableCrateId) -> Result<CrateNum, CrateNum> {
1265+
if let Some(&existing) = self.untracked().stable_crate_ids.read().get(&stable_crate_id) {
1266+
return Err(existing);
1267+
}
1268+
1269+
let num = CrateNum::new(self.untracked().stable_crate_ids.read().len());
1270+
self.untracked().stable_crate_ids.write().insert(stable_crate_id, num);
1271+
Ok(num)
1272+
}
1273+
12641274
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
12651275
// Create a dependency to the red node to be sure we re-execute this when the amount of
12661276
// definitions change.

0 commit comments

Comments
 (0)