adding mir::StaticKind enum for static and promoted

This commit is contained in:
Saleem Jaffer 2019-03-23 18:29:02 +05:30
parent cf2f1bb072
commit 752544b284
19 changed files with 240 additions and 168 deletions

View File

@ -1915,19 +1915,22 @@ pub enum PlaceBase<'tcx> {
Static(Box<Static<'tcx>>), Static(Box<Static<'tcx>>),
} }
/// The `DefId` of a static, along with its normalized type (which is /// We store the normalized type to avoid requiring normalization when reading MIR
/// stored to avoid requiring normalization when reading MIR).
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Static<'tcx> { pub struct Static<'tcx> {
pub def_id: DefId,
pub ty: Ty<'tcx>, pub ty: Ty<'tcx>,
pub promoted: Option<Promoted>, pub kind: StaticKind,
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, RustcEncodable, RustcDecodable)]
pub enum StaticKind {
Promoted(Promoted),
Static(DefId),
} }
impl_stable_hash_for!(struct Static<'tcx> { impl_stable_hash_for!(struct Static<'tcx> {
def_id,
ty, ty,
promoted kind
}); });
/// The `Projection` data structure defines things of the form `B.x` /// The `Projection` data structure defines things of the form `B.x`
@ -2058,21 +2061,23 @@ impl<'tcx> Debug for Place<'tcx> {
match *self { match *self {
Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id), Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id),
Base(PlaceBase::Static(box self::Static { def_id, ty, promoted })) => { Base(PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) })) => {
match promoted { write!(
None => write!( fmt,
fmt, "({}: {:?})",
"({}: {:?})", ty::tls::with(|tcx| tcx.def_path_str(def_id)),
ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty
ty )
), },
Some(pr) => write!( Base(PlaceBase::Static(
fmt, box self::Static { ty, kind: StaticKind::Promoted(promoted) })
"({:?}: {:?})", ) => {
pr, write!(
ty fmt,
), "({:?}: {:?})",
} promoted,
ty
)
}, },
Projection(ref data) => match data.elem { Projection(ref data) => match data.elem {
ProjectionElem::Downcast(ref adt_def, index) => { ProjectionElem::Downcast(ref adt_def, index) => {

View File

@ -725,15 +725,18 @@ macro_rules! make_mir_visitor {
place: & $($mutability)? Place<'tcx>, place: & $($mutability)? Place<'tcx>,
context: PlaceContext<'tcx>, context: PlaceContext<'tcx>,
location: Location) { location: Location) {
use crate::mir::{Static, StaticKind};
match place { match place {
Place::Base(PlaceBase::Local(local)) => { Place::Base(PlaceBase::Local(local)) => {
self.visit_local(local, context, location); self.visit_local(local, context, location);
} }
Place::Base(PlaceBase::Static(static_)) => { Place::Base(
if static_.promoted.is_none() { PlaceBase::Static(box Static{kind: StaticKind::Static(def_id), ..})
self.visit_def_id(& $($mutability)? static_.def_id, location); ) => {
} self.visit_def_id(& $($mutability)? *def_id, location)
self.visit_ty(& $($mutability)? static_.ty, TyContext::Location(location)); }
Place::Base(PlaceBase::Static(box Static{ty, ..})) => {
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
} }
Place::Projection(proj) => { Place::Projection(proj) => {
self.visit_projection(proj, context, location); self.visit_projection(proj, context, location);

View File

@ -1,7 +1,7 @@
use rustc::middle::lang_items; use rustc::middle::lang_items;
use rustc::ty::{self, Ty, TypeFoldable}; use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt}; use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
use rustc::mir::{self, Place, PlaceBase}; use rustc::mir::{self, Place, PlaceBase, Static, StaticKind};
use rustc::mir::interpret::EvalErrorKind; use rustc::mir::interpret::EvalErrorKind;
use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode}; use rustc_target::abi::call::{ArgType, FnType, PassMode, IgnoreMode};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
@ -621,14 +621,18 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// but specified directly in the code. This means it gets promoted // but specified directly in the code. This means it gets promoted
// and we can then extract the value by evaluating the promoted. // and we can then extract the value by evaluating the promoted.
mir::Operand::Copy( mir::Operand::Copy(
Place::Base(PlaceBase::Static( Place::Base(
box mir::Static {promoted: Some(promoted), ty, ..} PlaceBase::Static(
)) box Static { kind: StaticKind::Promoted(promoted), ty }
)
)
) | ) |
mir::Operand::Move( mir::Operand::Move(
Place::Base(PlaceBase::Static( Place::Base(
box mir::Static {promoted: Some(promoted), ty, ..} PlaceBase::Static(
)) box Static { kind: StaticKind::Promoted(promoted), ty }
)
)
) => { ) => {
let param_env = ty::ParamEnv::reveal_all(); let param_env = ty::ParamEnv::reveal_all();
let cid = mir::interpret::GlobalId { let cid = mir::interpret::GlobalId {

View File

@ -409,7 +409,9 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let result = match *place { let result = match *place {
mir::Place::Base(mir::PlaceBase::Local(_)) => bug!(), // handled above mir::Place::Base(mir::PlaceBase::Local(_)) => bug!(), // handled above
mir::Place::Base( mir::Place::Base(
mir::PlaceBase::Static(box mir::Static { def_id: _, ty, promoted: Some(promoted) }) mir::PlaceBase::Static(
box mir::Static { ty, kind: mir::StaticKind::Promoted(promoted) }
)
) => { ) => {
let param_env = ty::ParamEnv::reveal_all(); let param_env = ty::ParamEnv::reveal_all();
let cid = mir::interpret::GlobalId { let cid = mir::interpret::GlobalId {
@ -438,7 +440,9 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
} }
} }
mir::Place::Base( mir::Place::Base(
mir::PlaceBase::Static(box mir::Static { def_id, ty, promoted: None }) mir::PlaceBase::Static(
box mir::Static { ty, kind: mir::StaticKind::Static(def_id) }
)
) => { ) => {
// NB: The layout of a static may be unsized as is the case when working // NB: The layout of a static may be unsized as is the case when working
// with a static that is an extern_type. // with a static that is an extern_type.

View File

@ -10,7 +10,7 @@ use rustc::mir::{
self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Constant, self, AggregateKind, BindingForm, BorrowKind, ClearCrossCrate, Constant,
ConstraintCategory, Field, Local, LocalDecl, LocalKind, Location, Operand, ConstraintCategory, Field, Local, LocalDecl, LocalKind, Location, Operand,
Place, PlaceBase, PlaceProjection, ProjectionElem, Rvalue, Statement, StatementKind, Place, PlaceBase, PlaceProjection, ProjectionElem, Rvalue, Statement, StatementKind,
TerminatorKind, VarBindingForm, Static, StaticKind, TerminatorKind, VarBindingForm,
}; };
use rustc::ty::{self, DefIdTree}; use rustc::ty::{self, DefIdTree};
use rustc::ty::print::Print; use rustc::ty::print::Print;
@ -1601,12 +1601,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
Place::Base(PlaceBase::Local(local)) => { Place::Base(PlaceBase::Local(local)) => {
self.append_local_to_string(local, buf)?; self.append_local_to_string(local, buf)?;
} }
Place::Base(PlaceBase::Static(ref static_)) => { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
if static_.promoted.is_some() { buf.push_str("promoted");
buf.push_str("promoted"); }
} else { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string()); buf.push_str(&self.infcx.tcx.item_name(def_id).to_string());
}
} }
Place::Projection(ref proj) => { Place::Projection(ref proj) => {
match proj.elem { match proj.elem {
@ -1808,8 +1807,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
/// Checks if a place is a thread-local static. /// Checks if a place is a thread-local static.
pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool { pub fn is_place_thread_local(&self, place: &Place<'tcx>) -> bool {
if let Place::Base(PlaceBase::Static(statik)) = place { if let Place::Base(
let attrs = self.infcx.tcx.get_attrs(statik.def_id); PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })
) = place {
let attrs = self.infcx.tcx.get_attrs(*def_id);
let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local")); let is_thread_local = attrs.iter().any(|attr| attr.check_name("thread_local"));
debug!( debug!(

View File

@ -8,7 +8,9 @@ use rustc::infer::InferCtxt;
use rustc::lint::builtin::UNUSED_MUT; use rustc::lint::builtin::UNUSED_MUT;
use rustc::middle::borrowck::SignalledError; use rustc::middle::borrowck::SignalledError;
use rustc::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind}; use rustc::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind};
use rustc::mir::{ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Place, PlaceBase}; use rustc::mir::{
ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Place, PlaceBase, Static, StaticKind
};
use rustc::mir::{Field, Projection, ProjectionElem, Rvalue, Statement, StatementKind}; use rustc::mir::{Field, Projection, ProjectionElem, Rvalue, Statement, StatementKind};
use rustc::mir::{Terminator, TerminatorKind}; use rustc::mir::{Terminator, TerminatorKind};
use rustc::ty::query::Providers; use rustc::ty::query::Providers;
@ -1308,8 +1310,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// //
// FIXME: allow thread-locals to borrow other thread locals? // FIXME: allow thread-locals to borrow other thread locals?
let (might_be_alive, will_be_dropped) = match root_place { let (might_be_alive, will_be_dropped) = match root_place {
Place::Base(PlaceBase::Static(st)) => { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {
(true, st.promoted.is_none() && self.is_place_thread_local(&root_place)) (true, false)
}
Place::Base(PlaceBase::Static(box Static{ kind: _, .. })) => {
// Thread-locals might be dropped after the function exits, but
// "true" statics will never be.
(true, self.is_place_thread_local(&root_place))
} }
Place::Base(PlaceBase::Local(_)) => { Place::Base(PlaceBase::Local(_)) => {
// Locals are always dropped at function exit, and if they // Locals are always dropped at function exit, and if they
@ -1982,18 +1989,19 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
} }
// The rules for promotion are made by `qualify_consts`, there wouldn't even be a // The rules for promotion are made by `qualify_consts`, there wouldn't even be a
// `Place::Promoted` if the promotion weren't 100% legal. So we just forward this // `Place::Promoted` if the promotion weren't 100% legal. So we just forward this
Place::Base(PlaceBase::Static(ref static_)) => { Place::Base(PlaceBase::Static(box Static{kind: StaticKind::Promoted(_), ..})) =>
if static_.promoted.is_some() || Ok(RootPlace {
(static_.promoted.is_none() && place,
self.infcx.tcx.is_static(static_.def_id) is_local_mutation_allowed,
== Some(hir::Mutability::MutMutable) }),
){ Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
if self.infcx.tcx.is_static(def_id) != Some(hir::Mutability::MutMutable) {
Err(place)
} else {
Ok(RootPlace { Ok(RootPlace {
place, place,
is_local_mutation_allowed, is_local_mutation_allowed,
}) })
} else {
Err(place)
} }
} }
Place::Projection(ref proj) => { Place::Projection(ref proj) => {

View File

@ -1,7 +1,9 @@
use rustc::hir; use rustc::hir;
use rustc::hir::Node; use rustc::hir::Node;
use rustc::mir::{self, BindingForm, Constant, ClearCrossCrate, Local, Location, Mir}; use rustc::mir::{self, BindingForm, Constant, ClearCrossCrate, Local, Location, Mir};
use rustc::mir::{Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static}; use rustc::mir::{
Mutability, Operand, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind,
};
use rustc::mir::{Terminator, TerminatorKind}; use rustc::mir::{Terminator, TerminatorKind};
use rustc::ty::{self, Const, DefIdTree, TyS, TyKind, TyCtxt}; use rustc::ty::{self, Const, DefIdTree, TyS, TyKind, TyCtxt};
use rustc_data_structures::indexed_vec::Idx; use rustc_data_structures::indexed_vec::Idx;
@ -129,8 +131,10 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
} }
} }
Place::Base(PlaceBase::Static(box Static { def_id, ty: _, promoted })) => { Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })) =>
assert!(promoted.is_none()); unreachable!(),
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })) => {
if let Place::Base(PlaceBase::Static(_)) = access_place { if let Place::Base(PlaceBase::Static(_)) = access_place {
item_msg = format!("immutable static item `{}`", access_place_desc.unwrap()); item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
reason = String::new(); reason = String::new();

View File

@ -453,45 +453,53 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
Place::Base(PlaceBase::Local(index)) => PlaceTy::Ty { Place::Base(PlaceBase::Local(index)) => PlaceTy::Ty {
ty: self.mir.local_decls[index].ty, ty: self.mir.local_decls[index].ty,
}, },
Place::Base(PlaceBase::Static(box Static { def_id, ty: sty, promoted })) => { Place::Base(
PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted), ty: sty })
) => {
let sty = self.sanitize_type(place, sty); let sty = self.sanitize_type(place, sty);
let check_err =
|verifier: &mut TypeVerifier<'a, 'b, 'gcx, 'tcx> , if !self.errors_reported {
place: &Place<'tcx>, let promoted_mir = &self.mir.promoted[promoted];
ty, self.sanitize_promoted(promoted_mir, location);
sty| {
if let Err(terr) = verifier.cx.eq_types( let promoted_ty = promoted_mir.return_ty();
if let Err(terr) = self.cx.eq_types(
sty, sty,
ty, promoted_ty,
location.to_locations(), location.to_locations(),
ConstraintCategory::Boring, ConstraintCategory::Boring,
) { ) {
span_mirbug!( span_mirbug!(
verifier, self,
place, place,
"bad promoted type ({:?}: {:?}): {:?}", "bad promoted type ({:?}: {:?}): {:?}",
ty, promoted_ty,
sty, sty,
terr terr
); );
}; };
}; }
match promoted { PlaceTy::Ty { ty: sty }
Some(pr) => { }
if !self.errors_reported { Place::Base(
let promoted_mir = &self.mir.promoted[pr]; PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), ty: sty })
self.sanitize_promoted(promoted_mir, location); ) => {
let sty = self.sanitize_type(place, sty);
let promoted_ty = promoted_mir.return_ty(); let ty = self.tcx().type_of(def_id);
check_err(self, place, promoted_ty, sty); let ty = self.cx.normalize(ty, location);
} if let Err(terr) =
} self.cx
None => { .eq_types(ty, sty, location.to_locations(), ConstraintCategory::Boring)
let ty = self.tcx().type_of(def_id); {
let ty = self.cx.normalize(ty, location); span_mirbug!(
self,
check_err(self, place, ty, sty); place,
} "bad static type ({:?}: {:?}): {:?}",
ty,
sty,
terr
);
} }
PlaceTy::Ty { ty: sty } PlaceTy::Ty { ty: sty }
} }

View File

@ -1,6 +1,6 @@
use rustc::hir; use rustc::hir;
use rustc::mir::ProjectionElem; use rustc::mir::ProjectionElem;
use rustc::mir::{Local, Mir, Place, PlaceBase, Mutability}; use rustc::mir::{Local, Mir, Place, PlaceBase, Mutability, Static, StaticKind};
use rustc::ty::{self, TyCtxt}; use rustc::ty::{self, TyCtxt};
use crate::borrow_check::borrow_set::LocalsStateAtExit; use crate::borrow_check::borrow_set::LocalsStateAtExit;
@ -49,9 +49,10 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
} }
} }
} }
Place::Base(PlaceBase::Static(static_)) => { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) =>
static_.promoted.is_none() && false,
(tcx.is_static(static_.def_id) == Some(hir::Mutability::MutMutable)) Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
tcx.is_static(*def_id) == Some(hir::Mutability::MutMutable)
} }
Place::Projection(proj) => match proj.elem { Place::Projection(proj) => match proj.elem {
ProjectionElem::Field(..) ProjectionElem::Field(..)

View File

@ -2,7 +2,7 @@ use crate::borrow_check::ArtificialField;
use crate::borrow_check::Overlap; use crate::borrow_check::Overlap;
use crate::borrow_check::{Deep, Shallow, AccessDepth}; use crate::borrow_check::{Deep, Shallow, AccessDepth};
use rustc::hir; use rustc::hir;
use rustc::mir::{BorrowKind, Mir, Place, PlaceBase, Projection, ProjectionElem}; use rustc::mir::{BorrowKind, Mir, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind};
use rustc::ty::{self, TyCtxt}; use rustc::ty::{self, TyCtxt};
use std::cmp::max; use std::cmp::max;
@ -370,47 +370,71 @@ fn place_element_conflict<'a, 'gcx: 'tcx, 'tcx>(
Overlap::Disjoint Overlap::Disjoint
} }
} }
(Place::Base(PlaceBase::Static(s1)), Place::Base(PlaceBase::Static(s2))) => { (
match (s1.promoted, s2.promoted) { Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id_1), .. })),
(None, None) => { Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id_2), .. })),
if s1.def_id != s2.def_id { ) => {
debug!("place_element_conflict: DISJOINT-STATIC"); if *def_id_1 != *def_id_2 {
Overlap::Disjoint debug!("place_element_conflict: DISJOINT-STATIC");
} else if tcx.is_static(s1.def_id) == Some(hir::Mutability::MutMutable) { Overlap::Disjoint
// We ignore mutable statics - they can only be unsafe code. } else if tcx.is_static(*def_id_1) == Some(hir::Mutability::MutMutable) {
debug!("place_element_conflict: IGNORE-STATIC-MUT"); // We ignore mutable statics - they can only be unsafe code.
Overlap::Disjoint debug!("place_element_conflict: IGNORE-STATIC-MUT");
} else { Overlap::Disjoint
debug!("place_element_conflict: DISJOINT-OR-EQ-STATIC"); } else {
Overlap::EqualOrDisjoint debug!("place_element_conflict: DISJOINT-OR-EQ-STATIC");
} Overlap::EqualOrDisjoint
},
(Some(p1), Some(p2)) => {
if p1 == p2 {
if let ty::Array(_, size) = s1.ty.sty {
if size.unwrap_usize(tcx) == 0 {
// Ignore conflicts with promoted [T; 0].
debug!("place_element_conflict: IGNORE-LEN-0-PROMOTED");
return Overlap::Disjoint;
}
}
// the same promoted - base case, equal
debug!("place_element_conflict: DISJOINT-OR-EQ-PROMOTED");
Overlap::EqualOrDisjoint
} else {
// different promoteds - base case, disjoint
debug!("place_element_conflict: DISJOINT-PROMOTED");
Overlap::Disjoint
}
},
(_, _) => {
debug!("place_element_conflict: DISJOINT-STATIC-PROMOTED");
Overlap::Disjoint
}
} }
} }
(Place::Base(PlaceBase::Local(_)), Place::Base(PlaceBase::Static(_))) | (
(Place::Base(PlaceBase::Static(_)), Place::Base(PlaceBase::Local(_))) => { Place::Base(
PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted_1), ty })
),
Place::Base(
PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted_2), .. })
),
) => {
if *promoted_1 == *promoted_2 {
if let ty::Array(_, size) = ty.sty {
if size.unwrap_usize(tcx) == 0 {
// Ignore conflicts with promoted [T; 0].
debug!("place_element_conflict: IGNORE-LEN-0-PROMOTED");
return Overlap::Disjoint;
}
}
// the same promoted - base case, equal
debug!("place_element_conflict: DISJOINT-OR-EQ-PROMOTED");
Overlap::EqualOrDisjoint
} else {
// different promoteds - base case, disjoint
debug!("place_element_conflict: DISJOINT-PROMOTED");
Overlap::Disjoint
}
}
(
Place::Base(PlaceBase::Local(_)),
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }))
) |
(
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })),
Place::Base(PlaceBase::Local(_))
) |
(
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })),
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(_), .. }))
) |
(
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(_), .. })),
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. }))
) |
(
Place::Base(PlaceBase::Local(_)),
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(_), .. }))
) |
(
Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(_), .. })),
Place::Base(PlaceBase::Local(_))
) => {
debug!("place_element_conflict: DISJOINT-STATIC-LOCAL-PROMOTED"); debug!("place_element_conflict: DISJOINT-STATIC-LOCAL-PROMOTED");
Overlap::Disjoint Overlap::Disjoint
} }

