提交 34ff9aa8 编写于 作者: A Ariel Ben-Yehuda

store the normalized types of statics in MIR Lvalues

The types of statics, like all other items, are stored in the tcx
unnormalized. This is necessarily so, because
    a) Item types other than statics have generics, which can't be
normalized.
    b) Eager normalization causes undesirable on-demand dependencies.

Keeping with the principle that MIR lvalues require no normalization in
order to interpret, this patch stores the normalized type of the statics
in the Lvalue and reads it to get the lvalue type.

Fixes #39367.
上级 ca870827
......@@ -816,12 +816,20 @@ pub enum Lvalue<'tcx> {
Local(Local),
/// static or static mut variable
Static(DefId),
Static(Box<Static<'tcx>>),
/// projection out of an lvalue (access a field, deref a pointer, etc)
Projection(Box<LvalueProjection<'tcx>>),
}
/// The def-id of a static, along with its normalized type (which is
/// stored to avoid requiring normalization when reading MIR).
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Static<'tcx> {
pub def_id: DefId,
pub ty: Ty<'tcx>,
}
/// The `Projection` data structure defines things of the form `B.x`
/// or `*B` or `B[index]`. Note that it is parameterized because it is
/// shared between `Constant` and `Lvalue`. See the aliases
......@@ -911,8 +919,8 @@ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match *self {
Local(id) => write!(fmt, "{:?}", id),
Static(def_id) =>
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
Static(box self::Static { def_id, ty }) =>
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty),
Projection(ref data) =>
match data.elem {
ProjectionElem::Downcast(ref adt_def, index) =>
......
......@@ -125,8 +125,8 @@ pub fn ty<'a, 'gcx>(&self, mir: &Mir<'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Lval
match *self {
Lvalue::Local(index) =>
LvalueTy::Ty { ty: mir.local_decls[index].ty },
Lvalue::Static(def_id) =>
LvalueTy::Ty { ty: tcx.item_type(def_id) },
Lvalue::Static(ref data) =>
LvalueTy::Ty { ty: data.ty },
Lvalue::Projection(ref proj) =>
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
}
......
......@@ -154,6 +154,13 @@ fn visit_lvalue(&mut self,
self.super_lvalue(lvalue, context, location);
}
fn visit_static(&mut self,
static_: & $($mutability)* Static<'tcx>,
context: LvalueContext<'tcx>,
location: Location) {
self.super_static(static_, context, location);
}
fn visit_projection(&mut self,
lvalue: & $($mutability)* LvalueProjection<'tcx>,
context: LvalueContext<'tcx>,
......@@ -559,8 +566,8 @@ fn super_lvalue(&mut self,
match *lvalue {
Lvalue::Local(_) => {
}
Lvalue::Static(ref $($mutability)* def_id) => {
self.visit_def_id(def_id, location);
Lvalue::Static(ref $($mutability)* static_) => {
self.visit_static(static_, context, location);
}
Lvalue::Projection(ref $($mutability)* proj) => {
self.visit_projection(proj, context, location);
......@@ -568,6 +575,18 @@ fn super_lvalue(&mut self,
}
}
fn super_static(&mut self,
static_: & $($mutability)* Static<'tcx>,
_context: LvalueContext<'tcx>,
location: Location) {
let Static {
ref $($mutability)* def_id,
ref $($mutability)* ty,
} = *static_;
self.visit_def_id(def_id, location);
self.visit_ty(ty);
}
fn super_projection(&mut self,
proj: & $($mutability)* LvalueProjection<'tcx>,
context: LvalueContext<'tcx>,
......@@ -837,4 +856,3 @@ pub fn is_use(&self) -> bool {
self.is_mutating_use() || self.is_nonmutating_use()
}
}
......@@ -84,7 +84,7 @@ fn expr_as_lvalue(&mut self,
block.and(Lvalue::Local(index))
}
ExprKind::StaticRef { id } => {
block.and(Lvalue::Static(id))
block.and(Lvalue::Static(Box::new(Static { def_id: id, ty: expr.ty })))
}
ExprKind::Array { .. } |
......
......@@ -126,8 +126,18 @@ fn sanitize_lvalue(&mut self, lvalue: &Lvalue<'tcx>, location: Location) -> Lval
debug!("sanitize_lvalue: {:?}", lvalue);
match *lvalue {
Lvalue::Local(index) => LvalueTy::Ty { ty: self.mir.local_decls[index].ty },
Lvalue::Static(def_id) =>
LvalueTy::Ty { ty: self.tcx().item_type(def_id) },
Lvalue::Static(box Static { def_id, ty: sty }) => {
let sty = self.sanitize_type(lvalue, sty);
let ty = self.tcx().item_type(def_id);
let ty = self.cx.normalize(&ty);
if let Err(terr) = self.cx.eq_types(self.last_span, ty, sty) {
span_mirbug!(
self, lvalue, "bad static type ({:?}: {:?}): {:?}",
ty, sty, terr);
}
LvalueTy::Ty { ty: sty }
},
Lvalue::Projection(ref proj) => {
let base_ty = self.sanitize_lvalue(&proj.base, location);
if let LvalueTy::Ty { ty } = base_ty {
......
......@@ -382,11 +382,11 @@ fn const_lvalue(&self, lvalue: &mir::Lvalue<'tcx>, span: Span)
let lvalue = match *lvalue {
mir::Lvalue::Local(_) => bug!(), // handled above
mir::Lvalue::Static(def_id) => {
mir::Lvalue::Static(box mir::Static { def_id, ty }) => {
ConstLvalue {
base: Base::Static(consts::get_static(self.ccx, def_id)),
llextra: ptr::null_mut(),
ty: lvalue.ty(self.mir, tcx).to_ty(tcx)
ty: self.monomorphize(&ty),
}
}
mir::Lvalue::Projection(ref projection) => {
......
......@@ -304,10 +304,9 @@ pub fn trans_lvalue(&mut self,
let result = match *lvalue {
mir::Lvalue::Local(_) => bug!(), // handled above
mir::Lvalue::Static(def_id) => {
let const_ty = self.monomorphized_lvalue_ty(lvalue);
mir::Lvalue::Static(box mir::Static { def_id, ty }) => {
LvalueRef::new_sized(consts::get_static(ccx, def_id),
LvalueTy::from_ty(const_ty),
LvalueTy::from_ty(self.monomorphize(&ty)),
Alignment::AbiAligned)
},
mir::Lvalue::Projection(box mir::Projection {
......
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::ops::Deref;
struct ArenaSet<U: Deref, V=<U as Deref>::Target>(U, &'static V)
where V: 'static + ?Sized;
static Z: [u8; 4] = [1,2,3,4];
fn arena() -> &'static ArenaSet<Vec<u8>> {
fn __static_ref_initialize() -> ArenaSet<Vec<u8>> {
ArenaSet(vec![], &Z)
}
unsafe {
use std::sync::{Once, ONCE_INIT};
fn require_sync<T: Sync>(_: &T) { }
unsafe fn __stability() -> &'static ArenaSet<Vec<u8>> {
use std::mem::transmute;
use std::boxed::Box;
static mut DATA: *const ArenaSet<Vec<u8>> = 0 as *const ArenaSet<Vec<u8>>;
static mut ONCE: Once = ONCE_INIT;
ONCE.call_once(|| {
DATA = transmute
::<Box<ArenaSet<Vec<u8>>>, *const ArenaSet<Vec<u8>>>
(Box::new(__static_ref_initialize()));
});
&*DATA
}
let static_ref = __stability();
require_sync(static_ref);
static_ref
}
}
fn main() {
let &ArenaSet(ref u, v) = arena();
assert!(u.is_empty());
assert_eq!(v, Z);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册