Skip to content
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

issue 1296: implement #[nolink]; deprecate #[link_name = ""] #1309

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
4 changes: 2 additions & 2 deletions doc/tutorial/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ microsecond-resolution timer.
use std;
type timeval = {mutable tv_sec: u32,
mutable tv_usec: u32};
#[link_name = ""]
#[nolink]
native mod libc {
fn gettimeofday(tv: *timeval, tz: *()) -> i32;
}
Expand All @@ -199,7 +199,7 @@ microsecond-resolution timer.
ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
}

The `#[link_name = ""]` sets the name of the native module to the
The `#[nolink]` sets the name of the native module to the
empty string to prevent the rust compiler from trying to link it.
The standard C library is already linked with Rust programs.

Expand Down
29 changes: 21 additions & 8 deletions src/comp/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,31 @@ fn visit_item(e: env, i: @ast::item) {
}
either::left(msg) { e.sess.span_fatal(i.span, msg); }
}

let cstore = e.sess.get_cstore();
let native_name = i.ident;
alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
some(nn) { native_name = nn; }
none. { }
let already_added = false;
if vec::len(attr::find_attrs_by_name(i.attrs, "nolink")) == 0u {
alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
some(nn) { native_name = nn; }
none. { }
}
if native_name == "" {
e.sess.span_fatal(i.span,
"empty #[link_name] not allowed; use #[nolink].");
}
already_added = !cstore::add_used_library(cstore, native_name);
}
if !cstore::add_used_library(cstore, native_name) { ret; }
for a: ast::attribute in
attr::find_attrs_by_name(i.attrs, "link_args") {

let link_args = attr::find_attrs_by_name(i.attrs, "link_args");
if vec::len(link_args) > 0u && already_added {
e.sess.span_fatal(i.span, "library '" + native_name +
"' already added: can't specify link_args.");
}
for a: ast::attribute in link_args {
alt attr::get_meta_item_value_str(attr::attr_meta(a)) {
some(linkarg) { cstore::add_used_link_args(cstore, linkarg); }
some(linkarg) {
cstore::add_used_link_args(cstore, linkarg);
}
none. {/* fallthrough */ }
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/comp/metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ fn get_used_crate_files(cstore: cstore) -> [str] {
}

fn add_used_library(cstore: cstore, lib: str) -> bool {
if lib == "" { ret false; }
assert lib != "";

if vec::member(lib, p(cstore).used_libraries) { ret false; }

p(cstore).used_libraries += [lib];
ret true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/linux_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export fsync_fd;
// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
// by https://github.com/graydon/rust/issues#issue/268

#[link_name = ""]
#[link_name = ""] // FIXME remove after #[nolink] is snapshotted
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn read(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/macos_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export fsync_fd;
// FIXME Refactor into unix_os module or some such. Doesn't
// seem to work right now.

#[link_name = ""]
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn read(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
Expand Down Expand Up @@ -118,7 +118,8 @@ native mod rustrt {

fn getcwd() -> str { ret rustrt::rust_getcwd(); }

#[link_name = ""]
#[link_name = ""] // FIXME remove after #[nolink] is snapshotted
#[nolink]
#[abi = "cdecl"]
native mod mac_libc {
fn _NSGetExecutablePath(buf: str::sbuf,
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/win32_os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import core::option;
import ctypes::*;

#[abi = "cdecl"]
#[link_name = ""]
#[link_name = ""] // FIXME remove after #[nolink] is snapshotted
#[nolink]
native mod libc {
fn read(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
fn write(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/shootout-nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod llvm {
fn sqrt(n: float) -> float;
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/compile-fail/empty-linkname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// error-pattern:empty #[link_name] not allowed; use #[nolink].

#[link_name = ""]
native mod foo {
}
11 changes: 11 additions & 0 deletions src/test/compile-fail/nolink-with-link-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// error-pattern:aFdEfSeVEE

/* We're testing that link_args are indeed passed when nolink is specified.
So we try to compile with junk link_args and make sure they are visible in
the compiler output. */

#[link_args = "aFdEfSeVEEE"]
#[nolink]
native mod m1 { }

fn main() { }
17 changes: 17 additions & 0 deletions src/test/compile-fail/redundant-link-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// error-pattern:library 'm' already added: can't specify link_args.

/* I think it should undefined to have multiple modules that link in the same
library, but provide different link arguments. Unfortunately we don't track
link_args by module -- they are just appended as discovered into the crate
store -- but for now, it should be an error to provide link_args on a module
that's already been included (with or without link_args). */

#[link_name= "m"]
#[link_args="-foo"] // this could have been elided.
native mod m1 {
}

#[link_name= "m"]
#[link_args="-bar"] // this is the actual error trigger.
native mod m2 {
}
2 changes: 1 addition & 1 deletion src/test/run-pass/bind-native-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std;
import str;
import ctypes::*;

#[link_name = ""]
#[nolink]
native mod libc {
fn write(fd: c_int, buf: *u8, nbyte: size_t);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/binops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn test_fn() {
}

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod test {
fn do_gc();
fn unsupervise();
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/c-stack-returning-int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std;
import str;

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod libc {
fn atol(x: str::sbuf) -> int;
fn atoll(x: str::sbuf) -> i64;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/import-glob-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod a1 {
mod a2 {
// | | |
#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod b1 {
// | | |
import a1::b2::*;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/native-fn-linkname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std;
import vec;
import str;

#[link_name = ""]
#[nolink]
#[abi = "cdecl"]
native mod libc {
#[link_name = "strlen"]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/native-opaque-type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod libc {
type file_handle;
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/run-pass/native2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ native mod rustrt {
}

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod bar { }

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod zed { }

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod libc {
fn write(fd: int, buf: *u8, count: uint) -> int;
}

#[abi = "cdecl"]
#[link_name = ""]
#[nolink]
native mod baz { }

fn main(args: [str]) { }
2 changes: 1 addition & 1 deletion src/test/stdtest/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std;
import std::c_vec::*;
import ctypes::*;

#[link_name = ""]
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn malloc(n: size_t) -> *mutable u8;
Expand Down