View File

@ -126,9 +126,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
block.and(place) block.and(place)
} }
ExprKind::StaticRef { id } => block.and(Place::Base(PlaceBase::Static(Box::new(Static { ExprKind::StaticRef { id } => block.and(Place::Base(PlaceBase::Static(Box::new(Static {
def_id: id,
ty: expr.ty, ty: expr.ty,
promoted: None, kind: StaticKind::Static(id),
})))), })))),
ExprKind::PlaceTypeAscription { source, user_ty } => { ExprKind::PlaceTypeAscription { source, user_ty } => {

View File

@ -582,9 +582,9 @@ where
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { ) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
use rustc::mir::Place::*; use rustc::mir::Place::*;
use rustc::mir::PlaceBase; use rustc::mir::PlaceBase;
use rustc::mir::Static; use rustc::mir::{Static, StaticKind};
Ok(match *mir_place { Ok(match *mir_place {
Base(PlaceBase::Static(box Static {promoted: Some(promoted), ty: _, ..})) => { Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted), .. })) => {
let instance = self.frame().instance; let instance = self.frame().instance;
self.const_eval_raw(GlobalId { self.const_eval_raw(GlobalId {
instance, instance,
@ -592,7 +592,7 @@ where
})? })?
} }
Base(PlaceBase::Static(box Static {promoted: None, ty, def_id})) => { Base(PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), ty })) => {
assert!(!ty.needs_subst()); assert!(!ty.needs_subst());
let layout = self.layout_of(ty)?; let layout = self.layout_of(ty)?;
let instance = ty::Instance::mono(*self.tcx, def_id); let instance = ty::Instance::mono(*self.tcx, def_id);

View File

@ -184,7 +184,7 @@ use rustc::ty::subst::{InternalSubsts, SubstsRef};
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind}; use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind};
use rustc::ty::adjustment::CustomCoerceUnsized; use rustc::ty::adjustment::CustomCoerceUnsized;
use rustc::session::config::EntryFnType; use rustc::session::config::EntryFnType;
use rustc::mir::{self, Location, Promoted}; use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
use rustc::mir::visit::Visitor as MirVisitor; use rustc::mir::visit::Visitor as MirVisitor;
use rustc::mir::mono::MonoItem; use rustc::mir::mono::MonoItem;
use rustc::mir::interpret::{Scalar, GlobalId, AllocKind, ErrorHandled}; use rustc::mir::interpret::{Scalar, GlobalId, AllocKind, ErrorHandled};
@ -655,8 +655,8 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
context: mir::visit::PlaceContext<'tcx>, context: mir::visit::PlaceContext<'tcx>,
location: Location) { location: Location) {
match place { match place {
mir::Place::Base( Place::Base(
mir::PlaceBase::Static(box mir::Static{def_id, promoted:None, ..}) PlaceBase::Static(box Static{ kind:StaticKind::Static(def_id), .. })
) => { ) => {
debug!("visiting static {:?} @ {:?}", def_id, location); debug!("visiting static {:?} @ {:?}", def_id, location);

View File

@ -300,9 +300,12 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
&Place::Base(PlaceBase::Local(..)) => { &Place::Base(PlaceBase::Local(..)) => {
// locals are safe // locals are safe
} }
&Place::Base(PlaceBase::Static(box Static { def_id, ty: _, promoted })) => { &Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })) => {
assert!(promoted.is_none(), "unsafety checking should happen before promotion"); bug!("unsafety checking should happen before promotion")
}
&Place::Base(
PlaceBase::Static(box Static { kind: StaticKind::Static(def_id), .. })
) => {
if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) { if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) {
self.require_unsafe("use of mutable static", self.require_unsafe("use of mutable static",
"mutable statics can be mutated by multiple threads: aliasing violations \ "mutable statics can be mutated by multiple threads: aliasing violations \

View File

@ -4,7 +4,7 @@
use rustc::hir::def::Def; use rustc::hir::def::Def;
use rustc::mir::{Constant, Location, Place, PlaceBase, Mir, Operand, Rvalue, Local}; use rustc::mir::{Constant, Location, Place, PlaceBase, Mir, Operand, Rvalue, Local};
use rustc::mir::{NullOp, UnOp, StatementKind, Statement, BasicBlock, LocalKind}; use rustc::mir::{NullOp, UnOp, StatementKind, Statement, BasicBlock, LocalKind, Static, StaticKind};
use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem}; use rustc::mir::{TerminatorKind, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem};
use rustc::mir::visit::{Visitor, PlaceContext, MutatingUseContext, NonMutatingUseContext}; use rustc::mir::visit::{Visitor, PlaceContext, MutatingUseContext, NonMutatingUseContext};
use rustc::mir::interpret::{EvalErrorKind, Scalar, GlobalId, EvalResult}; use rustc::mir::interpret::{EvalErrorKind, Scalar, GlobalId, EvalResult};
@ -266,7 +266,6 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
} }
fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> { fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
use rustc::mir::Static;
match *place { match *place {
Place::Base(PlaceBase::Local(loc)) => self.places[loc].clone(), Place::Base(PlaceBase::Local(loc)) => self.places[loc].clone(),
Place::Projection(ref proj) => match proj.elem { Place::Projection(ref proj) => match proj.elem {
@ -283,7 +282,9 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
// an `Index` projection would throw us off-track. // an `Index` projection would throw us off-track.
_ => None, _ => None,
}, },
Place::Base(PlaceBase::Static(box Static {promoted: Some(promoted), ..})) => { Place::Base(
PlaceBase::Static(box Static {kind: StaticKind::Promoted(promoted), ..})
) => {
let generics = self.tcx.generics_of(self.source.def_id()); let generics = self.tcx.generics_of(self.source.def_id());
if generics.requires_monomorphization(self.tcx) { if generics.requires_monomorphization(self.tcx) {
// FIXME: can't handle code with generics // FIXME: can't handle code with generics

View File

@ -692,7 +692,9 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
// Return pointer; update the place itself // Return pointer; update the place itself
*place = self.destination.clone(); *place = self.destination.clone();
}, },
Place::Base(PlaceBase::Static(box Static { promoted: Some(promoted), .. })) => { Place::Base(
PlaceBase::Static(box Static { kind: StaticKind::Promoted(promoted), .. })
) => {
if let Some(p) = self.promoted_map.get(*promoted).cloned() { if let Some(p) = self.promoted_map.get(*promoted).cloned() {
*promoted = p; *promoted = p;
} }

View File

@ -12,7 +12,6 @@
//! initialization and can otherwise silence errors, if //! initialization and can otherwise silence errors, if
//! move analysis runs after promotion on broken MIR. //! move analysis runs after promotion on broken MIR.
use rustc::hir::def_id::DefId;
use rustc::mir::*; use rustc::mir::*;
use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor}; use rustc::mir::visit::{PlaceContext, MutatingUseContext, MutVisitor, Visitor};
use rustc::mir::traversal::ReversePostorder; use rustc::mir::traversal::ReversePostorder;
@ -152,8 +151,7 @@ struct Promoter<'a, 'tcx: 'a> {
/// If true, all nested temps are also kept in the /// If true, all nested temps are also kept in the
/// source MIR, not moved to the promoted MIR. /// source MIR, not moved to the promoted MIR.
keep_original: bool, keep_original: bool
def_id: DefId,
} }
impl<'a, 'tcx> Promoter<'a, 'tcx> { impl<'a, 'tcx> Promoter<'a, 'tcx> {
@ -289,16 +287,16 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
} }
fn promote_candidate(mut self, candidate: Candidate) { fn promote_candidate(mut self, candidate: Candidate) {
use rustc::mir::Static;
let mut operand = { let mut operand = {
let def_id = self.def_id;
let promoted = &mut self.promoted; let promoted = &mut self.promoted;
let promoted_id = Promoted::new(self.source.promoted.len()); let promoted_id = Promoted::new(self.source.promoted.len());
let mut promoted_place = |ty, span| { let mut promoted_place = |ty, span| {
promoted.span = span; promoted.span = span;
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span); promoted.local_decls[RETURN_PLACE] =
LocalDecl::new_return_place(ty, span);
Place::Base( Place::Base(
PlaceBase::Static(Box::new(Static { def_id, ty, promoted: Some(promoted_id) }))) PlaceBase::Static(box Static{ kind: StaticKind::Promoted(promoted_id), ty })
)
}; };
let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut(); let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
match candidate { match candidate {
@ -371,8 +369,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>, pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>,
mut temps: IndexVec<Local, TempState>, mut temps: IndexVec<Local, TempState>,
candidates: Vec<Candidate>, candidates: Vec<Candidate>) {
def_id: DefId) {
// Visit candidates in reverse, in case they're nested. // Visit candidates in reverse, in case they're nested.
debug!("promote_candidates({:?})", candidates); debug!("promote_candidates({:?})", candidates);
@ -417,8 +414,7 @@ pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
tcx, tcx,
source: mir, source: mir,
temps: &mut temps, temps: &mut temps,
keep_original: false, keep_original: false
def_id,
}; };
promoter.promote_candidate(candidate); promoter.promote_candidate(candidate);
} }

View File

@ -188,8 +188,9 @@ trait Qualif {
fn in_place(cx: &ConstCx<'_, 'tcx>, place: &Place<'tcx>) -> bool { fn in_place(cx: &ConstCx<'_, 'tcx>, place: &Place<'tcx>) -> bool {
match *place { match *place {
Place::Base(PlaceBase::Local(local)) => Self::in_local(cx, local), Place::Base(PlaceBase::Local(local)) => Self::in_local(cx, local),
Place::Base(PlaceBase::Static(box Static {kind: StaticKind::Promoted(_), .. })) =>
bug!("qualifying already promoted MIR"),
Place::Base(PlaceBase::Static(ref static_)) => { Place::Base(PlaceBase::Static(ref static_)) => {
assert!(static_.promoted.is_none(), "qualifying already promoted MIR");
Self::in_static(cx, static_) Self::in_static(cx, static_)
}, },
Place::Projection(ref proj) => Self::in_projection(cx, proj), Place::Projection(ref proj) => Self::in_projection(cx, proj),
@ -372,11 +373,18 @@ impl Qualif for IsNotConst {
const IDX: usize = 2; const IDX: usize = 2;
fn in_static(cx: &ConstCx<'_, 'tcx>, static_: &Static<'tcx>) -> bool { fn in_static(cx: &ConstCx<'_, 'tcx>, static_: &Static<'tcx>) -> bool {
// Only allow statics (not consts) to refer to other statics. match static_.kind {
let allowed = cx.mode == Mode::Static || cx.mode == Mode::StaticMut; StaticKind::Promoted(_) => unreachable!(),
StaticKind::Static(def_id) => {
// Only allow statics (not consts) to refer to other statics.
let allowed = cx.mode == Mode::Static || cx.mode == Mode::StaticMut;
!allowed || !allowed ||
cx.tcx.get_attrs(static_.def_id).iter().any(|attr| attr.check_name("thread_local")) cx.tcx.get_attrs(def_id).iter().any(
|attr| attr.check_name("thread_local"
))
}
}
} }
fn in_projection(cx: &ConstCx<'_, 'tcx>, proj: &PlaceProjection<'tcx>) -> bool { fn in_projection(cx: &ConstCx<'_, 'tcx>, proj: &PlaceProjection<'tcx>) -> bool {
@ -770,8 +778,9 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
); );
dest = &proj.base; dest = &proj.base;
}, },
Place::Base(PlaceBase::Static(st)) => { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) =>
assert!(st.promoted.is_none(), "promoteds don't exist yet during promotion"); bug!("promoteds don't exist yet during promotion"),
Place::Base(PlaceBase::Static(box Static{ kind: _, .. })) => {
// Catch more errors in the destination. `visit_place` also checks that we // Catch more errors in the destination. `visit_place` also checks that we
// do not try to access statics from constants or try to mutate statics // do not try to access statics from constants or try to mutate statics
self.visit_place( self.visit_place(
@ -921,10 +930,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
self.super_place(place, context, location); self.super_place(place, context, location);
match *place { match *place {
Place::Base(PlaceBase::Local(_)) => {} Place::Base(PlaceBase::Local(_)) => {}
Place::Base(PlaceBase::Static(ref global)) => { Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Promoted(_), .. })) => {}
assert!(global.promoted.is_none()); Place::Base(PlaceBase::Static(box Static{ kind: StaticKind::Static(def_id), .. })) => {
if self.tcx if self.tcx
.get_attrs(global.def_id) .get_attrs(def_id)
.iter() .iter()
.any(|attr| attr.check_name("thread_local")) { .any(|attr| attr.check_name("thread_local")) {
if self.mode != Mode::Fn { if self.mode != Mode::Fn {
@ -1516,7 +1525,7 @@ impl MirPass for QualifyAndPromoteConstants {
}; };
// Do the actual promotion, now that we know what's viable. // Do the actual promotion, now that we know what's viable.
promote_consts::promote_candidates(mir, tcx, temps, candidates, def_id); promote_consts::promote_candidates(mir, tcx, temps, candidates);
} else { } else {
if !mir.control_flow_destroyed.is_empty() { if !mir.control_flow_destroyed.is_empty() {
let mut locals = mir.vars_iter(); let mut locals = mir.vars_iter();

View File

@ -257,8 +257,8 @@ fn check_place(
match place { match place {
Place::Base(PlaceBase::Local(_)) => Ok(()), Place::Base(PlaceBase::Local(_)) => Ok(()),
// promoteds are always fine, they are essentially constants // promoteds are always fine, they are essentially constants
Place::Base(PlaceBase::Static(box Static {def_id: _, ty: _, promoted: Some(_)})) => Ok(()), Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Promoted(_), .. })) => Ok(()),
Place::Base(PlaceBase::Static(box Static {def_id: _, ty: _, promoted: None})) => Place::Base(PlaceBase::Static(box Static { kind: StaticKind::Static(_), .. })) =>
Err((span, "cannot access `static` items in const fn".into())), Err((span, "cannot access `static` items in const fn".into())),
Place::Projection(proj) => { Place::Projection(proj) => {
match proj.elem { match proj.elem {