Skip to content

Commit 3d6cf1d

Browse files
committed
auto merge of #14055 : nikomatsakis/rust/issue-5527-use-substs-in-trans, r=pcwalton
Code to use `ty::substs` in trans. As part of this, uncovered (and fixed) issue #14050. r? @pcwalton
2 parents 66f4f55 + ed7c849 commit 3d6cf1d

21 files changed

+435
-353
lines changed

src/librustc/metadata/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub enum astencode_tag { // Reserves 0x40 -- 0x5f
128128
tag_table_val = 0x45,
129129
tag_table_def = 0x46,
130130
tag_table_node_type = 0x47,
131-
tag_table_node_type_subst = 0x48,
131+
tag_table_item_subst = 0x48,
132132
tag_table_freevars = 0x49,
133133
tag_table_tcache = 0x4a,
134134
tag_table_param_defs = 0x4b,

src/librustc/middle/astencode.rs

+30-9
Original file line numberDiff line numberDiff line change
@@ -657,13 +657,13 @@ pub fn encode_vtable_origin(ecx: &e::EncodeContext,
657657
vtable_origin: &typeck::vtable_origin) {
658658
ebml_w.emit_enum("vtable_origin", |ebml_w| {
659659
match *vtable_origin {
660-
typeck::vtable_static(def_id, ref tys, ref vtable_res) => {
660+
typeck::vtable_static(def_id, ref substs, ref vtable_res) => {
661661
ebml_w.emit_enum_variant("vtable_static", 0u, 3u, |ebml_w| {
662662
ebml_w.emit_enum_variant_arg(0u, |ebml_w| {
663663
Ok(ebml_w.emit_def_id(def_id))
664664
});
665665
ebml_w.emit_enum_variant_arg(1u, |ebml_w| {
666-
Ok(ebml_w.emit_tys(ecx, tys.as_slice()))
666+
Ok(ebml_w.emit_substs(ecx, substs))
667667
});
668668
ebml_w.emit_enum_variant_arg(2u, |ebml_w| {
669669
Ok(encode_vtable_res(ecx, ebml_w, vtable_res))
@@ -744,7 +744,7 @@ impl<'a> vtable_decoder_helpers for reader::Decoder<'a> {
744744
Ok(this.read_def_id_noxcx(cdata))
745745
}).unwrap(),
746746
this.read_enum_variant_arg(1u, |this| {
747-
Ok(this.read_tys_noxcx(tcx, cdata))
747+
Ok(this.read_substs_noxcx(tcx, cdata))
748748
}).unwrap(),
749749
this.read_enum_variant_arg(2u, |this| {
750750
Ok(this.read_vtable_res(tcx, cdata))
@@ -962,11 +962,11 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
962962
})
963963
}
964964

965-
for tys in tcx.node_type_substs.borrow().find(&id).iter() {
966-
ebml_w.tag(c::tag_table_node_type_subst, |ebml_w| {
965+
for &item_substs in tcx.item_substs.borrow().find(&id).iter() {
966+
ebml_w.tag(c::tag_table_item_subst, |ebml_w| {
967967
ebml_w.id(id);
968968
ebml_w.tag(c::tag_table_val, |ebml_w| {
969-
ebml_w.emit_tys(ecx, tys.as_slice())
969+
ebml_w.emit_substs(ecx, &item_substs.substs);
970970
})
971971
})
972972
}
@@ -1091,6 +1091,9 @@ trait ebml_decoder_decoder_helpers {
10911091
fn read_tys_noxcx(&mut self,
10921092
tcx: &ty::ctxt,
10931093
cdata: &cstore::crate_metadata) -> Vec<ty::t>;
1094+
fn read_substs_noxcx(&mut self, tcx: &ty::ctxt,
1095+
cdata: &cstore::crate_metadata)
1096+
-> ty::substs;
10941097
}
10951098

10961099
impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
@@ -1115,6 +1118,21 @@ impl<'a> ebml_decoder_decoder_helpers for reader::Decoder<'a> {
11151118
.collect()
11161119
}
11171120

1121+
fn read_substs_noxcx(&mut self,
1122+
tcx: &ty::ctxt,
1123+
cdata: &cstore::crate_metadata)
1124+
-> ty::substs
1125+
{
1126+
self.read_opaque(|_, doc| {
1127+
Ok(tydecode::parse_substs_data(
1128+
doc.data,
1129+
cdata.cnum,
1130+
doc.start,
1131+
tcx,
1132+
|_, id| decoder::translate_def_id(cdata, id)))
1133+
}).unwrap()
1134+
}
1135+
11181136
fn read_ty(&mut self, xcx: &ExtendedDecodeContext) -> ty::t {
11191137
// Note: regions types embed local node ids. In principle, we
11201138
// should translate these node ids into the new decode
@@ -1312,9 +1330,12 @@ fn decode_side_tables(xcx: &ExtendedDecodeContext,
13121330
id, ty_to_str(dcx.tcx, ty));
13131331
dcx.tcx.node_types.borrow_mut().insert(id as uint, ty);
13141332
}
1315-
c::tag_table_node_type_subst => {
1316-
let tys = val_dsr.read_tys(xcx);
1317-
dcx.tcx.node_type_substs.borrow_mut().insert(id, tys);
1333+
c::tag_table_item_subst => {
1334+
let item_substs = ty::ItemSubsts {
1335+
substs: val_dsr.read_substs(xcx)
1336+
};
1337+
dcx.tcx.item_substs.borrow_mut().insert(
1338+
id, item_substs);
13181339
}
13191340
c::tag_table_freevars => {
13201341
let fv_info = val_dsr.read_to_vec(|val_dsr| {

src/librustc/middle/kind.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ pub fn check_expr(cx: &mut Context, e: &Expr) {
245245
{
246246
let method_map = cx.tcx.method_map.borrow();
247247
let method = method_map.find(&typeck::MethodCall::expr(e.id));
248-
let node_type_substs = cx.tcx.node_type_substs.borrow();
248+
let item_substs = cx.tcx.item_substs.borrow();
249249
let r = match method {
250250
Some(method) => Some(&method.substs.tps),
251-
None => node_type_substs.find(&e.id)
251+
None => item_substs.find(&e.id).map(|s| &s.substs.tps)
252252
};
253253
for ts in r.iter() {
254254
let def_map = cx.tcx.def_map.borrow();
@@ -341,15 +341,19 @@ fn check_trait_cast(cx: &mut Context, source_ty: ty::t, target_ty: ty::t, span:
341341
fn check_ty(cx: &mut Context, aty: &Ty) {
342342
match aty.node {
343343
TyPath(_, _, id) => {
344-
let node_type_substs = cx.tcx.node_type_substs.borrow();
345-
let r = node_type_substs.find(&id);
346-
for ts in r.iter() {
347-
let def_map = cx.tcx.def_map.borrow();
348-
let did = ast_util::def_id_of_def(def_map.get_copy(&id));
349-
let generics = ty::lookup_item_type(cx.tcx, did).generics;
350-
let type_param_defs = generics.type_param_defs();
351-
for (&ty, type_param_def) in ts.iter().zip(type_param_defs.iter()) {
352-
check_typaram_bounds(cx, aty.span, ty, type_param_def)
344+
match cx.tcx.item_substs.borrow().find(&id) {
345+
None => { }
346+
Some(ref item_substs) => {
347+
let def_map = cx.tcx.def_map.borrow();
348+
let did = ast_util::def_id_of_def(def_map.get_copy(&id));
349+
let generics = ty::lookup_item_type(cx.tcx, did).generics;
350+
let type_param_defs = generics.type_param_defs();
351+
for (&ty, type_param_def) in
352+
item_substs.substs.tps.iter().zip(
353+
type_param_defs.iter())
354+
{
355+
check_typaram_bounds(cx, aty.span, ty, type_param_def)
356+
}
353357
}
354358
}
355359
}

src/librustc/middle/subst.rs

+11
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ impl Subst for ty::substs {
195195
}
196196
}
197197

198+
impl Subst for ty::ItemSubsts {
199+
fn subst_spanned(&self, tcx: &ty::ctxt,
200+
substs: &ty::substs,
201+
span: Option<Span>)
202+
-> ty::ItemSubsts {
203+
ty::ItemSubsts {
204+
substs: self.substs.subst_spanned(tcx, substs, span)
205+
}
206+
}
207+
}
208+
198209
impl Subst for ty::RegionSubsts {
199210
fn subst_spanned(&self, tcx: &ty::ctxt,
200211
substs: &ty::substs,

src/librustc/middle/trans/base.rs

+15-47
Original file line numberDiff line numberDiff line change
@@ -478,35 +478,29 @@ pub fn note_unique_llvm_symbol(ccx: &CrateContext, sym: ~str) {
478478
pub fn get_res_dtor(ccx: &CrateContext,
479479
did: ast::DefId,
480480
parent_id: ast::DefId,
481-
substs: &[ty::t])
481+
substs: &ty::substs)
482482
-> ValueRef {
483483
let _icx = push_ctxt("trans_res_dtor");
484484
let did = if did.krate != ast::LOCAL_CRATE {
485485
inline::maybe_instantiate_inline(ccx, did)
486486
} else {
487487
did
488488
};
489-
if !substs.is_empty() {
489+
490+
if !substs.tps.is_empty() || !substs.self_ty.is_none() {
490491
assert_eq!(did.krate, ast::LOCAL_CRATE);
491-
let tsubsts = ty::substs {
492-
regions: ty::ErasedRegions,
493-
self_ty: None,
494-
tps: Vec::from_slice(substs),
495-
};
496492

497-
let vtables = typeck::check::vtable::trans_resolve_method(ccx.tcx(), did.node, &tsubsts);
498-
let (val, _) = monomorphize::monomorphic_fn(ccx, did, &tsubsts, vtables, None, None);
493+
let vtables = typeck::check::vtable::trans_resolve_method(ccx.tcx(), did.node, substs);
494+
let (val, _) = monomorphize::monomorphic_fn(ccx, did, substs, vtables, None, None);
499495

500496
val
501497
} else if did.krate == ast::LOCAL_CRATE {
502498
get_item_val(ccx, did.node)
503499
} else {
504500
let tcx = ccx.tcx();
505501
let name = csearch::get_symbol(&ccx.sess().cstore, did);
506-
let class_ty = ty::subst_tps(tcx,
507-
substs,
508-
None,
509-
ty::lookup_item_type(tcx, parent_id).ty);
502+
let class_ty = ty::subst(tcx, substs,
503+
ty::lookup_item_type(tcx, parent_id).ty);
510504
let llty = type_of_dtor(ccx, class_ty);
511505

512506
get_extern_fn(&mut *ccx.externs.borrow_mut(), ccx.llmod, name,
@@ -670,7 +664,7 @@ pub fn iter_structural_ty<'r,
670664
repr: &adt::Repr,
671665
av: ValueRef,
672666
variant: &ty::VariantInfo,
673-
tps: &[ty::t],
667+
substs: &ty::substs,
674668
f: val_and_ty_fn<'r,'b>)
675669
-> &'b Block<'b> {
676670
let _icx = push_ctxt("iter_variant");
@@ -680,7 +674,7 @@ pub fn iter_structural_ty<'r,
680674
for (i, &arg) in variant.args.iter().enumerate() {
681675
cx = f(cx,
682676
adt::trans_field_ptr(cx, repr, av, variant.disr_val, i),
683-
ty::subst_tps(tcx, tps, None, arg));
677+
ty::subst(tcx, substs, arg));
684678
}
685679
return cx;
686680
}
@@ -722,7 +716,7 @@ pub fn iter_structural_ty<'r,
722716
match adt::trans_switch(cx, &*repr, av) {
723717
(_match::single, None) => {
724718
cx = iter_variant(cx, &*repr, av, &**variants.get(0),
725-
substs.tps.as_slice(), f);
719+
substs, f);
726720
}
727721
(_match::switch, Some(lldiscrim_a)) => {
728722
cx = f(cx, lldiscrim_a, ty::mk_int());
@@ -748,7 +742,7 @@ pub fn iter_structural_ty<'r,
748742
&*repr,
749743
av,
750744
&**variant,
751-
substs.tps.as_slice(),
745+
substs,
752746
|x,y,z| f(x,y,z));
753747
Br(variant_cx, next_cx.llbb);
754748
}
@@ -1153,15 +1147,7 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
11531147
},
11541148
id, param_substs.map(|s| s.repr(ccx.tcx())));
11551149

1156-
let substd_output_type = match param_substs {
1157-
None => output_type,
1158-
Some(substs) => {
1159-
ty::subst_tps(ccx.tcx(),
1160-
substs.tys.as_slice(),
1161-
substs.self_ty,
1162-
output_type)
1163-
}
1164-
};
1150+
let substd_output_type = output_type.substp(ccx.tcx(), param_substs);
11651151
let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type);
11661152
let debug_context = debuginfo::create_function_debug_context(ccx, id, param_substs, llfndecl);
11671153

@@ -1213,15 +1199,7 @@ pub fn init_function<'a>(fcx: &'a FunctionContext<'a>,
12131199

12141200
// This shouldn't need to recompute the return type,
12151201
// as new_fn_ctxt did it already.
1216-
let substd_output_type = match fcx.param_substs {
1217-
None => output_type,
1218-
Some(substs) => {
1219-
ty::subst_tps(fcx.ccx.tcx(),
1220-
substs.tys.as_slice(),
1221-
substs.self_ty,
1222-
output_type)
1223-
}
1224-
};
1202+
let substd_output_type = output_type.substp(fcx.ccx.tcx(), fcx.param_substs);
12251203

12261204
if !return_type_is_void(fcx.ccx, substd_output_type) {
12271205
// If the function returns nil/bot, there is no real return
@@ -1508,18 +1486,8 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
15081486
disr: ty::Disr,
15091487
param_substs: Option<&param_substs>,
15101488
llfndecl: ValueRef) {
1511-
let ctor_ty = {
1512-
let no_substs: &[ty::t] = [];
1513-
let ty_param_substs: &[ty::t] = match param_substs {
1514-
Some(substs) => substs.tys.as_slice(),
1515-
None => no_substs
1516-
};
1517-
1518-
ty::subst_tps(ccx.tcx(),
1519-
ty_param_substs,
1520-
None,
1521-
ty::node_id_to_type(ccx.tcx(), ctor_id))
1522-
};
1489+
let ctor_ty = ty::node_id_to_type(ccx.tcx(), ctor_id);
1490+
let ctor_ty = ctor_ty.substp(ccx.tcx(), param_substs);
15231491

15241492
let result_ty = match ty::get(ctor_ty).sty {
15251493
ty::ty_bare_fn(ref bft) => bft.sig.output,

0 commit comments

Comments
 (0)