Skip to content

Commit 99a44ed

Browse files
committed
remove a hack that seems to only benefit a few very special cases
1 parent bd837e8 commit 99a44ed

File tree

3 files changed

+37
-55
lines changed

3 files changed

+37
-55
lines changed

compiler/rustc_mir/src/transform/promote_consts.rs

+6-52
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::cast::CastTy;
2222
use rustc_middle::ty::subst::InternalSubsts;
2323
use rustc_middle::ty::{self, List, TyCtxt, TypeFoldable};
2424
use rustc_span::symbol::sym;
25-
use rustc_span::{Span, DUMMY_SP};
25+
use rustc_span::Span;
2626

2727
use rustc_index::vec::{Idx, IndexVec};
2828
use rustc_target::spec::abi::Abi;
@@ -326,41 +326,16 @@ impl<'tcx> Validator<'_, 'tcx> {
326326
if place.projection.contains(&ProjectionElem::Deref) {
327327
return Err(Unpromotable);
328328
}
329-
330-
let mut has_mut_interior =
331-
self.qualif_local::<qualifs::HasMutInterior>(place.local);
332-
// HACK(eddyb) this should compute the same thing as
333-
// `<HasMutInterior as Qualif>::in_projection` from
334-
// `check_consts::qualifs` but without recursion.
335-
if has_mut_interior {
336-
// This allows borrowing fields which don't have
337-
// `HasMutInterior`, from a type that does, e.g.:
338-
// `let _: &'static _ = &(Cell::new(1), 2).1;`
339-
let mut place_projection = &place.projection[..];
340-
// FIXME(eddyb) use a forward loop instead of a reverse one.
341-
while let &[ref proj_base @ .., elem] = place_projection {
342-
// FIXME(eddyb) this is probably excessive, with
343-
// the exception of `union` member accesses.
344-
let ty =
345-
Place::ty_from(place.local, proj_base, self.body, self.tcx)
346-
.projection_ty(self.tcx, elem)
347-
.ty;
348-
if ty.is_freeze(self.tcx.at(DUMMY_SP), self.param_env) {
349-
has_mut_interior = false;
350-
break;
351-
}
352-
353-
place_projection = proj_base;
354-
}
329+
if self.qualif_local::<qualifs::NeedsDrop>(place.local) {
330+
return Err(Unpromotable);
355331
}
356332

357333
// FIXME(eddyb) this duplicates part of `validate_rvalue`.
334+
let has_mut_interior =
335+
self.qualif_local::<qualifs::HasMutInterior>(place.local);
358336
if has_mut_interior {
359337
return Err(Unpromotable);
360338
}
361-
if self.qualif_local::<qualifs::NeedsDrop>(place.local) {
362-
return Err(Unpromotable);
363-
}
364339

365340
if let BorrowKind::Mut { .. } = kind {
366341
let ty = place.ty(self.body, self.tcx).ty;
@@ -692,28 +667,7 @@ impl<'tcx> Validator<'_, 'tcx> {
692667

693668
self.validate_place(place)?;
694669

695-
// HACK(eddyb) this should compute the same thing as
696-
// `<HasMutInterior as Qualif>::in_projection` from
697-
// `check_consts::qualifs` but without recursion.
698-
let mut has_mut_interior =
699-
self.qualif_local::<qualifs::HasMutInterior>(place.local);
700-
if has_mut_interior {
701-
let mut place_projection = place.projection;
702-
// FIXME(eddyb) use a forward loop instead of a reverse one.
703-
while let &[ref proj_base @ .., elem] = place_projection {
704-
// FIXME(eddyb) this is probably excessive, with
705-
// the exception of `union` member accesses.
706-
let ty = Place::ty_from(place.local, proj_base, self.body, self.tcx)
707-
.projection_ty(self.tcx, elem)
708-
.ty;
709-
if ty.is_freeze(self.tcx.at(DUMMY_SP), self.param_env) {
710-
has_mut_interior = false;
711-
break;
712-
}
713-
714-
place_projection = proj_base;
715-
}
716-
}
670+
let has_mut_interior = self.qualif_local::<qualifs::HasMutInterior>(place.local);
717671
if has_mut_interior {
718672
return Err(Unpromotable);
719673
}

src/test/ui/consts/promote-not.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ const TEST_UNION: () = {
3434
let _x: &'static i32 = &unsafe { U { x: 0 }.x }; //~ ERROR temporary value dropped while borrowed
3535
};
3636

37+
// In a `const`, we do not promote things with interior mutability. Not even if we "project it away".
38+
const TEST_INTERIOR_MUT: () = {
39+
// The "0." case is already ruled out by not permitting any interior mutability in `const`.
40+
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
41+
};
42+
3743
fn main() {
38-
// We must not promote things with interior mutability.
44+
// We must not promote things with interior mutability. Not even if we "project it away".
3945
let _val: &'static _ = &(Cell::new(1), 2).0; //~ ERROR temporary value dropped while borrowed
46+
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
4047
}

src/test/ui/consts/promote-not.stderr

+23-2
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,36 @@ LL | };
4949
| - temporary value is freed at the end of this statement
5050

5151
error[E0716]: temporary value dropped while borrowed
52-
--> $DIR/promote-not.rs:39:29
52+
--> $DIR/promote-not.rs:40:29
53+
|
54+
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
55+
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
56+
| |
57+
| type annotation requires that borrow lasts for `'static`
58+
LL | };
59+
| - temporary value is freed at the end of this statement
60+
61+
error[E0716]: temporary value dropped while borrowed
62+
--> $DIR/promote-not.rs:45:29
5363
|
5464
LL | let _val: &'static _ = &(Cell::new(1), 2).0;
5565
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
5666
| |
5767
| type annotation requires that borrow lasts for `'static`
68+
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
69+
LL | }
70+
| - temporary value is freed at the end of this statement
71+
72+
error[E0716]: temporary value dropped while borrowed
73+
--> $DIR/promote-not.rs:46:29
74+
|
75+
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
76+
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
77+
| |
78+
| type annotation requires that borrow lasts for `'static`
5879
LL | }
5980
| - temporary value is freed at the end of this statement
6081

61-
error: aborting due to 6 previous errors
82+
error: aborting due to 8 previous errors
6283

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

0 commit comments

Comments
 (0)