Fix and cleanup callee shims

This commit is contained in:
Mark Simulacrum 2016-12-18 22:24:42 -07:00
parent f9f1406eef
commit 6e3d8cda2c
3 changed files with 31 additions and 20 deletions

View File

@ -502,15 +502,15 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
let fcx = FunctionContext::new(ccx, llfn, fn_ty);
let bcx = fcx.get_entry_block();
let llargs = get_params(fcx.llfn);
let mut llargs = get_params(fcx.llfn);
let self_idx = fcx.fn_ty.ret.is_indirect() as usize;
let self_arg = llargs.remove(fcx.fn_ty.ret.is_indirect() as usize);
let llfnpointer = llfnpointer.unwrap_or_else(|| {
// the first argument (`self`) will be ptr to the fn pointer
if is_by_ref {
bcx.load(llargs[self_idx])
bcx.load(self_arg)
} else {
llargs[self_idx]
self_arg
}
});
@ -522,16 +522,7 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
let fn_ret = callee.ty.fn_ret();
let fn_ty = callee.direct_fn_type(ccx, &[]);
let mut args = Vec::new();
if fn_ty.ret.is_indirect() {
if !fn_ty.ret.is_ignore() {
args.push(get_param(fcx.llfn, 0));
}
}
args.extend_from_slice(&llargs[(self_idx + 1)..]);
let llret = bcx.call(llfnpointer, &args, None);
let llret = bcx.call(llfnpointer, &llargs, None);
fn_ty.apply_attrs_callsite(llret);
if fn_ret.0.is_never() {

View File

@ -12,7 +12,7 @@ use attributes;
use llvm::{ValueRef, get_params};
use rustc::traits;
use abi::FnType;
use callee::Callee;
use callee::{Callee, CalleeData};
use common::*;
use consts;
use declare;
@ -84,14 +84,20 @@ pub fn trans_object_shim<'a, 'tcx>(ccx: &'a CrateContext<'a, 'tcx>,
let fcx = FunctionContext::new(ccx, llfn, fn_ty);
let bcx = fcx.get_entry_block();
let llargs = get_params(fcx.llfn);
let mut llargs = get_params(fcx.llfn);
let fn_ret = callee.ty.fn_ret();
let fn_ty = callee.direct_fn_type(ccx, &[]);
let mut args = Vec::new();
args.extend_from_slice(&llargs);
let llret = bcx.call(callee.reify(ccx), &args, None);
let fn_ptr = match callee.data {
CalleeData::Virtual(idx) => {
let fn_ptr = get_virtual_method(&bcx,
llargs.remove(fn_ty.ret.is_indirect() as usize + 1), idx);
let llty = fn_ty.llvm_type(bcx.ccx()).ptr_to();
bcx.pointercast(fn_ptr, llty)
},
_ => bug!("trans_object_shim called with non-virtual callee"),
};
let llret = bcx.call(fn_ptr, &llargs, None);
fn_ty.apply_attrs_callsite(llret);
if fn_ret.0.is_never() {

View File

@ -0,0 +1,14 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
assert_eq!((ToString::to_string as fn(&(ToString+'static)) -> String)(&"foo"),
String::from("foo"));
}