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

rustc: use LocalDefId instead of DefIndex where possible. #66131

Merged
merged 9 commits into from
Mar 19, 2020

Conversation

eddyb
Copy link
Member

@eddyb eddyb commented Nov 5, 2019

That is, wherever DefIndex always referred to a "def" in the local crate, I replaced it with LocalDefId.
While LocalDefId already existed, it wasn't used a lot, but I hope I'm on the right track.

Unresolved questions:

  • should LocalDefId implement rustc_index::Idx?
    • this would get rid of a couple more DefIndex uses
  • should LocalDefId be encoded/decoded as just a DefIndex?
    • right now it's a bit messy, LocalDefId encodes/decodes like DefId
  • should DefId::assert_local be named something else, like expect_local?

A future PR should change tcx.hir().local_def_id(...) to return LocalDefId instead of DefId, as changing it in this PR would be too noisy.

r? @michaelwoerister cc @nikomatsakis @petrochenkov @Zoxc

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 5, 2019
src/librustc/hir/def_id.rs Outdated Show resolved Hide resolved
@petrochenkov petrochenkov self-assigned this Nov 5, 2019
@bors

This comment has been minimized.

@petrochenkov
Copy link
Contributor

Is enforcing the invariant through type system the end goal here, or this is a part of some larger plan?
(Seems like a good idea regardless.)

@petrochenkov
Copy link
Contributor

should DefId::assert_local be named something else, like expect_local?

expect_local looks better to me.

@petrochenkov petrochenkov removed their assignment Nov 6, 2019
@eddyb
Copy link
Member Author

eddyb commented Nov 6, 2019

Is enforcing the invariant through type system the end goal here, or this is a part of some larger plan?

I don't remember very well why I started this precisely, but I believe HirId's owner field, and related logic, was a significant part of it.

Probably the immediate next step would be to make {Impl,Trait}ItemId and ItemId use LocalDefId instead of HirId, and maybe even replace them altogether.
I'm experimenting with expanding the set of "HIR ID owners" from "just item-likes" towards "anything with a DefId", but the relevant code has been fighting me, hence switching to making PRs like this one.

I should mention that other than the invariant, LocalDefId could also be a performance boost (including potential uses for optimizing certain maps into flat arrays etc.).

Speaking of which:

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Nov 6, 2019

⌛ Trying commit f1a113e with merge 5e1cb0a...

bors added a commit that referenced this pull request Nov 6, 2019
rustc: use LocalDefId instead of DefIndex where possible.

That is, wherever `DefIndex` always referred to a "def" in the local crate, I replaced it with `LocalDefId`.
While `LocalDefId` already existed, it wasn't used a lot, but I hope I'm on the right track.

Unresolved questions:
* should `LocalDefId` implement `rustc_index::Idx`?
  * this would get rid of a couple more `DefIndex` uses
* should `LocalDefId` be encoded/decoded as just a `DefIndex`?
  * right now it's a bit messy, `LocalDefId` encodes/decodes like `DefId`
* should `DefId::assert_local` be named something else, like `expect_local`?
* what do we do with `tcx.hir().local_def_id(...)`?
  * right now it returns `DefId`, but changing it in this PR would be noisy
  * ideally it would return `LocalDefId` to spare the caller of `.assert_local()`

r? @michaelwoerister cc @nikomatsakis @petrochenkov @Zoxc
@bors
Copy link
Contributor

bors commented Nov 6, 2019

☀️ Try build successful - checks-azure
Build commit: 5e1cb0a (5e1cb0a8eaabbb729bdab2fd2e8af0c616f9d34b)

@eddyb
Copy link
Member Author

eddyb commented Nov 7, 2019

@rust-timer build 5e1cb0a

@rust-timer
Copy link
Collaborator

Queued 5e1cb0a with parent 3f0e164, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit 5e1cb0a, comparison URL.

@bors
Copy link
Contributor

bors commented Nov 8, 2019

