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

change maybe_body_owned_by to take local def id #99311

Merged
merged 4 commits into from
Jul 30, 2022

Conversation

kckeiks
Copy link
Contributor

@kckeiks kckeiks commented Jul 16, 2022

Issue #96341
r? @cjgillot

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 16, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2022

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 16, 2022
@kckeiks
Copy link
Contributor Author

kckeiks commented Jul 16, 2022

@cjgillot

  1. Could you please clarify how one should approach converting a HirId to LocalDefId? Should we check that a conversion can be made before calling tcx.hir().local_def_id?
  2. Can every HirId be converted to LocalDefId? Why or why not?

@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch 2 times, most recently from 3a1b692 to 2ee5d8a Compare July 16, 2022 16:28
@rust-log-analyzer

This comment has been minimized.

@@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
self.tcx
.hir()
.maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
.maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id))
Copy link
Contributor

Choose a reason for hiding this comment

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

local_def_id(local_def_id_to_hir_id(def_id)) is def_id by definition.

Suggested change
.maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id))
.maybe_body_owned_by(expr.hir_id.owner)

Copy link
Contributor Author

@kckeiks kckeiks Jul 19, 2022

Choose a reason for hiding this comment

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

Sorry, I was using expr.hir_id.owner initially but I was unsure because of the different ways there are to convert HirId to LocalDefId like .owner, local_def_id or {expect | as }_owner.

let def_id = self.tcx.hir().local_def_id(id);
debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
let body_id = self.tcx.hir().body_owned_by(id);
let local_did = self.tcx.hir().local_def_id(id);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the rename?

Copy link
Contributor Author

@kckeiks kckeiks Jul 19, 2022

Choose a reason for hiding this comment

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

I keep seeing local_did, did and def_id used for LocalDefId so I changed it to keep it consistent with local_did.

@@ -398,7 +398,7 @@ impl<'hir> Map<'hir> {

pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId {
for (parent, _) in self.parent_iter(hir_id) {
if let Some(body) = self.maybe_body_owned_by(parent) {
if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Inlining the former implementation of maybe_body_owned_by here is probably a better solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change fixed all broken tests. What is logically wrong with this condition let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did)?

@@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span};
pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
let body_id = match def_id.as_local() {
None => return,
Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)),
Some(did) => tcx.hir().body_owned_by(did),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Some(did) => tcx.hir().body_owned_by(did),
Some(def_id) => tcx.hir().body_owned_by(def_id),

@@ -1761,8 +1761,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {

let generator_body = generator_did
.as_local()
.map(|def_id| hir.local_def_id_to_hir_id(def_id))
.and_then(|hir_id| hir.maybe_body_owned_by(hir_id))
.and_then(|local_did| hir.maybe_body_owned_by(local_did))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.and_then(|local_did| hir.maybe_body_owned_by(local_did))
.and_then(|def_id| hir.maybe_body_owned_by(def_id))

let body_id = local_did
.and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id))
.or(hir_id)
.map_or(hir::CRATE_HIR_ID, |did| did);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you simplify the combinators here?

