Skip to content

Commit e8b5a61

Browse files
committed
Isolate CrateNum creation to TyCtxt methods
1 parent 4bd030e commit e8b5a61

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 8 additions & 12 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,16 @@ impl<'tcx> TyCtxt<'tcx> {
11731173
feed
11741174
}
11751175

1176+
pub fn create_crate_num(self, stable_crate_id: StableCrateId) -> Result<CrateNum, CrateNum> {
1177+
if let Some(&existing) = self.untracked().stable_crate_ids.read().get(&stable_crate_id) {
1178+
return Err(existing);
1179+
}
1180+
1181+
let num = CrateNum::new(self.untracked().stable_crate_ids.read().len());
1182+
self.untracked().stable_crate_ids.write().insert(stable_crate_id, num);
1183+
Ok(num)
1184+
}
1185+
11761186
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
11771187
// Create a dependency to the red node to be sure we re-execute this when the amount of
11781188
// definitions change.

0 commit comments

Comments
 (0)