Skip to content

Commit ca87082

Browse files
committed
more through normalization in typeck & trans
Fixes rust-lang#27901. Fixes rust-lang#28828. Fixes rust-lang#38135. Fixes rust-lang#39363.
1 parent e1cb9ba commit ca87082

File tree

14 files changed

+121
-110
lines changed

14 files changed

+121
-110
lines changed

src/librustc_trans/base.rs

Lines changed: 2 additions & 7 deletions
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

Lines changed: 4 additions & 14 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 11 additions & 1 deletion
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 4 additions & 5 deletions
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

Lines changed: 4 additions & 7 deletions
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/partitioning.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@
103103
//! inlining, even when they are not marked #[inline].
104104
105105
use collector::InliningMap;
106+
use common;
106107
use context::SharedCrateContext;
107108
use llvm;
108-
use monomorphize;
109109
use rustc::dep_graph::{DepNode, WorkProductId};
110110
use rustc::hir::def_id::DefId;
111111
use rustc::hir::map::DefPathData;
@@ -468,12 +468,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(scx: &SharedCrateContext<'a, 't
468468
if let Some(impl_def_id) = tcx.impl_of_method(instance.def) {
469469
// This is a method within an inherent impl, find out what the
470470
// self-type is:
471-
let impl_self_ty = tcx.item_type(impl_def_id);
472-
let impl_self_ty = tcx.erase_regions(&impl_self_ty);
473-
let impl_self_ty = monomorphize::apply_param_substs(scx,
474-
instance.substs,
475-
&impl_self_ty);
476-
471+
let impl_self_ty = common::def_ty(scx, impl_def_id, instance.substs);
477472
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
478473
return Some(def_id);
479474
}

src/librustc_trans/trans_item.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use common;
2222
use declare;
2323
use glue::DropGlueKind;
2424
use llvm;
25-
use monomorphize::{self, Instance};
25+
use monomorphize::Instance;
2626
use rustc::dep_graph::DepNode;
2727
use rustc::hir;
2828
use rustc::hir::def_id::DefId;
@@ -146,7 +146,7 @@ impl<'a, 'tcx> TransItem<'tcx> {
146146
linkage: llvm::Linkage,
147147
symbol_name: &str) {
148148
let def_id = ccx.tcx().hir.local_def_id(node_id);
149-
let ty = ccx.tcx().item_type(def_id);
149+
let ty = common::def_ty(ccx.shared(), def_id, Substs::empty());
150150
let llty = type_of::type_of(ccx, ty);
151151

152152
let g = declare::define_global(ccx, symbol_name, llty).unwrap_or_else(|| {
@@ -168,10 +168,7 @@ impl<'a, 'tcx> TransItem<'tcx> {
168168
assert!(!instance.substs.needs_infer() &&
169169
!instance.substs.has_param_types());
170170

171-
let item_ty = ccx.tcx().item_type(instance.def);
172-
let item_ty = ccx.tcx().erase_regions(&item_ty);
173-
let mono_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &item_ty);
174-
171+
let mono_ty = common::def_ty(ccx.shared(), instance.def, instance.substs);
175172
let attrs = ccx.tcx().get_attrs(instance.def);
176173
let lldecl = declare::declare_fn(ccx, symbol_name, mono_ty);
177174
unsafe { llvm::LLVMRustSetLinkage(lldecl, linkage) };

src/librustc_typeck/astconv.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,8 @@ pub trait AstConv<'gcx, 'tcx> {
7979
item_name: ast::Name)
8080
-> Ty<'tcx>;
8181

82-
/// Project an associated type from a non-higher-ranked trait reference.
83-
/// This is fairly straightforward and can be accommodated in any context.
84-
fn projected_ty(&self,
85-
span: Span,
86-
_trait_ref: ty::TraitRef<'tcx>,
87-
_item_name: ast::Name)
88-
-> Ty<'tcx>;
82+
/// Normalize an associated type coming from the user.
83+
fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>;
8984

9085
/// Invoked when we encounter an error from some prior pass
9186
/// (e.g. resolve) that is translated into a ty-error. This is
@@ -310,8 +305,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
310305
tcx.types.err
311306
} else {
312307
// This is a default type parameter.
313-
ty::queries::ty::get(tcx, span, def.def_id)
314-
.subst_spanned(tcx, substs, Some(span))
308+
self.normalize_ty(
309+
span,
310+
ty::queries::ty::get(tcx, span, def.def_id)
311+
.subst_spanned(tcx, substs, Some(span))
312+
)
315313
}
316314
} else {
317315
// We've already errored above about the mismatch.
@@ -600,7 +598,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
600598
-> Ty<'tcx>
601599
{
602600
let substs = self.ast_path_substs_for_ty(span, did, item_segment);
603-
ty::queries::ty::get(self.tcx(), span, did).subst(self.tcx(), substs)
601+
self.normalize_ty(
602+
span,
603+
ty::queries::ty::get(self.tcx(), span, did).subst(self.tcx(), substs)
604+
)
604605
}
605606

606607
/// Transform a PolyTraitRef into a PolyExistentialTraitRef by
@@ -900,6 +901,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
900901

901902
let trait_did = bound.0.def_id;
902903
let ty = self.projected_ty_from_poly_trait_ref(span, bound, assoc_name);
904+
let ty = self.normalize_ty(span, ty);
903905

904906
let item = tcx.associated_items(trait_did).find(|i| i.name == assoc_name);
905907
let def_id = item.expect("missing associated type").def_id;
@@ -939,7 +941,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
939941

940942
debug!("qpath_to_ty: trait_ref={:?}", trait_ref);
941943

942-
self.projected_ty(span, trait_ref, item_segment.name)
944+
self.normalize_ty(span, tcx.mk_projection(trait_ref, item_segment.name))
943945
}
944946

945947
pub fn prohibit_type_params(&self, segments: &[hir::PathSegment]) {

0 commit comments

Comments
 (0)