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 908bf5a310. 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 b6120bfb35.

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.
This commit is contained in:
Yuki Okushi 2021-06-04 13:42:56 +09:00 committed by GitHub
commit 3500e76330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -1931,6 +1931,10 @@ impl CrateMetadata {
self.root.hash self.root.hash
} }
fn num_def_ids(&self) -> usize {
self.root.tables.def_keys.size()
}
fn local_def_id(&self, index: DefIndex) -> DefId { fn local_def_id(&self, index: DefIndex) -> DefId {
DefId { krate: self.cnum, index } DefId { krate: self.cnum, index }
} }

View File

@ -458,6 +458,13 @@ pub fn module_expansion_untracked(&self, def_id: DefId, sess: &Session) -> ExpnI
self.get_crate_data(def_id.krate).module_expansion(def_id.index, sess) self.get_crate_data(def_id.krate).module_expansion(def_id.index, sess)
} }
/// Only public-facing way to traverse all the definitions in a non-local crate.
/// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
/// See <https://github.com/rust-lang/rust/pull/85889> for context.
pub fn num_def_ids_untracked(&self, cnum: CrateNum) -> usize {
self.get_crate_data(cnum).num_def_ids()
}
pub fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> { pub fn item_attrs(&self, def_id: DefId, sess: &Session) -> Vec<ast::Attribute> {
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess).collect() self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess).collect()
} }