Skip to content

Commit bc5eb7d

Browse files
committed
Stop passing around Option<&substs> in trans and just pass &substs, making the code more regular
1 parent 0f03b56 commit bc5eb7d

File tree

14 files changed

+129
-185
lines changed

14 files changed

+129
-185
lines changed

src/librustc/middle/subst.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ impl Substs {
6363
}
6464
}
6565

66+
pub fn trans_empty() -> Substs {
67+
Substs {
68+
self_ty: None,
69+
tps: Vec::new(),
70+
regions: ErasedRegions
71+
}
72+
}
73+
6674
pub fn is_noop(&self) -> bool {
6775
let regions_is_noop = match self.regions {
6876
ErasedRegions => false, // may be used to canonicalize

src/librustc/middle/trans/base.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,19 +1098,19 @@ pub fn new_fn_ctxt<'a>(ccx: &'a CrateContext,
10981098
id: ast::NodeId,
10991099
has_env: bool,
11001100
output_type: ty::t,
1101-
param_substs: Option<&'a param_substs>,
1101+
param_substs: &'a param_substs,
11021102
sp: Option<Span>,
11031103
block_arena: &'a TypedArena<Block<'a>>)
11041104
-> FunctionContext<'a> {
1105-
for p in param_substs.iter() { p.validate(); }
1105+
param_substs.validate();
11061106

11071107
debug!("new_fn_ctxt(path={}, id={}, param_substs={})",
11081108
if id == -1 {
11091109
"".to_string()
11101110
} else {
11111111
ccx.tcx.map.path_to_str(id).to_string()
11121112
},
1113-
id, param_substs.map(|s| s.repr(ccx.tcx())));
1113+
id, param_substs.repr(ccx.tcx()));
11141114

