Skip to content

Use type based qualification for unions #90373

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

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ where
if Q::in_adt_inherently(cx, def, substs) {
return true;
}
if def.is_union() && Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is correct, but conservative for things like drop, which unions must directly implement, it's never invoked on their fields. Will this get us in trouble wit ManuallyDrop in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the concern here that we will look inside ManuallyDrop and qualify value as needing drop based on the inner content? That seems like a possible issue, but more for already existing implementation, not for the new one?

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm good point. Ok. I'm convinced about this PR XD

return true;
}
}

// Otherwise, proceed structurally...
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_const_eval/src/transform/check_consts/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ where
}
}

fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, value: bool) {
fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, mut value: bool) {
debug_assert!(!place.is_indirect());

if !value {
for (base, _elem) in place.iter_projections() {
let base_ty = base.ty(self.ccx.body, self.ccx.tcx);
if base_ty.ty.is_union() && Q::in_any_value_of_ty(self.ccx, base_ty.ty) {
value = true;
break;
}
}
}

match (value, place.as_ref()) {
(true, mir::PlaceRef { local, .. }) => {
self.qualifs_per_local.insert(local);
Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/consts/qualif-union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Checks that unions use type based qualification. Regression test for issue #90268.
#![feature(untagged_unions)]
use std::cell::Cell;

union U { i: u32, c: Cell<u32> }

const C1: Cell<u32> = {
unsafe { U { c: Cell::new(0) }.c }
};

const C2: Cell<u32> = {
unsafe { U { i : 0 }.c }
};

const C3: Cell<u32> = {
let mut u = U { i: 0 };
u.i = 1;
unsafe { u.c }
};

const C4: U = U { i: 0 };

const C5: [U; 1] = [U {i : 0}; 1];

fn main() {
// Interior mutability should prevent promotion.
let _: &'static _ = &C1; //~ ERROR temporary value dropped while borrowed
let _: &'static _ = &C2; //~ ERROR temporary value dropped while borrowed
let _: &'static _ = &C3; //~ ERROR temporary value dropped while borrowed
let _: &'static _ = &C4; //~ ERROR temporary value dropped while borrowed
let _: &'static _ = &C5; //~ ERROR temporary value dropped while borrowed
}
57 changes: 57 additions & 0 deletions src/test/ui/consts/qualif-union.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/qualif-union.rs:27:26
|
LL | let _: &'static _ = &C1;
| ---------- ^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | }
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/qualif-union.rs:28:26
|
LL | let _: &'static _ = &C2;
| ---------- ^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | }
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/qualif-union.rs:29:26
|
LL | let _: &'static _ = &C3;
| ---------- ^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | }
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/qualif-union.rs:30:26
|
LL | let _: &'static _ = &C4;
| ---------- ^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
LL | let _: &'static _ = &C5;
LL | }
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/qualif-union.rs:31:26
|
LL | let _: &'static _ = &C5;
| ---------- ^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0716`.