diff options
| author | Haitao Li <lihaitao@gmail.com> | 2011-11-15 22:30:03 -0600 |
|---|---|---|
| committer | Haitao Li <lihaitao@gmail.com> | 2011-11-15 22:31:33 +0800 |
| commit | eae478cd74e9d80e2e9d2557c53e7ad2b9090240 (patch) | |
| tree | d19fdcc70d53ef3d59b609593a0e385d356c0669 | |
| parent | 0898d441cb42e09092be7772f4c24fd10e36fa72 (diff) | |
Use native fn's link name attribute if given
Fixes issue #905
| -rw-r--r-- | src/comp/middle/trans.rs | 10 | ||||
| -rw-r--r-- | src/test/run-pass/native-fn-linkname.rs | 19 |
2 files changed, 27 insertions, 2 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 36393213fb3..95ea4f8e067 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -5805,9 +5805,15 @@ fn item_path(item: @ast::item) -> [str] { ret [item.ident]; } fn collect_native_item(ccx: @crate_ctxt, i: @ast::native_item, &&pt: [str], _v: vt<[str]>) { alt i.node { - ast::native_item_fn(_, _, _) { + ast::native_item_fn(link_name, _, _) { if !ccx.obj_methods.contains_key(i.id) { - register_native_fn(ccx, i.span, pt, i.ident, i.id); + let name = + if option::is_some(link_name) { + option::get(link_name) + } else { + i.ident + }; + register_native_fn(ccx, i.span, pt, name, i.id); } } _ { } diff --git a/src/test/run-pass/native-fn-linkname.rs b/src/test/run-pass/native-fn-linkname.rs new file mode 100644 index 00000000000..a43cab54f35 --- /dev/null +++ b/src/test/run-pass/native-fn-linkname.rs @@ -0,0 +1,19 @@ +use std; + +import std::vec; +import std::str; + +native "cdecl" mod libc = "" { + fn my_strlen(str: *u8) -> uint = "strlen"; +} + +fn strlen(str: str) -> uint unsafe { + // C string is terminated with a zero + let bytes = str::bytes(str) + [0u8]; + ret libc::my_strlen(vec::unsafe::to_ptr(bytes)); +} + +fn main(_args: [str]) { + let len = strlen("Rust"); + assert(len == 4u); +} |
