Skip to content

Commit 8c6c0f8

Browse files
committed
Auto merge of #40163 - arielb1:normalization-1702, r=nikomatsakis
More through normalization, Feb/Mar 2017 edition Fix a few normalization bugs. Fixes #27901. Fixes #28828. Fixes #38135. Fixes #39363. Fixes #39367.
2 parents 5208090 + 4aede75 commit 8c6c0f8

File tree

22 files changed

+235
-136
lines changed

22 files changed

+235
-136
lines changed

src/librustc/mir/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,20 @@ pub enum Lvalue<'tcx> {
816816
Local(Local),
817817

818818
/// static or static mut variable
819-
Static(DefId),
819+
Static(Box<Static<'tcx>>),
820820

821821
/// projection out of an lvalue (access a field, deref a pointer, etc)
822822
Projection(Box<LvalueProjection<'tcx>>),
823823
}
824824

825+
/// The def-id of a static, along with its normalized type (which is
826+
/// stored to avoid requiring normalization when reading MIR).
827+
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable)]
828+
pub struct Static<'tcx> {
829+
pub def_id: DefId,
830+
pub ty: Ty<'tcx>,
831+
}
832+
825833
/// The `Projection` data structure defines things of the form `B.x`
826834
/// or `*B` or `B[index]`. Note that it is parameterized because it is
827835
/// shared between `Constant` and `Lvalue`. See the aliases
@@ -911,8 +919,8 @@ impl<'tcx> Debug for Lvalue<'tcx> {
911919

912920
match *self {
913921
Local(id) => write!(fmt, "{:?}", id),
914-
Static(def_id) =>
915-
write!(fmt, "{}", ty::tls::with(|tcx| tcx.item_path_str(def_id))),
922+
Static(box self::Static { def_id, ty }) =>
923+
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.item_path_str(def_id)), ty),
916924
Projection(ref data) =>
917925
match data.elem {
918926
ProjectionElem::Downcast(ref adt_def, index) =>

src/librustc/mir/tcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ impl<'tcx> Lvalue<'tcx> {
125125
match *self {
126126
Lvalue::Local(index) =>
127127
LvalueTy::Ty { ty: mir.local_decls[index].ty },
128-
Lvalue::Static(def_id) =>
129-
LvalueTy::Ty { ty: tcx.item_type(def_id) },
128+
Lvalue::Static(ref data) =>
129+
LvalueTy::Ty { ty: data.ty },
130130
Lvalue::Projection(ref proj) =>
131131
proj.base.ty(mir, tcx).projection_ty(tcx, &proj.elem),
132132
}

src/librustc/mir/visit.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ macro_rules! make_mir_visitor {
154154
self.super_lvalue(lvalue, context, location);
155155
}
156156

157+
fn visit_static(&mut self,
158+
static_: & $($mutability)* Static<'tcx>,
159+
context: LvalueContext<'tcx>,
160+
location: Location) {
161+
self.super_static(static_, context, location);
162+
}
163+
157164
fn visit_projection(&mut self,
158165
lvalue: & $($mutability)* LvalueProjection<'tcx>,
159166
context: LvalueContext<'tcx>,
@@ -554,15 +561,27 @@ macro_rules! make_mir_visitor {
554561
match *lvalue {
555562
Lvalue::Local(_) => {
556563
}
557-
Lvalue::Static(ref $($mutability)* def_id) => {
558-
self.visit_def_id(def_id, location);
564+
Lvalue::Static(ref $($mutability)* static_) => {
565+
self.visit_static(static_, context, location);
559566
}
560567
Lvalue::Projection(ref $($mutability)* proj) => {
561568
self.visit_projection(proj, context, location);
562569
}
563570
}
564571
}
565572

573+
fn super_static(&mut self,
574+
static_: & $($mutability)* Static<'tcx>,
575+
_context: LvalueContext<'tcx>,
576+
location: Location) {
577+
let Static {
578+
ref $($mutability)* def_id,
579+
ref $($mutability)* ty,
580+
} = *static_;
581+
self.visit_def_id(def_id, location);
582+
self.visit_ty(ty);
583+
}
584+
566585
fn super_projection(&mut self,
567586
proj: & $($mutability)* LvalueProjection<'tcx>,
568587
context: LvalueContext<'tcx>,
@@ -818,4 +837,3 @@ impl<'tcx> LvalueContext<'tcx> {
818837
self.is_mutating_use() || self.is_nonmutating_use()
819838
}
820839
}
821-

src/librustc_mir/build/expr/as_lvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
8686
block.and(Lvalue::Local(index))
8787
}
8888
ExprKind::StaticRef { id } => {
89-
block.and(Lvalue::Static(id))
89+
block.and(Lvalue::Static(Box::new(Static { def_id: id, ty: expr.ty })))
9090
}
9191

9292
ExprKind::Array { .. } |

src/librustc_mir/transform/type_check.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ use syntax_pos::{Span, DUMMY_SP};
2525

2626
use rustc_data_structures::indexed_vec::Idx;
2727

28+
fn mirbug(tcx: TyCtxt, span: Span, msg: &str) {
29+
tcx.sess.diagnostic().span_bug(span, msg);
30+
}
31+
2832
macro_rules! span_mirbug {
2933
($context:expr, $elem:expr, $($message:tt)*) => ({
30-
$context.tcx().sess.span_warn(
31-
$context.last_span,
32-
&format!("broken MIR ({:?}): {}", $elem, format!($($message)*))
33-
)
34+
mirbug($context.tcx(), $context.last_span,
35+
&format!("broken MIR ({:?}): {}", $elem, format!($($message)*)))
3436
})
3537
}
3638

3739
macro_rules! span_mirbug_and_err {
3840
($context:expr, $elem:expr, $($message:tt)*) => ({
3941
{
40-
$context.tcx().sess.span_warn(
41-
$context.last_span,
42-
&format!("broken MIR ({:?}): {:?}", $elem, format!($($message)*))
43-
);
42+
span_mirbug!($context, $elem, $($message)*);
4443
$context.error()
4544
}
4645
})
@@ -125,8 +124,18 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
125124
debug!("sanitize_lvalue: {:?}", lvalue);
126125
match *lvalue {
127126
Lvalue::Local(index) => LvalueTy::Ty { ty: self.mir.local_decls[index].ty },
128-
Lvalue::Static(def_id) =>
129-
LvalueTy::Ty { ty: self.tcx().item_type(def_id) },
127+
Lvalue::Static(box Static { def_id, ty: sty }) => {
128+
let sty = self.sanitize_type(lvalue, sty);
129+
let ty = self.tcx().item_type(def_id);
130+
let ty = self.cx.normalize(&ty);
131+
if let Err(terr) = self.cx.eq_types(self.last_span, ty, sty) {
132+
span_mirbug!(
133+
self, lvalue, "bad static type ({:?}: {:?}): {:?}",
134+
ty, sty, terr);
135+
}
136+
LvalueTy::Ty { ty: sty }
137+
138+
},
130139
Lvalue::Projection(ref proj) => {
131140
let base_ty = self.sanitize_lvalue(&proj.base, location);
132141
if let LvalueTy::Ty { ty } = base_ty {

src/librustc_trans/base.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,7 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance
596596
// release builds.
597597
info!("trans_instance({})", instance);
598598

599-
let fn_ty = ccx.tcx().item_type(instance.def);
600-
let fn_ty = ccx.tcx().erase_regions(&fn_ty);
601-
let fn_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &fn_ty);
602-
599+
let fn_ty = common::def_ty(ccx.shared(), instance.def, instance.substs);
603600
let sig = common::ty_fn_sig(ccx, fn_ty);
604601
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
605602

@@ -626,9 +623,7 @@ pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
626623
attributes::inline(llfn, attributes::InlineAttr::Hint);
627624
attributes::set_frame_pointer_elimination(ccx, llfn);
628625

629-
let ctor_ty = ccx.tcx().item_type(def_id);
630-
let ctor_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &ctor_ty);
631-
626+
let ctor_ty = common::def_ty(ccx.shared(), def_id, substs);
632627
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&ctor_ty.fn_sig());
633628
let fn_ty = FnType::new(ccx, sig, &[]);
634629

src/librustc_trans/callee.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ use abi::{Abi, FnType};
2424
use attributes;
2525
use base;
2626
use builder::Builder;
27-
use common::{self, CrateContext, SharedCrateContext};
27+
use common::{self, CrateContext};
2828
use cleanup::CleanupScope;
2929
use mir::lvalue::LvalueRef;
3030
use consts;
31+
use common::def_ty;
3132
use declare;
3233
use value::Value;
3334
use meth;
34-
use monomorphize::{self, Instance};
35+
use monomorphize::Instance;
3536
use trans_item::TransItem;
3637
use type_of;
3738
use Disr;
@@ -207,16 +208,6 @@ impl<'tcx> Callee<'tcx> {
207208
}
208209
}
209210

210-
/// Given a DefId and some Substs, produces the monomorphic item type.
211-
fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
212-
def_id: DefId,
213-
substs: &'tcx Substs<'tcx>)
214-
-> Ty<'tcx> {
215-
let ty = shared.tcx().item_type(def_id);
216-
monomorphize::apply_param_substs(shared, substs, &ty)
217-
}
218-
219-
220211
fn trans_closure_method<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
221212
def_id: DefId,
222213
substs: ty::ClosureSubsts<'tcx>,
@@ -544,8 +535,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
544535

545536
let substs = tcx.normalize_associated_type(&substs);
546537
let instance = Instance::new(def_id, substs);
547-
let item_ty = ccx.tcx().item_type(def_id);
548-
let fn_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &item_ty);
538+
let fn_ty = common::def_ty(ccx.shared(), def_id, substs);
549539

