Skip to content

Commit a7f6e00

Browse files
jdmbrson
authored andcommitted
Fix metadata serialization of foreign functions. Properly take the value of foreign functions from other crates to fix #1840.
1 parent f3b50ae commit a7f6e00

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

src/rustc/metadata/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ fn item_to_def_like(item: ebml::doc, did: ast::def_id, cnum: ast::crate_num)
288288
'u' { dl_def(ast::def_fn(did, ast::unsafe_fn)) }
289289
'f' { dl_def(ast::def_fn(did, ast::impure_fn)) }
290290
'p' { dl_def(ast::def_fn(did, ast::pure_fn)) }
291+
'F' { dl_def(ast::def_fn(did, ast::extern_fn)) }
291292
'y' { dl_def(ast::def_ty(did)) }
292293
't' { dl_def(ast::def_ty(did)) }
293294
'm' { dl_def(ast::def_mod(did)) }

src/rustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ fn purity_fn_family(p: purity) -> char {
527527
unsafe_fn { 'u' }
528528
pure_fn { 'p' }
529529
impure_fn { 'f' }
530-
extern_fn { 'c' }
530+
extern_fn { 'F' }
531531
}
532532
}
533533

src/rustc/middle/trans/base.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,16 +2392,18 @@ fn lval_static_fn_inner(bcx: block, fn_id: ast::def_id, id: ast::node_id,
23922392
ccx, node_id_type(bcx, id))));
23932393
}
23942394

2395-
// FIXME: Need to support extern-ABI functions (#1840)
2396-
if fn_id.crate == ast::local_crate {
2397-
alt bcx.tcx().def_map.find(id) {
2398-
some(ast::def_fn(_, ast::extern_fn)) {
2395+
alt ty::get(tpt.ty).struct {
2396+
ty::ty_fn(fn_ty) {
2397+
alt fn_ty.purity {
2398+
ast::extern_fn {
23992399
// Extern functions are just opaque pointers
24002400
let val = PointerCast(bcx, val, T_ptr(T_i8()));
24012401
ret lval_no_env(bcx, val, owned_imm);
24022402
}
2403-
_ { }
2403+
_ { /* fall through */ }
24042404
}
2405+
}
2406+
_ { /* fall through */ }
24052407
}
24062408

24072409
ret {bcx: bcx, val: val, kind: owned, env: null_env};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[link(name = "externcallback",
2+
vers = "0.1")];
3+
4+
#[crate_type = "lib"];
5+
6+
extern mod rustrt {
7+
fn rust_dbg_call(cb: *u8,
8+
data: libc::uintptr_t) -> libc::uintptr_t;
9+
}
10+
11+
fn fact(n: uint) -> uint {
12+
#debug("n = %?", n);
13+
rustrt::rust_dbg_call(cb, n)
14+
}
15+
16+
extern fn cb(data: libc::uintptr_t) -> libc::uintptr_t {
17+
if data == 1u {
18+
data
19+
} else {
20+
fact(data - 1u) * data
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//aux-build:extern-crosscrate-source.rs
2+
3+
use externcallback(vers = "0.1");
4+
5+
fn fact(n: uint) -> uint {
6+
#debug("n = %?", n);
7+
externcallback::rustrt::rust_dbg_call(externcallback::cb, n)
8+
}
9+
10+
fn main() {
11+
let result = fact(10u);
12+
#debug("result = %?", result);
13+
assert result == 3628800u;
14+
}

0 commit comments

Comments
 (0)