compiler/rustc_typeck/src/check/expr.rs Outdated Show resolved Hide resolved
@@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {

fn check_item(cx: &LateContext<'_>, hir_id: HirId) {
let hir = cx.tcx.hir();
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
if let Some(body_id) = hir.maybe_body_owned_by(hir.local_def_id(hir_id)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is only called for HIR owners.

Suggested change
if let Some(body_id) = hir.maybe_body_owned_by(hir.local_def_id(hir_id)) {
if let Some(body_id) = hir.maybe_body_owned_by(hir_id.expect_owner()) {

@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch 2 times, most recently from 1df294e to 33ec223 Compare July 19, 2022 21:14
@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch from 9b5e4cf to 9c2fb9f Compare July 19, 2022 22:01
@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch from 9c2fb9f to 9b485f9 Compare July 19, 2022 22:29
@rust-log-analyzer

This comment has been minimized.

@kckeiks kckeiks marked this pull request as ready for review July 20, 2022 02:52
@@ -12,7 +13,7 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, count_recv: &hi
if_chain! {
if is_trait_method(cx, count_recv, sym::Iterator);
let closure = expr_or_init(cx, map_arg);
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(closure.hir_id);
if let Some(body_id) = cx.tcx.hir().find(closure.hir_id).map(map::associated_body).flatten();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjgillot I initially used closure.hir_id.owner but it broke the test for this function so on a whim I used .find(closure.hir_id).map(map::associated_body).flatten() and it worked. Why didn't maybe_body_owned_by(LocalDefId) worked but using the Node works? It looks like maybe_body_owned_by also uses find to get the Node.

Copy link
Contributor

Choose a reason for hiding this comment

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

For closures, hir_id.owner and local_def_id(hir_id) point to different things. The former points to the enclosing function, the latter to the closure.
Using local_def_id(closure.hir_id) should work.
Is it enough to make associated_body private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using local_def_id(closure.hir_id) does not work. Here is the error:

+error: internal compiler error: /home/fausto/Repo/rust/compiler/rustc_middle/src/hir/map/mod.rs:195:13: local_def_id: no entry for `HirId { owner: DefId(0:6 ~ suspicious_map[53a1]::negative), local_id: 79 }`, which has a map of `Some(Expr(Expr { hir_id: HirId { owner: DefId(0:6 ~ suspicious_map[53a1]::negative), local_id: 79 }, kind: Path(Resolved(None, Path { span: $DIR/suspicious_map.rs:27:24: 27:36 (#0), res: Def(Fn, DefId(0:10 ~ suspicious_map[53a1]::do_something)), segments: [PathSegment { ident: do_something#0, hir_id: Some(HirId { owner: DefId(0:6 ~ suspicious_map[53a1]::negative), local_id: 78 }), res: Some(Def(Fn, DefId(0:10 ~ suspicious_map[53a1]::do_something))), args: None, infer_args: true }] })), span: $DIR/suspicious_map.rs:27:24: 27:36 (#0) }))`
+
+thread 'rustc' panicked at 'Box<dyn Any>', /home/fausto/Repo/rust/compiler/rustc_errors/src/lib.rs:1392:9
+note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
+
+note: the compiler unexpectedly panicked. this is a bug.
+
+note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
+
+note: Clippy version: clippy 0.1.64 (9b485f92000 2022-07-19)
+
+query stack during panic:
+#0 [analysis] running analysis passes on this crate
+end of query stack
+error: aborting due to 3 previous errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aside from this, I think all tasks are done. Let me know if I missed something.

Copy link
Contributor

Choose a reason for hiding this comment

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

As the code does not check whether this is a closure beforehand. You can use tcx.hir().opt_local_def_id which won't ICE on failure, and just return None.

@cjgillot cjgillot 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 Jul 24, 2022
@bors
Copy link
Contributor

bors commented Jul 26, 2022

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

@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch from 0816f5e to 0cc1ba3 Compare July 29, 2022 20:42
@rustbot
Copy link
Collaborator

rustbot commented Jul 29, 2022

Some changes occurred in src/tools/cargo

cc @ehuss

The Miri submodule was changed

cc @rust-lang/miri

@kckeiks
Copy link
Contributor Author

kckeiks commented Jul 29, 2022

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot 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 Jul 29, 2022
@rust-log-analyzer

This comment has been minimized.

kckeiks added 2 commits July 29, 2022 18:25
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
kckeiks added 2 commits July 29, 2022 18:26
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
@kckeiks kckeiks force-pushed the clean-up-body-owner-methods branch from 0cc1ba3 to 94611b8 Compare July 29, 2022 22:27
@cjgillot
Copy link
Contributor

Thanks @kckeiks!
@bors r+

@bors
Copy link
Contributor

bors commented Jul 30, 2022

📌 Commit 94611b8 has been approved by cjgillot

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 30, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#99311 (change maybe_body_owned_by to take local def id)
 - rust-lang#99862 (Improve type mismatch w/ function signatures)
 - rust-lang#99895 (don't call type ascription "cast")
 - rust-lang#99900 (remove some manual hash stable impls)
 - rust-lang#99903 (Add diagnostic when using public instead of pub)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c668820 into rust-lang:master Jul 30, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 30, 2022
flip1995 pushed a commit to flip1995/rust that referenced this pull request Aug 11, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#99311 (change maybe_body_owned_by to take local def id)
 - rust-lang#99862 (Improve type mismatch w/ function signatures)
 - rust-lang#99895 (don't call type ascription "cast")
 - rust-lang#99900 (remove some manual hash stable impls)
 - rust-lang#99903 (Add diagnostic when using public instead of pub)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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.

6 participants