11151115
let substd_output_type = output_type.substp(ccx.tcx(), param_substs);
11161116
let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type);
@@ -1303,7 +1303,7 @@ pub fn trans_closure(ccx: &CrateContext,
13031303
decl: &ast::FnDecl,
13041304
body: &ast::Block,
13051305
llfndecl: ValueRef,
1306-
param_substs: Option<&param_substs>,
1306+
param_substs: &param_substs,
13071307
id: ast::NodeId,
13081308
_attributes: &[ast::Attribute],
13091309
output_type: ty::t,
@@ -1314,7 +1314,7 @@ pub fn trans_closure(ccx: &CrateContext,
13141314
set_uwtable(llfndecl);
13151315

13161316
debug!("trans_closure(..., param_substs={})",
1317-
param_substs.map(|s| s.repr(ccx.tcx())));
1317+
param_substs.repr(ccx.tcx()));
13181318

13191319
let has_env = match ty::get(ty::node_id_to_type(ccx.tcx(), id)).sty {
13201320
ty::ty_closure(_) => true,
@@ -1327,7 +1327,7 @@ pub fn trans_closure(ccx: &CrateContext,
13271327
id,
13281328
has_env,
13291329
output_type,
1330-
param_substs.map(|s| &*s),
1330+
param_substs,
13311331
Some(body.span),
13321332
&arena);
13331333
init_function(&fcx, false, output_type);
@@ -1403,11 +1403,11 @@ pub fn trans_fn(ccx: &CrateContext,
14031403
decl: &ast::FnDecl,
14041404
body: &ast::Block,
14051405
llfndecl: ValueRef,
1406-
param_substs: Option<&param_substs>,
1406+
param_substs: &param_substs,
14071407
id: ast::NodeId,
14081408
attrs: &[ast::Attribute]) {
14091409
let _s = StatRecorder::new(ccx, ccx.tcx.map.path_to_str(id).to_string());
1410-
debug!("trans_fn(param_substs={})", param_substs.map(|s| s.repr(ccx.tcx())));
1410+
debug!("trans_fn(param_substs={})", param_substs.repr(ccx.tcx()));
14111411
let _icx = push_ctxt("trans_fn");
14121412
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx(), id));
14131413
trans_closure(ccx, decl, body, llfndecl,
@@ -1419,7 +1419,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
14191419
variant: &ast::Variant,
14201420
_args: &[ast::VariantArg],
14211421
disr: ty::Disr,
1422-
param_substs: Option<&param_substs>,
1422+
param_substs: &param_substs,
14231423
llfndecl: ValueRef) {
14241424
let _icx = push_ctxt("trans_enum_variant");
14251425

@@ -1434,7 +1434,7 @@ pub fn trans_enum_variant(ccx: &CrateContext,
14341434
pub fn trans_tuple_struct(ccx: &CrateContext,
14351435
_fields: &[ast::StructField],
14361436
ctor_id: ast::NodeId,
1437-
param_substs: Option<&param_substs>,
1437+
param_substs: &param_substs,
14381438
llfndecl: ValueRef) {
14391439
let _icx = push_ctxt("trans_tuple_struct");
14401440

@@ -1449,7 +1449,7 @@ pub fn trans_tuple_struct(ccx: &CrateContext,
14491449
fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
14501450
ctor_id: ast::NodeId,
14511451
disr: ty::Disr,
1452-
param_substs: Option<&param_substs>,
1452+
param_substs: &param_substs,
14531453
llfndecl: ValueRef) {
14541454
let ctor_ty = ty::node_id_to_type(ccx.tcx(), ctor_id);
14551455
let ctor_ty = ctor_ty.substp(ccx.tcx(), param_substs);
@@ -1464,7 +1464,7 @@ fn trans_enum_variant_or_tuple_like_struct(ccx: &CrateContext,
14641464

14651465
let arena = TypedArena::new();
14661466
let fcx = new_fn_ctxt(ccx, llfndecl, ctor_id, false, result_ty,
1467-
param_substs.map(|s| &*s), None, &arena);
1467+
param_substs, None, &arena);
14681468
init_function(&fcx, false, result_ty);
14691469

14701470
let arg_tys = ty::ty_fn_args(ctor_ty);
@@ -1500,7 +1500,7 @@ fn trans_enum_def(ccx: &CrateContext, enum_definition: &ast::EnumDef,
15001500
ast::TupleVariantKind(ref args) if args.len() > 0 => {
15011501
let llfn = get_item_val(ccx, variant.node.id);
15021502
trans_enum_variant(ccx, id, variant, args.as_slice(),
1503-
disr_val, None, llfn);
1503+
disr_val, &param_substs::empty(), llfn);
15041504
}
15051505
ast::TupleVariantKind(_) => {
15061506
// Nothing to do.
@@ -1587,7 +1587,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
15871587
decl,
15881588
body,
15891589
llfn,
1590-
None,
1590+
&param_substs::empty(),
15911591
item.id,
15921592
item.attrs.as_slice());
15931593
} else {
@@ -1660,7 +1660,7 @@ pub fn trans_struct_def(ccx: &CrateContext, struct_def: @ast::StructDef) {
16601660
Some(ctor_id) if struct_def.fields.len() > 0 => {
16611661
let llfndecl = get_item_val(ccx, ctor_id);
16621662
trans_tuple_struct(ccx, struct_def.fields.as_slice(),
1663-
ctor_id, None, llfndecl);
1663+
ctor_id, &param_substs::empty(), llfndecl);
16641664
}
16651665
Some(_) | None => {}
16661666
}

src/librustc/middle/trans/callee.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn trans_fn_ref_with_vtables_to_callee<'a>(bcx: &'a Block<'a>,
187187
def_id: ast::DefId,
188188
ref_id: ast::NodeId,
189189
substs: subst::Substs,
190-
vtables: Option<typeck::vtable_res>)
190+
vtables: typeck::vtable_res)
191191
-> Callee<'a> {
192192
Callee {bcx: bcx,
193193
data: Fn(trans_fn_ref_with_vtables(bcx, def_id, ExprId(ref_id),
@@ -198,8 +198,9 @@ fn resolve_default_method_vtables(bcx: &Block,
198198
impl_id: ast::DefId,
199199
method: &ty::Method,
200200
substs: &subst::Substs,
201-
impl_vtables: Option<typeck::vtable_res>)
202-
-> (typeck::vtable_res, typeck::vtable_param_res) {
201+
impl_vtables: typeck::vtable_res)
202+
-> (typeck::vtable_res, typeck::vtable_param_res)
203+
{
203204

204205
// Get the vtables that the impl implements the trait at
205206
let impl_res = ty::lookup_impl_vtables(bcx.tcx(), impl_id);
@@ -213,27 +214,15 @@ fn resolve_default_method_vtables(bcx: &Block,
213214
};
214215

215216
let mut param_vtables = resolve_vtables_under_param_substs(
216-
bcx.tcx(), Some(&param_substs), impl_res.trait_vtables.as_slice());
217+
bcx.tcx(), &param_substs, impl_res.trait_vtables.as_slice());
217218

218219
// Now we pull any vtables for parameters on the actual method.
219220
let num_method_vtables = method.generics.type_param_defs().len();
220-
match impl_vtables {
221-
Some(ref vtables) => {
222-
let num_impl_type_parameters =
223-
vtables.len() - num_method_vtables;
224-
param_vtables.push_all(vtables.tailn(num_impl_type_parameters))
225-
},
226-
None => {
227-
param_vtables.extend(range(0, num_method_vtables).map(
228-
|_| -> typeck::vtable_param_res {
229-
Vec::new()
230-
}
231-
))
232-
}
233-
}
221+
let num_impl_type_parameters = impl_vtables.len() - num_method_vtables;
222+
param_vtables.push_all(impl_vtables.tailn(num_impl_type_parameters));
234223

235224
let self_vtables = resolve_param_vtables_under_param_substs(
236-
bcx.tcx(), Some(&param_substs), impl_res.self_vtables.as_slice());
225+
bcx.tcx(), &param_substs, impl_res.self_vtables.as_slice());
237226

238227
(param_vtables, self_vtables)
239228
}
@@ -244,7 +233,7 @@ pub fn trans_fn_ref_with_vtables(
244233
def_id: ast::DefId, // def id of fn
245234
node: ExprOrMethodCall, // node id of use of fn; may be zero if N/A
246235
substs: subst::Substs, // values for fn's ty params
247-
vtables: Option<typeck::vtable_res>) // vtables for the call
236+
vtables: typeck::vtable_res) // vtables for the call
248237
-> ValueRef {
249238
/*!
250239
* Translates a reference to a fn/method item, monomorphizing and
@@ -336,7 +325,7 @@ pub fn trans_fn_ref_with_vtables(
336325
self_vtables.repr(tcx), param_vtables.repr(tcx));
337326

338327
(true, source_id,
339-
new_substs, Some(self_vtables), Some(param_vtables))
328+
new_substs, Some(self_vtables), param_vtables)
340329
}
341330
};
342331

@@ -507,7 +496,7 @@ pub fn trans_lang_call<'a>(
507496
did,
508497
0,
509498
subst::Substs::empty(),
510-
None)
499+
Vec::new())
511500
},
512501
ArgVals(args),
513502
dest)

src/librustc/middle/trans/closure.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ pub fn get_wrapper_for_bare_fn(ccx: &CrateContext,
421421
let _icx = push_ctxt("closure::get_wrapper_for_bare_fn");
422422

423423
let arena = TypedArena::new();
424-
let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output, None, None, &arena);
424+
let empty_param_substs = param_substs::empty();
425+
let fcx = new_fn_ctxt(ccx, llfn, -1, true, f.sig.output,
426+
&empty_param_substs, None, &arena);
425427
init_function(&fcx, true, f.sig.output);
426428
let bcx = fcx.entry_bcx.borrow().clone().unwrap();
427429

src/librustc/middle/trans/common.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,19 @@ pub type ExternMap = HashMap<String, ValueRef>;
180180
// will only be set in the case of default methods.
181181
pub struct param_substs {
182182
pub substs: subst::Substs,
183-
pub vtables: Option<typeck::vtable_res>,
183+
pub vtables: typeck::vtable_res,
184184
pub self_vtables: Option<typeck::vtable_param_res>
185185
}
186186

187187
impl param_substs {
188+
pub fn empty() -> param_substs {
189+
param_substs {
190+
substs: subst::Substs::trans_empty(),
191+
vtables: Vec::new(),
192+
self_vtables: None
193+
}
194+
}
195+
188196
pub fn validate(&self) {
189197
for t in self.substs.tps.iter() {
190198
assert!(!ty::type_needs_infer(*t));
@@ -206,21 +214,13 @@ impl Repr for param_substs {
206214
}
207215

208216
pub trait SubstP {
209-
fn substp(&self, tcx: &ty::ctxt, param_substs: Option<&param_substs>)
217+
fn substp(&self, tcx: &ty::ctxt, param_substs: &param_substs)
210218
-> Self;
211219
}
212220

213221
impl<T:Subst+Clone> SubstP for T {
214-
fn substp(&self, tcx: &ty::ctxt, param_substs: Option<&param_substs>)
215-
-> T {
216-
match param_substs {
217-
Some(substs) => {
218-
self.subst(tcx, &substs.substs)
219-
}
220-
None => {
221-
(*self).clone()
222-
}
223-
}
222+
fn substp(&self, tcx: &ty::ctxt, substs: &param_substs) -> T {
223+
self.subst(tcx, &substs.substs)
224224
}
225225
}
226226

@@ -281,7 +281,7 @@ pub struct FunctionContext<'a> {
281281

282282
// If this function is being monomorphized, this contains the type
283283
// substitutions used.
284-
pub param_substs: Option<&'a param_substs>,
284+
pub param_substs: &'a param_substs,
285285

286286
// The source span and nesting context where this function comes from, for
287287
// error reporting and symbol generation.
@@ -697,16 +697,7 @@ pub fn is_null(val: ValueRef) -> bool {
697697
}
698698

699699
pub fn monomorphize_type(bcx: &Block, t: ty::t) -> ty::t {
700-
match bcx.fcx.param_substs {
701-
Some(ref substs) => {
702-
t.subst(bcx.tcx(), &substs.substs)
703-
}
704-
_ => {
705-
assert!(!ty::type_has_params(t));
706-
assert!(!ty::type_has_self(t));
707-
t
708-
}
709-
}
700+
t.subst(bcx.tcx(), &bcx.fcx.param_substs.substs)
710701
}
711702

712703
pub fn node_id_type(bcx: &Block, id: ast::NodeId) -> ty::t {
@@ -759,10 +750,10 @@ pub fn node_id_substs(bcx: &Block,
759750
}
760751

761752
pub fn node_vtables(bcx: &Block, id: typeck::MethodCall)
762-
-> Option<typeck::vtable_res> {
753+
-> typeck::vtable_res {
763754
bcx.tcx().vtable_map.borrow().find(&id).map(|vts| {
764755
resolve_vtables_in_fn_ctxt(bcx.fcx, vts.as_slice())
765-
})
756+
}).unwrap_or_else(|| Vec::new())
766757
}
767758

768759
// Apply the typaram substitutions in the FunctionContext to some
@@ -776,7 +767,7 @@ pub fn resolve_vtables_in_fn_ctxt(fcx: &FunctionContext,
776767
}
777768

778769
pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,
779-
param_substs: Option<&param_substs>,
770+
param_substs: &param_substs,
780771
vts: &[typeck::vtable_param_res])
781772
-> typeck::vtable_res {
782773
vts.iter().map(|ds| {
@@ -788,7 +779,7 @@ pub fn resolve_vtables_under_param_substs(tcx: &ty::ctxt,
788779

789780
pub fn resolve_param_vtables_under_param_substs(
790781
tcx: &ty::ctxt,
791-
param_substs: Option<&param_substs>,
782+
param_substs: &param_substs,
792783
ds: &[typeck::vtable_origin])
793784
-> typeck::vtable_param_res {
794785
ds.iter().map(|d| {
@@ -801,7 +792,7 @@ pub fn resolve_param_vtables_under_param_substs(
801792

802793

803794
pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt,
804-
param_substs: Option<&param_substs>,
795+
param_substs: &param_substs,
805796
vt: &typeck::vtable_origin)
806797
-> typeck::vtable_origin {
807798
match *vt {
@@ -812,16 +803,7 @@ pub fn resolve_vtable_under_param_substs(tcx: &ty::ctxt,
812803
resolve_vtables_under_param_substs(tcx, param_substs, sub.as_slice()))
813804
}
814805
typeck::vtable_param(n_param, n_bound) => {
815-
match param_substs {
816-
Some(substs) => {
817-
find_vtable(tcx, substs, n_param, n_bound)
818-
}
819-
_ => {
820-
tcx.sess.bug(format!(
821-
"resolve_vtable_under_param_substs: asked to lookup \
822-
but no vtables in the fn_ctxt!").as_slice())
823-
}
824-
}
806+
find_vtable(tcx, param_substs, n_param, n_bound)
825807
}
826808
}
827809
}
@@ -837,9 +819,7 @@ pub fn find_vtable(tcx: &ty::ctxt,
837819
let param_bounds = match n_param {
838820
typeck::param_self => ps.self_vtables.as_ref().expect("self vtables missing"),
839821
typeck::param_numbered(n) => {
840-
let tables = ps.vtables.as_ref()
841-
.expect("vtables missing where they are needed");
842-
tables.get(n)
822+
ps.vtables.get(n)
843823
}
844824
};
845825
param_bounds.get(n_bound).clone()

0 commit comments

Comments
 (0)