550540
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
551541
return (llfn, fn_ty);

src/librustc_trans/collector.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use syntax_pos::DUMMY_SP;
207207
use base::custom_coerce_unsize_info;
208208
use callee::needs_fn_once_adapter_shim;
209209
use context::SharedCrateContext;
210-
use common::fulfill_obligation;
210+
use common::{def_ty, fulfill_obligation};
211211
use glue::{self, DropGlueKind};
212212
use monomorphize::{self, Instance};
213213
use util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
@@ -341,7 +341,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(scx: &SharedCrateContext<'a, 'tcx>,
341341
// Sanity check whether this ended up being collected accidentally
342342
debug_assert!(should_trans_locally(scx.tcx(), def_id));
343343

344-
let ty = scx.tcx().item_type(def_id);
344+
let ty = def_ty(scx, def_id, Substs::empty());
345345
let ty = glue::get_drop_glue_type(scx, ty);
346346
neighbors.push(TransItem::DropGlue(DropGlueKind::Ty(ty)));
347347

@@ -815,10 +815,7 @@ fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
815815
}
816816
ty::TyAdt(def, substs) => {
817817
for field in def.all_fields() {
818-
let field_type = scx.tcx().item_type(field.did);
819-
let field_type = monomorphize::apply_param_substs(scx,
820-
substs,
821-
&field_type);
818+
let field_type = def_ty(scx, field.did, substs);
822819
let field_type = glue::get_drop_glue_type(scx, field_type);
823820

824821
if scx.type_needs_drop(field_type) {
@@ -1184,7 +1181,7 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
11841181
debug!("RootCollector: ADT drop-glue for {}",
11851182
def_id_to_string(self.scx.tcx(), def_id));
11861183

1187-
let ty = self.scx.tcx().item_type(def_id);
1184+
let ty = def_ty(self.scx, def_id, Substs::empty());
11881185
let ty = glue::get_drop_glue_type(self.scx, ty);
11891186
self.output.push(TransItem::DropGlue(DropGlueKind::Ty(ty)));
11901187
}

src/librustc_trans/common.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use type_::Type;
2929
use value::Value;
3030
use rustc::ty::{self, Ty, TyCtxt};
3131
use rustc::ty::layout::Layout;
32-
use rustc::ty::subst::Subst;
32+
use rustc::ty::subst::{Subst, Substs};
3333
use rustc::traits::{self, SelectionContext, Reveal};
3434
use rustc::hir;
3535

@@ -604,3 +604,13 @@ pub fn ty_fn_sig<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
604604
pub fn is_closure(tcx: TyCtxt, def_id: DefId) -> bool {
605605
tcx.def_key(def_id).disambiguated_data.data == DefPathData::ClosureExpr
606606
}
607+
608+
/// Given a DefId and some Substs, produces the monomorphic item type.
609+
pub fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
610+
def_id: DefId,
611+
substs: &'tcx Substs<'tcx>)
612+
-> Ty<'tcx>
613+
{
614+
let ty = shared.tcx().item_type(def_id);
615+
monomorphize::apply_param_substs(shared, substs, &ty)
616+
}

src/librustc_trans/consts.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ use rustc::hir::map as hir_map;
1818
use {debuginfo, machine};
1919
use base;
2020
use trans_item::TransItem;
21-
use common::{CrateContext, val_ty};
21+
use common::{self, CrateContext, val_ty};
2222
use declare;
23-
use monomorphize::{Instance};
23+
use monomorphize::Instance;
2424
use type_::Type;
2525
use type_of;
2626
use rustc::ty;
27+
use rustc::ty::subst::Substs;
2728

2829
use rustc::hir;
2930

@@ -84,7 +85,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
8485
return g;
8586
}
8687

