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

Replace DefId with LocalDefId where possible #70853

Closed
ecstatic-morse opened this issue Apr 6, 2020 · 7 comments
Closed

Replace DefId with LocalDefId where possible #70853

ecstatic-morse opened this issue Apr 6, 2020 · 7 comments
Assignees
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Apr 6, 2020

In the compiler, a DefId is used to lookup a single "definition" (type, method, const, etc.) somewhere in the code. It can refer to definitions in the crate currently being compiled or to definitions in other crates. There are quite a few places in the compiler which will only work if passed a local DefId--maybe they need to access the HIR for that definition, which is only available in the current crate--but accept DefId as a parameter. These places should use LocalDefId instead.

To resolve this issue, you need to find functions or methods that will panic if a DefId is not local. Such places should be calling DefId::expect_local and then working with the returned LocalDefId, but you are more likely to see older idioms (e.g., tcx.as_local_hir_id(def_id).unwrap()). Code like this should be refactored to take a LocalDefId instead of a DefId and their caller made responsible for asserting that a given DefId is local. The end goal is to move the call to expect_local as high up in the call graph as we can. If possible, it should be the first thing we do when executing a query like so,

is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),

Ideally this would be done module-by-module so it can be reviewed more easily (not in a single, giant PR). See the last commit in #66132 for prior art.

This issue has been assigned to @marmeladema via this comment.

@ecstatic-morse ecstatic-morse added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-help-wanted Call for participation: Help is requested to fix this issue. labels Apr 6, 2020
@lcnr
Copy link
Contributor

lcnr commented Apr 6, 2020

Is there a reason why the queries should keep using DefId instead of LocalDefId?

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Apr 6, 2020

@lcnr For many queries, the result is encoded into a crate's metadata so it can be looked up while compiling other crates. For these queries, only the initial computation of the result of needs to be done in the local crate. Cross-crate invocations will look up the cached result via metadata.

However, queries that are not encoded into a crate's metadata are only accessible in the local crate. Such queries should indeed take a LocalDefId instead of a DefId.

@marmeladema
Copy link
Contributor

I can try to take a look, its sounds easy enough and well constrained.

@Dylan-DPC-zz
Copy link

@rustbot assign @marmeladema

@rustbot rustbot self-assigned this Apr 7, 2020
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 9, 2020
…ocal-def-id, r=eddyb

librustc_hir: return LocalDefId instead of DefId in local_def_id

Its a first try to remove a few calls to `expect_local` and use `LocalDefId` instead of `DefId` where possible for rust-lang#70853

This adds some calls to `.to_def_id()` to get a `DefId` back when needed. I don't know if I should push `LocalDefId` even further and change, for example, `Res::Def` to accept a `LocalDefId` instead of a `DefId` as second argument.

cc @ecstatic-morse
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 10, 2020
…e-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 10, 2020
…dle-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 13, 2020
Take `impl Into<DefId>` for query methods on `TyCtxt`

Alternative implementation of rust-lang#70956. cc rust-lang#70853.
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 24, 2020
…e-local-def-id-2, r=eddyb

Simplify `local_def_id` and `as_local_hir_id`

See rust-lang#70853
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 27, 2020
…=eddyb

Convert more queries to use `LocalDefId`

This PR is based on commits in rust-lang#71215 and should partially solve rust-lang#70853
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 28, 2020
…ddyb

Convert more queries to use `LocalDefId`

This PR is based on commits in rust-lang#71215 and should partially solve rust-lang#70853
@marmeladema
Copy link
Contributor

@eddyb @ecstatic-morse the last PR i had has landed. Do you see any other places where LocalDefId should be used?

@eddyb
Copy link
Member

eddyb commented Apr 28, 2020

There are some places that use HirId but are always owners, I think, which would allow using LocalDefId instead, but that doesn't (directly) count.

There might still be DefId which could be LocalDefId in smaller (or shorter-lived, I guess) context types, but I wouldn't know where to start looking for them.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 27, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 28, 2020
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 28, 2020
@torhovland
Copy link
Contributor

Looks like this issue can be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants