Skip to content

Commit 90c2d64

Browse files
authored
Rollup merge of rust-lang#59232 - saleemjaffer:mir_place_refactor, r=oli-obk
Merge `Promoted` and `Static` in `mir::Place` fixes rust-lang#53848
2 parents b316514 + fb93f10 commit 90c2d64

26 files changed

+233
-210
lines changed

src/librustc/mir/mod.rs

+29-21
Original file line numberDiff line numberDiff line change
@@ -1913,22 +1913,24 @@ pub enum PlaceBase<'tcx> {
19131913

19141914
/// static or static mut variable
19151915
Static(Box<Static<'tcx>>),
1916-
1917-
/// Constant code promoted to an injected static
1918-
Promoted(Box<(Promoted, Ty<'tcx>)>),
19191916
}
19201917

1921-
/// The `DefId` of a static, along with its normalized type (which is
1922-
/// stored to avoid requiring normalization when reading MIR).
1918+
/// We store the normalized type to avoid requiring normalization when reading MIR
19231919
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
19241920
pub struct Static<'tcx> {
1925-
pub def_id: DefId,
19261921
pub ty: Ty<'tcx>,
1922+
pub kind: StaticKind,
1923+
}
1924+
1925+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable)]
1926+
pub enum StaticKind {
1927+
Promoted(Promoted),
1928+
Static(DefId),
19271929
}
19281930

19291931
impl_stable_hash_for!(struct Static<'tcx> {
1930-
def_id,
1931-
ty
1932+
ty,
1933+
kind
19321934
});
19331935

19341936
/// The `Projection` data structure defines things of the form `B.x`
@@ -2048,7 +2050,7 @@ impl<'tcx> Place<'tcx> {
20482050
match self {
20492051
Place::Base(PlaceBase::Local(local)) => Some(*local),
20502052
Place::Projection(box Projection { base, elem: _ }) => base.base_local(),
2051-
Place::Base(PlaceBase::Promoted(..)) | Place::Base(PlaceBase::Static(..)) => None,
2053+
Place::Base(PlaceBase::Static(..)) => None,
20522054
}
20532055
}
20542056
}
@@ -2059,18 +2061,24 @@ impl<'tcx> Debug for Place<'tcx> {
20592061

20602062
match *self {
20612063
Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id),
2062-
Base(PlaceBase::Static(box self::Static { def_id, ty })) => write!(
2063-
fmt,
2064-
"({}: {:?})",
2065-
ty::tls::with(|tcx| tcx.def_path_str(def_id)),
2066-
ty
2067-
),
2068-
Base(PlaceBase::Promoted(ref promoted)) => write!(
2069-
fmt,
2070-
"({:?}: {:?})",
2071-
promoted.0,
2072-
promoted.1
2073-
),
2064+
Base(PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) })) => {
2065+
write!(
2066+
fmt,
2067+
"({}: {:?})",
2068+
ty::tls::with(|tcx| tcx.def_path_str(def_id)),
2069+
ty
2070+
)
2071+
},
2072+
Base(PlaceBase::Static(
2073+
box self::Static { ty, kind: StaticKind::Promoted(promoted) })
2074+
) => {
2075+
write!(
2076+
fmt,
2077+
"({:?}: {:?})",
2078+
promoted,
2079+
ty
2080+
)
2081+
},
20742082
Projection(ref data) => match data.elem {
20752083
ProjectionElem::Downcast(ref adt_def, index) => {
20762084
write!(fmt, "({:?} as {})", data.base, adt_def.variants[index].ident)

src/librustc/mir/tcx.rs

-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ impl<'tcx> Place<'tcx> {
160160
match *self {
161161
Place::Base(PlaceBase::Local(index)) =>
162162
PlaceTy::Ty { ty: local_decls.local_decls()[index].ty },
163-
Place::Base(PlaceBase::Promoted(ref data)) => PlaceTy::Ty { ty: data.1 },
164163
Place::Base(PlaceBase::Static(ref data)) =>
165164
PlaceTy::Ty { ty: data.ty },
166165
Place::Projection(ref proj) =>

src/librustc/mir/visit.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,6 @@ macro_rules! make_mir_visitor {
156156
self.super_place(place, context, location);
157157
}
158158

159-
fn visit_static(&mut self,
160-
static_: & $($mutability)? Static<'tcx>,
161-
context: PlaceContext<'tcx>,
162-
location: Location) {
163-
self.super_static(static_, context, location);
164-
}
165-
166159
fn visit_projection(&mut self,
167160
place: & $($mutability)? PlaceProjection<'tcx>,
168161
context: PlaceContext<'tcx>,
@@ -736,27 +729,18 @@ macro_rules! make_mir_visitor {
736729
Place::Base(PlaceBase::Local(local)) => {
737730
self.visit_local(local, context, location);
738731
}
739-
Place::Base(PlaceBase::Static(static_)) => {
740-
self.visit_static(static_, context, location);
732+
Place::Base(PlaceBase::Static(box Static { kind, ty })) => {
733+
if let StaticKind::Static(def_id) = kind {
734+
self.visit_def_id(& $($mutability)? *def_id, location)
735+
}
736+
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
741737
}
742-
Place::Base(PlaceBase::Promoted(promoted)) => {
743-
self.visit_ty(& $($mutability)? promoted.1, TyContext::Location(location));
744-
},
745738
Place::Projection(proj) => {
746739
self.visit_projection(proj, context, location);
747740
}
748741
}
749742
}
750743

751-
fn super_static(&mut self,
752-
static_: & $($mutability)? Static<'tcx>,
753-
_context: PlaceContext<'tcx>,
754-
location: Location) {
755-
let Static { def_id, ty } = static_;
756-
self.visit_def_id(def_id, location);
757-
self.visit_ty(ty, TyContext::Location(location));
758-
}
759-
760744
fn super_projection(&mut self,
761745
proj: & $($mutability)? PlaceProjection<'tcx>,
762746
context: PlaceContext<'tcx>,

src/librustc_codegen_ssa/mir/block.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::middle::lang_items;
22
use rustc::ty::{self, Ty, TypeFoldable};
33
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
4-
use rustc::mir;
4+
use rustc::mir::{self, Place, PlaceBase, Static, StaticKind};
55
use rustc::mir::interpret::EvalErrorKind;
66
use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode};
77
use rustc_target::spec::abi::Abi;
@@ -621,15 +621,23 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
621621
// but specified directly in the code. This means it gets promoted
622622
// and we can then extract the value by evaluating the promoted.
623623
mir::Operand::Copy(
624-
mir::Place::Base(mir::PlaceBase::Promoted(box(index, ty)))
624+
Place::Base(
625+
PlaceBase::Static(
626+
box Static { kind: StaticKind::Promoted(promoted), ty }
627+
)
628+
)
625629
) |
626630
mir::Operand::Move(
627-
mir::Place::Base(mir::PlaceBase::Promoted(box(index, ty)))
631+
Place::Base(
632+
PlaceBase::Static(
633+
box Static { kind: StaticKind::Promoted(promoted), ty }
634+
)
635+
)
628636
) => {
629637
let param_env = ty::ParamEnv::reveal_all();
630638
let cid = mir::interpret::GlobalId {
631639
instance: self.instance,
632-
promoted: Some(index),
640+
promoted: Some(promoted),
633641
};
634642
let c = bx.tcx().const_eval(param_env.and(cid));
635643
let (llval, ty) = self.simd_shuffle_indices(

src/librustc_codegen_ssa/mir/place.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,15 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
408408

409409
let result = match *place {
410410
mir::Place::Base(mir::PlaceBase::Local(_)) => bug!(), // handled above
411-
mir::Place::Base(mir::PlaceBase::Promoted(box (index, ty))) => {
411+
mir::Place::Base(
412+
mir::PlaceBase::Static(
413+
box mir::Static { ty, kind: mir::StaticKind::Promoted(promoted) }
414+
)
415+
) => {
412416
let param_env = ty::ParamEnv::reveal_all();
413417
let cid = mir::interpret::GlobalId {
414418
instance: self.instance,
415-
promoted: Some(index),
419+
promoted: Some(promoted),
416420
};
417421
let layout = cx.layout_of(self.monomorphize(&ty));
418422
match bx.tcx().const_eval(param_env.and(cid)) {
@@ -435,7 +439,11 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
435439
}
436440
}
437441
}
438-
mir::Place::Base(mir::PlaceBase::Static(box mir::Static { def_id, ty })) => {
442+
mir::Place::Base(
443+
mir::PlaceBase::Static(
444+
box mir::Static { ty, kind: mir::StaticKind::Static(def_id) }
445+
)
446+
) => {
439447
// NB: The layout of a static may be unsized as is the case when working
440448
// with a static that is an extern_type.
441449
let layout = cx.layout_of(self.monomorphize(&ty));

src/librustc_mir/borrow_check/error_reporting.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc::mir::{
1010
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Constant,
1111
ConstraintCategory, Field, Local, LocalDecl, LocalKind, Location, Operand,
1212
Place, PlaceBase, PlaceProjection, ProjectionElem, Rvalue, Statement, StatementKind,
13-
TerminatorKind, VarBindingForm,
13+
Static, StaticKind, TerminatorKind, VarBindingForm,
1414
};
1515
use rustc::ty::{self, DefIdTree};
1616
use rustc::ty::print::Print;
@@ -1598,14 +1598,14 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15981598
including_downcast: &IncludingDowncast,
15991599
) -> Result<(), ()> {
16001600
match *place {
1601-
Place::Base(PlaceBase::Promoted(_)) => {
1602-
buf.push_str("promoted");
1603-
}
16041601
Place::Base(PlaceBase::Local(local)) => {
16051602
self.append_local_to_string(local, buf)?;
16061603
}
1607-
Place::Base(PlaceBase::Static(ref static_)) => {
1608-
buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string());
1604+
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
1605+
buf.push_str("promoted");
1606+
}
1607+
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
1608+
buf.push_str(&self.infcx.tcx.item_name(def_id).to_string());
16091609
}
16101610
Place::Projection(ref proj) => {
16111611
match proj.elem {
@@ -1744,8 +1744,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
17441744
let local = &self.mir.local_decls[local];
17451745
self.describe_field_from_ty(&local.ty, field)
17461746
}
1747-
Place::Base(PlaceBase::Promoted(ref prom)) =>
1748-
self.describe_field_from_ty(&prom.1, field),
17491747
Place::Base(PlaceBase::Static(ref static_)) =>
17501748
self.describe_field_from_ty(&static_.ty, field),
17511749
Place::Projection(ref proj) => match proj.elem {
@@ -1809,8 +1807,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18091807

18101808
/// Checks if a place is a thread-local static.
18111809
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
1812-
if let Place::Base(PlaceBase::Static(statik)) = place {
1813-
let attrs = self.infcx.tcx.get_attrs(statik.def_id);
1810+
if let Place::Base(
1811+
PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })
1812+
) = place {
1813+
let attrs = self.infcx.tcx.get_attrs(*def_id);
18141814
let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
18151815

18161816
debug!(
@@ -1828,8 +1828,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18281828
let tcx = self.infcx.tcx;
18291829
match place {
18301830
Place::Base(PlaceBase::Local(_)) |
1831-
Place::Base(PlaceBase::Static(_)) |
1832-
Place::Base(PlaceBase::Promoted(_)) => {
1831+
Place::Base(PlaceBase::Static(_)) => {
18331832
StorageDeadOrDrop::LocalStorageDead
18341833
}
18351834
Place::Projection(box PlaceProjection { base, elem }) => {

src/librustc_mir/borrow_check/mod.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc::infer::InferCtxt;
88
use rustc::lint::builtin::UNUSED_MUT;
99
use rustc::middle::borrowck::SignalledError;
1010
use rustc::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
11-
use rustc::mir::{ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Place, PlaceBase};
11+
use rustc::mir::{
12+
ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Place, PlaceBase, Static, StaticKind
13+
};
1214
use rustc::mir::{Field, Projection, ProjectionElem, Rvalue, Statement, StatementKind};
1315
use rustc::mir::{Terminator, TerminatorKind};
1416
use rustc::ty::query::Providers;
@@ -1226,8 +1228,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
12261228
}
12271229
Operand::Move(Place::Base(PlaceBase::Static(..)))
12281230
| Operand::Copy(Place::Base(PlaceBase::Static(..)))
1229-
| Operand::Move(Place::Base(PlaceBase::Promoted(..)))
1230-
| Operand::Copy(Place::Base(PlaceBase::Promoted(..)))
12311231
| Operand::Constant(..) => {}
12321232
}
12331233
}
@@ -1310,12 +1310,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13101310
//
13111311
// FIXME: allow thread-locals to borrow other thread locals?
13121312
let (might_be_alive, will_be_dropped) = match root_place {
1313-
Place::Base(PlaceBase::Promoted(_)) => (true, false),
1314-
Place::Base(PlaceBase::Static(_)) => {
1313+
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
1314+
(true, false)
1315+
}
1316+
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(_), .. })) => {
13151317
// Thread-locals might be dropped after the function exits, but
13161318
// "true" statics will never be.
1317-
let is_thread_local = self.is_place_thread_local(&root_place);
1318-
(true, is_thread_local)
1319+
(true, self.is_place_thread_local(&root_place))
13191320
}
13201321
Place::Base(PlaceBase::Local(_)) => {
13211322
// Locals are always dropped at function exit, and if they
@@ -1578,7 +1579,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
15781579
match *last_prefix {
15791580
Place::Base(PlaceBase::Local(_)) => panic!("should have move path for every Local"),
15801581
Place::Projection(_) => panic!("PrefixSet::All meant don't stop for Projection"),
1581-
Place::Base(PlaceBase::Promoted(_)) |
15821582
Place::Base(PlaceBase::Static(_)) => Err(NoMovePathFound::ReachedStatic),
15831583
}
15841584
}
@@ -1605,7 +1605,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16051605
let mut place = place;
16061606
loop {
16071607
match *place {
1608-
Place::Base(PlaceBase::Promoted(_)) |
16091608
Place::Base(PlaceBase::Local(_)) | Place::Base(PlaceBase::Static(_)) => {
16101609
// assigning to `x` does not require `x` be initialized.
16111610
break;
@@ -1953,10 +1952,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19531952
self.used_mut_upvars.push(field);
19541953
}
19551954
}
1956-
RootPlace {
1957-
place: Place::Base(PlaceBase::Promoted(..)),
1958-
is_local_mutation_allowed: _,
1959-
} => {}
19601955
RootPlace {
19611956
place: Place::Base(PlaceBase::Static(..)),
19621957
is_local_mutation_allowed: _,
@@ -1994,12 +1989,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19941989
}
19951990
// The rules for promotion are made by `qualify_consts`, there wouldn't even be a
19961991
// `Place::Promoted` if the promotion weren't 100% legal. So we just forward this
1997-
Place::Base(PlaceBase::Promoted(_)) => Ok(RootPlace {
1998-
place,
1999-
is_local_mutation_allowed,
2000-
}),
2001-
Place::Base(PlaceBase::Static(ref static_)) => {
2002-
if self.infcx.tcx.is_static(static_.def_id) != Some(hir::Mutability::MutMutable) {
1992+
Place::Base(PlaceBase::Static(box Static{kind: StaticKind::Promoted(_), ..})) =>
1993+
Ok(RootPlace {
1994+
place,
1995+
is_local_mutation_allowed,
1996+
}),
1997+
Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
1998+
if self.infcx.tcx.is_static(def_id) != Some(hir::Mutability::MutMutable) {
20031999
Err(place)
20042000
} else {
20052001
Ok(RootPlace {

src/librustc_mir/borrow_check/mutability_errors.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc::hir;
22
use rustc::hir::Node;
33
use rustc::mir::{self, BindingForm, Constant, ClearCrossCrate, Local, Location, Mir};
4-
use rustc::mir::{Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static};
4+
use rustc::mir::{
5+
Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind,
6+
};
57
use rustc::mir::{Terminator, TerminatorKind};
68
use rustc::ty::{self, Const, DefIdTree, TyS, TyKind, TyCtxt};
79
use rustc_data_structures::indexed_vec::Idx;
@@ -129,9 +131,10 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
129131
}
130132
}
131133

132-
Place::Base(PlaceBase::Promoted(_)) => unreachable!(),
134+
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })) =>
135+
unreachable!(),
133136

134-
Place::Base(PlaceBase::Static(box Static { def_id, ty: _ })) => {
137+
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })) => {
135138
if let Place::Base(PlaceBase::Static(_)) = access_place {
136139
item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
137140
reason = String::new();

0 commit comments

Comments
 (0)