Skip to content

Commit

Permalink
Rollup merge of #85889 - denismerigoux:master, r=petrochenkov
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnTitor committed Jun 4, 2021
2 parents f6aaf05 + d4ea9fa commit 3500e76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,10 @@ impl CrateMetadata {
self.root.hash
}

fn num_def_ids(&self) -> usize {
self.root.tables.def_keys.size()
}

fn local_def_id(&self, index: DefIndex) -> DefId {
DefId { krate: self.cnum, index }
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ impl CStore {
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> {
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess).collect()
}
Expand Down

0 comments on commit 3500e76

Please sign in to comment.