Skip to content

Conversation

LeSeulArtichaut
Copy link
Contributor

Previously, when encountering a closure in a constant, the THIR unsafeck gets invoked on the owner of the constant instead of the constant itself, producing cycles.
Supersedes #87492. @FabianWolff thanks for your work on that PR, I copied your test file and added you as a co-author.

Fixes #87414.
r? @oli-obk

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 30, 2021
@@ -599,13 +599,10 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD

// Closures are handled by their owner, if it has a body
if tcx.is_closure(def.did.to_def_id()) {
let owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
Copy link
Contributor

Choose a reason for hiding this comment

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

just to be clear I understand, because I understood something different from your text. owner here is == def.did, and thus invoking thir_check_unsafety on that will obviously cycle?

Copy link
Contributor Author

@LeSeulArtichaut LeSeulArtichaut Jul 31, 2021

Choose a reason for hiding this comment

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

No, that's not the problem. Let me illustrate with an example:

fn function() {
    let closure = || {};
}

We're trying to compute unsafeck for closure, which should invoke unsafeck for the parent body function. So def.did is the DefId of the closure, and the HIR is constructed in such a way that the corresponding HirId has function as its owner, so in the code owner is the DefId of function which id correct.

Enter consts:

fn function() -> [(); { || {}; 4 }] {}

Here, if I try the same "hack", i.e. get the HirId for the closure and take its owner, I get owner to be the DefId of function whereas I want the DefId of the anonymous constant { || {}; 4 }.

To be fair I don't entirely understand which query invocations cycle and which don't when using ensure, and I've also not dived into the internals of HIR to check that enclosing_body_owner always does the right thing, but this PR doesn't cycle on the examples provided in #87414 and passed the rest of the test suite.

Copy link
Contributor

Choose a reason for hiding this comment

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

Your explanation makes sense, thanks! I do think we should get a more solid strategy around all this, but I think it kind of overlaps with various other things like generics in anonymous constants. So let's merge this PR now

@oli-obk
Copy link
Contributor

oli-obk commented Aug 2, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 2, 2021

📌 Commit 1280423 has been approved by oli-obk

@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 Aug 2, 2021
@cjgillot
Copy link
Contributor

cjgillot commented Aug 2, 2021

I confirm that this PR fixes the ICE I had in #87114, where I implemented essentially the same logic.

@cjgillot cjgillot removed their assignment Aug 2, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Aug 3, 2021
Properly find owner of closure in THIR unsafeck

Previously, when encountering a closure in a constant, the THIR unsafeck gets invoked on the owner of the constant instead of the constant itself, producing cycles.
Supersedes rust-lang#87492. `@FabianWolff` thanks for your work on that PR, I copied your test file and added you as a co-author.

Fixes rust-lang#87414.
r? `@oli-obk`
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Aug 3, 2021
Properly find owner of closure in THIR unsafeck

Previously, when encountering a closure in a constant, the THIR unsafeck gets invoked on the owner of the constant instead of the constant itself, producing cycles.
Supersedes rust-lang#87492. ``@FabianWolff`` thanks for your work on that PR, I copied your test file and added you as a co-author.

Fixes rust-lang#87414.
r? ``@oli-obk``
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 3, 2021
Rollup of 8 pull requests

Successful merges:

 - rust-lang#87645 (Properly find owner of closure in THIR unsafeck)
 - rust-lang#87646 (Fix a parser ICE on invalid `fn` body)
 - rust-lang#87652 (Validate that naked functions are never inlined)
 - rust-lang#87685 (Write docs for SyncOnceCell From and Default impl)
 - rust-lang#87693 (Add `aarch64-apple-ios-sim` as a possible target to the manifest)
 - rust-lang#87708 (Add convenience method for handling ipv4-mapped addresses by canonicalizing them)
 - rust-lang#87711 (Correct typo)
 - rust-lang#87716 (Allow generic SIMD array element type)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 345862d into rust-lang:master Aug 3, 2021
@rustbot rustbot added this to the 1.56.0 milestone Aug 3, 2021
@LeSeulArtichaut LeSeulArtichaut deleted the issue-87414 branch August 3, 2021 13:28
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

thir-unsafeck query loops on closures in const-generics
6 participants