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

Check whether the closure's owner is an ADT in thir-unsafeck #86138

Merged
merged 1 commit into from
Jun 24, 2021

Conversation

FabianWolff
Copy link
Contributor

This pull request fixes #85871. The code in rustc_mir_build/src/check_unsafety.rs incorrectly assumes that a closure's owner always has a body, but only functions, closures, and constants have bodies, whereas a closure can also appear inside a struct or enum:

struct S {
    arr: [(); match || 1 { _ => 42 }]
}

enum E {
    A([(); { || 1; 42 }])
}

This pull request fixes the resulting ICE by checking whether the closure's owner is an ADT and only deferring to thir_check_unsafety(owner) if it isn't.

@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2021
@petrochenkov
Copy link
Contributor

r? @nikomatsakis

// ...unless they occur inside an ADT definition, e.g. in the
// length part of an array (issue #85871)
let owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
if !tcx.type_of(owner).is_adt() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if this is adequate. For example, what about type Foo = [...; || ...];?

We probably want to identify the cases where we should check the owner instead and enumerate those.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, .is_adt() is not comprehensive enough (although it is not a regression from the current behavior either).

I have used .maybe_body_owned_by(...).is_some() now, which should be more robust, and expanded the test case; could you have another look?

@nikomatsakis
Copy link
Contributor

The concept of body owner is a bit confusing, but this does seem right. Nice.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jun 23, 2021

📌 Commit 433c1ae has been approved by nikomatsakis

@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 Jun 23, 2021
@bors
Copy link
Contributor

bors commented Jun 23, 2021

⌛ Testing commit 433c1ae with merge f1e691d...

@bors
Copy link
Contributor

bors commented Jun 24, 2021

☀️ Test successful - checks-actions
Approved by: nikomatsakis
Pushing f1e691d to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 24, 2021
@bors bors merged commit f1e691d into rust-lang:master Jun 24, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 24, 2021
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.

ICE: struct has no associated body with -Zthir-unsafeck=yes
6 participants