Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restoring the num_def_ids function in the CStore API #85889

Merged
merged 1 commit into from
Jun 4, 2021

Conversation

denismerigoux
Copy link
Contributor

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.

@rust-highfive
Copy link
Collaborator

r? @estebank

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 1, 2021
@jyn514
Copy link
Member

jyn514 commented Jun 1, 2021

r? @petrochenkov

@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 1, 2021
@jyn514
Copy link
Member

jyn514 commented Jun 1, 2021

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.

Why does it do that?

@denismerigoux
Copy link
Contributor Author

Why does it do that?

It was a architectural choice I made one year ago when I started to dev on it. I could change it on my side, requiring a diff that I estimate at ~1000 lines of code or more and one month of work (at least). The balance between this and the 6 lines of code in the Rust compiler made me decide that I should at least try this PR :)

@petrochenkov
Copy link
Contributor

r=me after adding the comment (#85889 (comment)) and squashing commits.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 1, 2021
@jyn514 jyn514 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 2, 2021
@petrochenkov
Copy link
Contributor

@bors r+ rollup

@petrochenkov petrochenkov added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 2, 2021
@bors
Copy link
Contributor

bors commented Jun 2, 2021

📌 Commit 3844570 has been approved by petrochenkov

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Jun 3, 2021
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.
@JohnTitor
Copy link
Member

Failed in rollup: #85949 (comment)
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 3, 2021
…mber of exported symbols for each crate

Restored underlying num_def_ids_method

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

Changed name to fit with naming convention

Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>

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

Replace regular doc with Rustdoc comment

Co-authored-by: Joshua Nelson <jyn514@gmail.com>

Clarifies third-party use of num_def_ids_untracked
@denismerigoux
Copy link
Contributor Author

Sorry for the missing hyperlink markup. BTW as this is my first Rust compiler PR I'm REALLY impressed by the bors tooling, rollup system, error message finding, automatic PR labeling system, etc. Thanks so much to everyone involved for creating and running such a great org.

@bjorn3
Copy link
Member

bjorn3 commented Jun 3, 2021

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Jun 3, 2021

📌 Commit d4ea9fa has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 3, 2021
@jyn514
Copy link
Member

jyn514 commented Jun 3, 2021

@bors rollup

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 3, 2021
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.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 4, 2021
Rollup of 8 pull requests

Successful merges:

 - rust-lang#85717 (Document `From` impls for cow.rs)
 - rust-lang#85850 (Remove unused feature gates)
 - rust-lang#85888 (Fix typo in internal documentation for `TrustedRandomAccess`)
 - rust-lang#85889 (Restoring the `num_def_ids` function in the CStore API )
 - rust-lang#85899 (jsondocck small cleanup)
 - rust-lang#85937 (Fix bad suggestions for code from proc_macro)
 - rust-lang#85963 (Show `::{{constructor}}` in std::any::type_name().)
 - rust-lang#85977 (Fix linkcheck script from getting out of sync.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3500e76 into rust-lang:master Jun 4, 2021
@rustbot rustbot added this to the 1.54.0 milestone Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants