-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
|
This comment has been minimized.
This comment has been minimized.
3a1b692
to
2ee5d8a
Compare
This comment has been minimized.
This comment has been minimized.
compiler/rustc_driver/src/pretty.rs
Outdated
@@ -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)) |
There was a problem hiding this comment.
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.
.maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id)) | |
.maybe_body_owned_by(expr.hir_id.owner) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the rename?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.and_then(|local_did| hir.maybe_body_owned_by(local_did)) | |
.and_then(|def_id| hir.maybe_body_owned_by(def_id)) |
compiler/rustc_ty_utils/src/ty.rs
Outdated
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); |
There was a problem hiding this comment.
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?
@@ -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)) { |
There was a problem hiding this comment.
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.
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()) { |
1df294e
to
33ec223
Compare
This comment has been minimized.
This comment has been minimized.
9b5e4cf
to
9c2fb9f
Compare
This comment has been minimized.
This comment has been minimized.
9c2fb9f
to
9b485f9
Compare
This comment has been minimized.
This comment has been minimized.
@@ -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(); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
☔ The latest upstream changes (presumably #99735) made this pull request unmergeable. Please resolve the merge conflicts. |
0816f5e
to
0cc1ba3
Compare
Some changes occurred in src/tools/cargo cc @ehuss The Miri submodule was changed cc @rust-lang/miri |
@rustbot label -S-waiting-on-author +S-waiting-on-review |
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
0cc1ba3
to
94611b8
Compare
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
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
Issue #96341
r? @cjgillot