Skip to content

Commit eae478c

Browse files
committed
Use native fn's link name attribute if given
Fixes issue #905
1 parent 0898d44 commit eae478c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/comp/middle/trans.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5805,9 +5805,15 @@ fn item_path(item: @ast::item) -> [str] { ret [item.ident]; }
58055805
fn collect_native_item(ccx: @crate_ctxt, i: @ast::native_item, &&pt: [str],
58065806
_v: vt<[str]>) {
58075807
alt i.node {
5808-
ast::native_item_fn(_, _, _) {
5808+
ast::native_item_fn(link_name, _, _) {
58095809
if !ccx.obj_methods.contains_key(i.id) {
5810-
register_native_fn(ccx, i.span, pt, i.ident, i.id);
5810+
let name =
5811+
if option::is_some(link_name) {
5812+
option::get(link_name)
5813+
} else {
5814+
i.ident
5815+
};
5816+
register_native_fn(ccx, i.span, pt, name, i.id);
58115817
}
58125818
}
58135819
_ { }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std;
2+
3+
import std::vec;
4+
import std::str;
5+
6+
native "cdecl" mod libc = "" {
7+
fn my_strlen(str: *u8) -> uint = "strlen";
8+
}
9+
10+
fn strlen(str: str) -> uint unsafe {
11+
// C string is terminated with a zero
12+
let bytes = str::bytes(str) + [0u8];
13+
ret libc::my_strlen(vec::unsafe::to_ptr(bytes));
14+
}
15+
16+
fn main(_args: [str]) {
17+
let len = strlen("Rust");
18+
assert(len == 4u);
19+
}

0 commit comments

Comments
 (0)