Skip to content

Commit 3500e76

Browse files
authored
Rollup merge of #85889 - denismerigoux:master, r=petrochenkov
Restoring the `num_def_ids` function in the CStore API ## The context I am the maintainer of https://github.com/hacspec/hacspec, an embedded Rust DSL aimed at cryptographic specifications. As it is normal for an embedded DSL, Hacspec's compiler relies on being plugged to the internal API of the Rust compiler, which is unstable and subject to changes. ## The problem The Hacspec compiler features its own typechecker, that performs an additional, more restrictive typechecking pass over the Rust code of a crate. To complete this typechecking, the Hacspec compiler needs to retrieve the signature of functions defined in non-local imported crates. Rather than retrieving these signatures on-demand, the Hacspec compiler pre-populates its typechecking context with all the Hacspec-compatible symbols defined in non-local crates first. This requires having a way to iterate over all the definitions in a non-local crate. I used to do this with `CrateMetadata::all_def_path_hashes_and_def_ids`, but this function was deleted in 908bf5a. Then, I fellback on `CStore::num_def_ids`, exploiting the fact that all the `DefIds` for a crate have the same `krate_num` and range from `0` to `num_def_ids(krate_num)`. But `num_def_ids` was deleted in b6120bf. I looked to the `Cstore::item_children_untracked` function to replicate the feature of traversing through all the `DefId` for a crate, using `CRATE_DEF_INDEX` as the root, but this does not work as recursive `Cstore::item_children_untracked` calls do not reach all the symbols I was able to reach using the two previous methods. ## Description of this PR This PR simply restores in the public API of `CStore` the `num_def_ids` function, giving the size of the definition table for a given crate.
2 parents f6aaf05 + d4ea9fa commit 3500e76

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,10 @@ impl CrateMetadata {
19311931
self.root.hash
19321932
}
19331933

1934+
fn num_def_ids(&self) -> usize {
1935+
self.root.tables.def_keys.size()
1936+
}
1937+
19341938
fn local_def_id(&self, index: DefIndex) -> DefId {
19351939
DefId { krate: self.cnum, index }
19361940
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ impl CStore {
458458
self.get_crate_data(def_id.krate).module_expansion(def_id.index, sess)
459459
}
460460

461+
/// Only public-facing way to traverse all the definitions in a non-local crate.
462+
/// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
463+
/// See <https://github.com/rust-lang/rust/pull/85889> for context.
464+
pub fn num_def_ids_untracked(&self, cnum: CrateNum) -> usize {
465+
self.get_crate_data(cnum).num_def_ids()
466+
}
467+
461468
pub fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> {
462469
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess).collect()
463470
}

0 commit comments

Comments
 (0)