]> git.lizzy.rs Git - rust.git/commitdiff
Use native fn's link name attribute if given
authorHaitao Li <lihaitao@gmail.com>
Wed, 16 Nov 2011 04:30:03 +0000 (22:30 -0600)
committerHaitao Li <lihaitao@gmail.com>
Tue, 15 Nov 2011 14:31:33 +0000 (22:31 +0800)
Fixes issue #905

src/comp/middle/trans.rs
src/test/run-pass/native-fn-linkname.rs [new file with mode: 0644]

index 36393213fb3103128cbf63e80118b858ec25adc0..95ea4f8e0679419336365bb738b29a9c30875a0a 100644 (file)
@@ -5805,9 +5805,15 @@ fn trans_simple_native_abi(bcx: @block_ctxt, name: str,
 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 (file)
index 0000000..a43cab5
--- /dev/null
@@ -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);
+}