☔ The latest upstream changes (presumably #66225) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@michaelwoerister michaelwoerister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thanks, @eddyb.

I left a few comments.

Regarding your questions:

should LocalDefId implement rustc_index::Idx?

Yes, I think that would be a good idea.

should LocalDefId be encoded/decoded as just a DefIndex?

In the incr. comp. cache we need to encode in the form of a DefPathHash for remapping. I'm not sure about crate metadata. Encoding as a DefId might make the code more consistent? How many cases are there where we encode a LocalDefId in crate metadata?

should DefId::assert_local be named something else, like expect_local?

As mentioned below, I also like expect_local more than assert_local.

what do we do with tcx.hir().local_def_id(...)?

Yeah, I'd also like this to return a LocalDefId. If it's too much for this PR, I'm fine with doing it later (maybe leave a FIXME, just in case).

src/librustc/hir/def_id.rs Outdated Show resolved Hide resolved
src/librustc/hir/def_id.rs Outdated Show resolved Hide resolved
@@ -1272,7 +1272,7 @@ impl LoweringContext<'_> {
AnonymousLifetimeMode::PassThrough,
|this, idty| this.lower_fn_decl(
&sig.decl,
Some((fn_def_id, idty)),
Some((fn_def_id.to_def_id(), idty)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to wonder if this method should be called to_global_def_id...

src/librustc/hir/map/definitions.rs Show resolved Hide resolved
src/librustc/hir/mod.rs Outdated Show resolved Hide resolved
src/librustc/ty/query/keys.rs Show resolved Hide resolved
src/librustc/ty/query/on_disk_cache.rs Outdated Show resolved Hide resolved
@JohnCSimon JohnCSimon 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 Nov 24, 2019
@JohnCSimon

This comment has been minimized.

@bors

This comment has been minimized.

@JohnCSimon

This comment has been minimized.

@joelpalmer joelpalmer added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 6, 2020
@joelpalmer

This comment has been minimized.

@bors

This comment has been minimized.

@eddyb
Copy link
Member Author

eddyb commented Mar 19, 2020

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Mar 19, 2020

📌 Commit 16e25f0 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 Mar 19, 2020
@bors
Copy link
Contributor

bors commented Mar 19, 2020

⌛ Testing commit 16e25f0 with merge 3c6f982...

@bors
Copy link
Contributor

bors commented Mar 19, 2020

☀️ Test successful - checks-azure
Approved by: petrochenkov
Pushing 3c6f982 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 19, 2020
@bors bors merged commit 3c6f982 into rust-lang:master Mar 19, 2020
@eddyb eddyb deleted the local-def-id branch March 19, 2020 12:25
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Mar 19, 2020
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Mar 19, 2020
bors added a commit to rust-lang/rust-clippy that referenced this pull request Mar 19, 2020
Rollup of 4 pull requests

Successful merges:

 - #5326 (rustup rust-lang/rust#69838)
 - #5333 (rustup rust-lang/rust#69189)
 - #5336 (rustup rust-lang/rust#69920)
 - #5341 (Rustup to rust-lang/rust#66131)

Failed merges:

r? @ghost
bors added a commit to rust-lang/rust-clippy that referenced this pull request Mar 19, 2020
Rollup of 4 pull requests

Successful merges:

 - #5326 (rustup rust-lang/rust#69838)
 - #5333 (rustup rust-lang/rust#69189)
 - #5336 (rustup rust-lang/rust#69920)
 - #5341 (Rustup to rust-lang/rust#66131)

Failed merges:

r? @ghost

changelog: none
bors added a commit to rust-lang/rust-clippy that referenced this pull request Mar 19, 2020
Rollup of 4 pull requests

Successful merges:

 - #5326 (rustup rust-lang/rust#69838)
 - #5333 (rustup rust-lang/rust#69189)
 - #5336 (rustup rust-lang/rust#69920)
 - #5341 (Rustup to rust-lang/rust#66131)

Failed merges:

r? @ghost

changelog: none
bors added a commit to rust-lang/rust-clippy that referenced this pull request Mar 19, 2020
Rollup of 4 pull requests

Successful merges:

 - #5326 (rustup rust-lang/rust#69838)
 - #5333 (rustup rust-lang/rust#69189)
 - #5336 (rustup rust-lang/rust#69920)
 - #5341 (Rustup to rust-lang/rust#66131)

Failed merges:

r? @ghost

changelog: none
bors added a commit to rust-lang/rust-clippy that referenced this pull request Mar 19, 2020
Rollup of 4 pull requests

Successful merges:

 - #5326 (rustup rust-lang/rust#69838)
 - #5333 (rustup rust-lang/rust#69189)
 - #5336 (rustup rust-lang/rust#69920)
 - #5341 (Rustup to rust-lang/rust#66131)

Failed merges:

r? @ghost

changelog: none
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 20, 2020
Fix oudated comment for NamedRegionMap

`ResolveLifetimes` uses a `LocalDefId` since rust-lang#66131.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 21, 2020
Fix oudated comment for NamedRegionMap

`ResolveLifetimes` uses a `LocalDefId` since rust-lang#66131.
Centril added a commit to Centril/rust that referenced this pull request Mar 21, 2020
Fix oudated comment for NamedRegionMap

`ResolveLifetimes` uses a `LocalDefId` since rust-lang#66131.
Centril added a commit to Centril/rust that referenced this pull request Mar 21, 2020
Fix oudated comment for NamedRegionMap

`ResolveLifetimes` uses a `LocalDefId` since rust-lang#66131.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants