Skip to content

Commit f14bc54

Browse files
gmfawcettbrson
authored andcommitted
allow #[link_args] with #[nolink]. For now, fail if two modules link same lib, and second has 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).
1 parent 7ddd353 commit f14bc54

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

src/comp/metadata/creader.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,28 @@ fn visit_item(e: env, i: @ast::item) {
6060

6161
let cstore = e.sess.get_cstore();
6262
let native_name = i.ident;
63-
if vec::len(attr::find_attrs_by_name(i.attrs, "nolink")) > 0u {
64-
ret;
65-
}
66-
alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
67-
some(nn) { native_name = nn; }
68-
none. { }
63+
let already_added = false;
64+
if vec::len(attr::find_attrs_by_name(i.attrs, "nolink")) == 0u {
65+
alt attr::get_meta_item_value_str_by_name(i.attrs, "link_name") {
66+
some(nn) { native_name = nn; }
67+
none. { }
68+
}
69+
if native_name == "" {
70+
e.sess.span_fatal(i.span,
71+
"empty #[link_name] not allowed; use #[nolink].");
72+
}
73+
already_added = !cstore::add_used_library(cstore, native_name);
6974
}
70-
if native_name == "" {
71-
e.sess.span_fatal(i.span,
72-
"empty #[link_name] not allowed; use #[nolink].");
75+
let link_args = attr::find_attrs_by_name(i.attrs, "link_args");
76+
if vec::len(link_args) > 0u && already_added {
77+
e.sess.span_fatal(i.span, "library '" + native_name +
78+
"' already added: can't specify link_args.");
7379
}
74-
if !cstore::add_used_library(cstore, native_name) { ret; }
75-
for a: ast::attribute in
76-
attr::find_attrs_by_name(i.attrs, "link_args") {
77-
80+
for a: ast::attribute in link_args {
7881
alt attr::get_meta_item_value_str(attr::attr_meta(a)) {
79-
some(linkarg) { cstore::add_used_link_args(cstore, linkarg); }
82+
some(linkarg) {
83+
cstore::add_used_link_args(cstore, linkarg);
84+
}
8085
none. {/* fallthrough */ }
8186
}
8287
}

src/comp/metadata/cstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn get_used_crate_files(cstore: cstore) -> [str] {
9090
}
9191

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

9595
if vec::member(lib, p(cstore).used_libraries) { ret false; }
9696
p(cstore).used_libraries += [lib];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// error-pattern:aFdEfSeVEE
2+
3+
/* We're testing that link_args are indeed passed when nolink is specified.
4+
So we try to compile with junk link_args and make sure they are visible in
5+
the compiler output. */
6+
7+
#[link_args = "aFdEfSeVEEE"]
8+
#[nolink]
9+
native mod m1 { }
10+
11+
fn main() { }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// error-pattern:library 'm' already added: can't specify link_args.
2+
3+
/* I think it should undefined to have multiple modules that link in the same
4+
library, but provide different link arguments. Unfortunately we don't track
5+
link_args by module -- they are just appended as discovered into the crate
6+
store -- but for now, it should be an error to provide link_args on a module
7+
that's already been included (with or without link_args). */
8+
9+
#[link_name= "m"]
10+
#[link_args="-foo"] // this could have been elided.
11+
native mod m1 {
12+
}
13+
14+
#[link_name= "m"]
15+
#[link_args="-bar"] // this is the actual error trigger.
16+
native mod m2 {
17+
}

0 commit comments

Comments
 (0)