87-
let ty = ccx.tcx().item_type(def_id);
88+
let ty = common::def_ty(ccx.shared(), def_id, Substs::empty());
8889
let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) {
8990

9091
let llty = type_of::type_of(ccx, ty);
@@ -234,7 +235,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
234235
v
235236
};
236237

237-
let ty = ccx.tcx().item_type(def_id);
238+
let ty = common::def_ty(ccx.shared(), def_id, Substs::empty());
238239
let llty = type_of::type_of(ccx, ty);
239240
let g = if val_llty == llty {
240241
g

src/librustc_trans/debuginfo/metadata.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc::ty::util::TypeIdHasher;
3333
use rustc::hir;
3434
use rustc_data_structures::ToHex;
3535
use {type_of, machine, monomorphize};
36-
use common::CrateContext;
36+
use common::{self, CrateContext};
3737
use type_::Type;
3838
use rustc::ty::{self, AdtKind, Ty, layout};
3939
use session::config;
@@ -377,7 +377,7 @@ fn subroutine_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
377377
span: Span)
378378
-> MetadataCreationResult
379379
{
380-
let signature = cx.tcx().erase_late_bound_regions(&signature);
380+
let signature = cx.tcx().erase_late_bound_regions_and_normalize(&signature);
381381

382382
let mut signature_metadata: Vec<DIType> = Vec::with_capacity(signature.inputs().len() + 1);
383383

@@ -1764,16 +1764,15 @@ pub fn create_global_var_metadata(cx: &CrateContext,
17641764
};
17651765

17661766
let is_local_to_unit = is_node_local_to_unit(cx, node_id);
1767-
let variable_type = tcx.erase_regions(&tcx.item_type(node_def_id));
1767+
let variable_type = common::def_ty(cx.shared(), node_def_id, Substs::empty());
17681768
let type_metadata = type_metadata(cx, variable_type, span);
17691769
let var_name = tcx.item_name(node_def_id).to_string();
17701770
let linkage_name = mangled_name_of_item(cx, node_def_id, "");
17711771

17721772
let var_name = CString::new(var_name).unwrap();
17731773
let linkage_name = CString::new(linkage_name).unwrap();
17741774

1775-
let ty = cx.tcx().item_type(node_def_id);
1776-
let global_align = type_of::align_of(cx, ty);
1775+
let global_align = type_of::align_of(cx, variable_type);
17771776

17781777
unsafe {
17791778
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),

src/librustc_trans/debuginfo/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use rustc::hir::def_id::DefId;
2727
use rustc::ty::subst::Substs;
2828

2929
use abi::Abi;
30-
use common::CrateContext;
30+
use common::{self, CrateContext};
3131
use builder::Builder;
32-
use monomorphize::{self, Instance};
32+
use monomorphize::Instance;
3333
use rustc::ty::{self, Ty};
3434
use rustc::mir;
3535
use session::config::{self, FullDebugInfo, LimitedDebugInfo, NoDebugInfo};
@@ -397,11 +397,8 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
397397
let self_type = cx.tcx().impl_of_method(instance.def).and_then(|impl_def_id| {
398398
// If the method does *not* belong to a trait, proceed
399399
if cx.tcx().trait_id_of_impl(impl_def_id).is_none() {
400-
let impl_self_ty = cx.tcx().item_type(impl_def_id);
401-
let impl_self_ty = cx.tcx().erase_regions(&impl_self_ty);
402-
let impl_self_ty = monomorphize::apply_param_substs(cx.shared(),
403-
instance.substs,
404-
&impl_self_ty);
400+
let impl_self_ty =
401+
common::def_ty(cx.shared(), impl_def_id, instance.substs);
405402

406403
// Only "class" methods are generally understood by LLVM,
407404
// so avoid methods on other types (e.g. `<*mut T>::null`).

src/librustc_trans/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,11 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
382382

383383
let lvalue = match *lvalue {
384384
mir::Lvalue::Local(_) => bug!(), // handled above
385-
mir::Lvalue::Static(def_id) => {
385+
mir::Lvalue::Static(box mir::Static { def_id, ty }) => {
386386
ConstLvalue {
387387
base: Base::Static(consts::get_static(self.ccx, def_id)),
388388
llextra: ptr::null_mut(),
389-
ty: lvalue.ty(self.mir, tcx).to_ty(tcx)
389+
ty: self.monomorphize(&ty),
390390
}
391391
}
392392
mir::Lvalue::Projection(ref projection) => {

0 commit comments

Comments
 (0)