Skip to content

Fix translation of unboxing shim for rust-call ABI methods #18113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/librustc/middle/trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,32 @@ pub fn trans_unboxing_shim(bcx: Block,
};
let boxed_function_type =
ty::mk_bare_fn(tcx, boxed_function_type).subst(tcx, &substs);
let function_type =
ty::mk_bare_fn(tcx, (*fty).clone()).subst(tcx, &substs);
let function_type = match fty.abi {
synabi::RustCall => {
// We're passing through to a RustCall ABI function, but
// because the shim will already perform untupling, we
// need to pretend the shimmed function does not use
// RustCall so the untupled arguments can be passed
// through verbatim. This is kind of ugly.
let fake_ty = ty::FnSig {
binder_id: fty.sig.binder_id,
inputs: type_of::untuple_arguments_if_necessary(ccx,
fty.sig.inputs.as_slice(),
fty.abi),
output: fty.sig.output,
variadic: false,
};
let fake_ty = ty::BareFnTy {
fn_style: fty.fn_style,
abi: synabi::Rust,
sig: fake_ty,
};
ty::mk_bare_fn(tcx, fake_ty).subst(tcx, &substs)
}
_ => {
ty::mk_bare_fn(tcx, (*fty).clone()).subst(tcx, &substs)
}
};

let function_name = ty::with_path(tcx, method_id, |path| {
link::mangle_internal_name_by_path_and_seq(path, "unboxing_shim")
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/trans/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ pub fn type_of_explicit_arg(ccx: &CrateContext, arg_ty: ty::t) -> Type {
/// Yields the types of the "real" arguments for this function. For most
/// functions, these are simply the types of the arguments. For functions with
/// the `RustCall` ABI, however, this untuples the arguments of the function.
fn untuple_arguments_if_necessary(ccx: &CrateContext,
inputs: &[ty::t],
abi: abi::Abi)
-> Vec<ty::t> {
pub fn untuple_arguments_if_necessary(ccx: &CrateContext,
inputs: &[ty::t],
abi: abi::Abi)
-> Vec<ty::t> {
if abi != abi::RustCall {
return inputs.iter().map(|x| (*x).clone()).collect()
}
Expand Down
42 changes: 42 additions & 0 deletions src/test/run-pass/issue-16739.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2014 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.

#![feature(unboxed_closures)]

// Test that unboxing shim for calling rust-call ABI methods through a
// trait box works and does not cause an ICE

struct Foo { foo: uint }

impl FnOnce<(), uint> for Foo {
#[rust_call_abi_hack]
fn call_once(self, _: ()) -> uint { self.foo }
}

impl FnOnce<(uint,), uint> for Foo {
#[rust_call_abi_hack]
fn call_once(self, (x,): (uint,)) -> uint { self.foo + x }
}

impl FnOnce<(uint, uint), uint> for Foo {
#[rust_call_abi_hack]
fn call_once(self, (x, y): (uint, uint)) -> uint { self.foo + x + y }
}

fn main() {
let f = box Foo { foo: 42 } as Box<FnOnce<(), uint>>;
assert_eq!(f.call_once(()), 42);

let f = box Foo { foo: 40 } as Box<FnOnce<(uint,), uint>>;
assert_eq!(f.call_once((2,)), 42);

let f = box Foo { foo: 40 } as Box<FnOnce<(uint, uint), uint>>;
assert_eq!(f.call_once((1, 1)), 42);
}