Skip to content

Commit 25c2be0

Browse files
committed
Forbid boxed ifaces with self types or generic methods in bounded params
They are a soundness hole. Closes #1994
1 parent 484469c commit 25c2be0

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,12 +2668,12 @@ fn invoke_(bcx: block, llfn: ValueRef, llargs: [ValueRef],
26682668
// cleanups to run
26692669
if bcx.unreachable { ret bcx; }
26702670
let normal_bcx = sub_block(bcx, "normal return");
2671-
/*std::io::println("fn: " + lib::llvm::type_to_str(bcx.ccx().tn,
2672-
val_ty(llfn)));
2671+
/*io::println("fn: " + lib::llvm::type_to_str(bcx.ccx().tn,
2672+
val_ty(llfn)));
26732673
for a in llargs {
2674-
std::io::println(" a: " + lib::llvm::type_to_str(bcx.ccx().tn,
2675-
val_ty(a)));
2676-
}*/
2674+
io::println(" a: " + lib::llvm::type_to_str(bcx.ccx().tn,
2675+
val_ty(a)));
2676+
}// */
26772677
invoker(bcx, llfn, llargs, normal_bcx.llbb, get_landing_pad(bcx));
26782678
ret normal_bcx;
26792679
}

src/rustc/middle/trans/impl.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ fn trans_monomorphized_callee(bcx: block, callee_id: ast::node_id,
115115
let ty_substs = impl_substs +
116116
vec::tailn(node_substs, node_substs.len() - n_m_tps);
117117
let {bcx, val} = trans_self_arg(bcx, base);
118-
{env: self_env(val, node_id_type(bcx, base.id), none)
119-
with lval_static_fn_inner(bcx, mth_id, callee_id, ty_substs,
120-
some(sub_origins))}
118+
let lval = lval_static_fn_inner(bcx, mth_id, callee_id, ty_substs,
119+
some(sub_origins));
120+
{env: self_env(val, node_id_type(bcx, base.id), none),
121+
val: PointerCast(bcx, lval.val, T_ptr(type_of_fn_from_ty(
122+
ccx, node_id_type(bcx, callee_id))))
123+
with lval}
121124
}
122125
typeck::vtable_iface(iid, tps) {
123126
trans_iface_callee(bcx, base, callee_id, n_method)
@@ -236,7 +239,7 @@ fn make_impl_vtable(ccx: @crate_ctxt, impl_id: ast::def_id, substs: [ty::t],
236239
let fty = ty::substitute_type_params(tcx, substs,
237240
ty::mk_fn(tcx, im.fty));
238241
if (*im.tps).len() > 0u || ty::type_has_vars(fty) {
239-
C_null(type_of_fn_from_ty(ccx, fty))
242+
C_null(T_ptr(T_nil()))
240243
} else {
241244
let m_id = method_with_name(ccx, impl_id, im.ident);
242245
if has_tps {

src/rustc/middle/typeck.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ fn lookup_method(fcx: @fn_ctxt, expr: @ast::expr, node_id: ast::node_id,
17821782
some({method_ty: fty, n_tps: method_n_tps, substs, origin, self_sub}) {
17831783
let tcx = fcx.ccx.tcx;
17841784
let substs = substs, n_tps = vec::len(substs), n_tys = vec::len(tps);
1785-
let has_self = ty::type_has_params(fty);
1785+
let has_self = ty::type_has_vars(fty);
17861786
if method_n_tps + n_tps > 0u {
17871787
if n_tys == 0u || n_tys != method_n_tps {
17881788
if n_tys != 0u {
@@ -3335,15 +3335,16 @@ mod vtable {
33353335
}
33363336

33373337
fn lookup_vtables(fcx: @fn_ctxt, isc: resolve::iscopes, sp: span,
3338-
bounds: @[ty::param_bounds], tys: [ty::t])
3339-
-> vtable_res {
3338+
bounds: @[ty::param_bounds], tys: [ty::t],
3339+
allow_unsafe: bool) -> vtable_res {
33403340
let tcx = fcx.ccx.tcx, result = [], i = 0u;
33413341
for ty in tys {
33423342
for bound in *bounds[i] {
33433343
alt bound {
33443344
ty::bound_iface(i_ty) {
33453345
let i_ty = ty::substitute_type_params(tcx, tys, i_ty);
3346-
result += [lookup_vtable(fcx, isc, sp, ty, i_ty)];
3346+
result += [lookup_vtable(fcx, isc, sp, ty, i_ty,
3347+
allow_unsafe)];
33473348
}
33483349
_ {}
33493350
}
@@ -3354,7 +3355,8 @@ mod vtable {
33543355
}
33553356

33563357
fn lookup_vtable(fcx: @fn_ctxt, isc: resolve::iscopes, sp: span,
3357-
ty: ty::t, iface_ty: ty::t) -> vtable_origin {
3358+
ty: ty::t, iface_ty: ty::t, allow_unsafe: bool)
3359+
-> vtable_origin {
33583360
let tcx = fcx.ccx.tcx;
33593361
let (iface_id, iface_tps) = alt check ty::get(iface_ty).struct {
33603362
ty::ty_iface(did, tps) { (did, tps) }
@@ -3378,6 +3380,20 @@ mod vtable {
33783380
}
33793381
}
33803382
ty::ty_iface(did, tps) if iface_id == did {
3383+
if !allow_unsafe {
3384+
for m in *ty::iface_methods(tcx, did) {
3385+
if ty::type_has_vars(ty::mk_fn(tcx, m.fty)) {
3386+
tcx.sess.span_err(
3387+
sp, "a boxed iface with self types may not be \
3388+
passed as a bounded type");
3389+
} else if (*m.tps).len() > 0u {
3390+
tcx.sess.span_err(
3391+
sp, "a boxed iface with generic methods may not \
3392+
be passed as a bounded type");
3393+
3394+
}
3395+
}
3396+
}
33813397
ret vtable_iface(did, tps);
33823398
}
33833399
_ {
@@ -3410,8 +3426,8 @@ mod vtable {
34103426
im.did);
34113427
let params = vec::map(vars, {|t|
34123428
fixup_ty(fcx, sp, t)});
3413-
let subres = lookup_vtables(fcx, isc, sp,
3414-
im_bs, params);
3429+
let subres = lookup_vtables(
3430+
fcx, isc, sp, im_bs, params, false);
34153431
found = some(vtable_static(im.did, params,
34163432
subres));
34173433
}
@@ -3469,7 +3485,7 @@ mod vtable {
34693485
if has_iface_bounds(*item_ty.bounds) {
34703486
let impls = cx.impl_map.get(ex.id);
34713487
cx.vtable_map.insert(ex.id, lookup_vtables(
3472-
fcx, impls, ex.span, item_ty.bounds, ts));
3488+
fcx, impls, ex.span, item_ty.bounds, ts, false));
34733489
}
34743490
}
34753491
_ {}
@@ -3490,7 +3506,7 @@ mod vtable {
34903506
let ts = ty::node_id_to_type_params(cx.tcx, callee_id);
34913507
let iscs = cx.impl_map.get(ex.id);
34923508
cx.vtable_map.insert(callee_id, lookup_vtables(
3493-
fcx, iscs, ex.span, bounds, ts));
3509+
fcx, iscs, ex.span, bounds, ts, false));
34943510
}
34953511
}
34963512
_ {}
@@ -3502,7 +3518,8 @@ mod vtable {
35023518
ty::ty_iface(_, _) {
35033519
let impls = cx.impl_map.get(ex.id);
35043520
let vtable = lookup_vtable(fcx, impls, ex.span,
3505-
expr_ty(cx.tcx, src), target_ty);
3521+
expr_ty(cx.tcx, src), target_ty,
3522+
true);
35063523
cx.vtable_map.insert(ex.id, @[vtable]);
35073524
}
35083525
_ {}

0 commit comments

Comments
 (0)