Skip to content

Commit dd26ffd

Browse files
committed
implement link-arg as an attribute
1 parent ce4727f commit dd26ffd

File tree

19 files changed

+117
-18
lines changed

19 files changed

+117
-18
lines changed

compiler/rustc_feature/src/unstable.rs

+3
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,9 @@ declare_features! (
500500
(incomplete, lazy_type_alias, "1.72.0", Some(112792), None),
501501
/// Allows `if/while p && let q = r && ...` chains.
502502
(unstable, let_chains, "1.37.0", Some(53667), None),
503+
/// Allows using `#[link(kind = "link-arg", name = "...")]`
504+
/// to pass custom arguments to the linker.
505+
(unstable, link_arg_attribute, "CURRENT_RUSTC_VERSION", Some(99427), None),
503506
/// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
504507
(unstable, lint_reasons, "1.31.0", Some(54503), None),
505508
/// Give access to additional metadata about declarative macro meta-variables.

compiler/rustc_metadata/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ metadata_unknown_import_name_type =
269269
unknown import name type `{$import_name_type}`, expected one of: decorated, noprefix, undecorated
270270
271271
metadata_unknown_link_kind =
272-
unknown link kind `{$kind}`, expected one of: static, dylib, framework, raw-dylib
272+
unknown link kind `{$kind}`, expected one of: static, dylib, framework, raw-dylib, link-arg
273273
.label = unknown link kind
274274
275275
metadata_unknown_link_modifier =

compiler/rustc_metadata/src/native_libs.rs

+12
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ impl<'tcx> Collector<'tcx> {
160160
}
161161
NativeLibKind::RawDylib
162162
}
163+
"link-arg" => {
164+
if !features.link_arg_attribute {
165+
feature_err(
166+
&sess.parse_sess,
167+
sym::link_arg_attribute,
168+
item.span(),
169+
"link-arg link kind is unstable",
170+
)
171+
.emit();
172+
}
173+
NativeLibKind::LinkArg
174+
}
163175
kind => {
164176
sess.emit_err(errors::UnknownLinkKind { span, kind });
165177
continue;

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ symbols! {
950950
likely,
951951
line,
952952
link,
953+
link_arg_attribute,
953954
link_args,
954955
link_cfg,
955956
link_llvm_intrinsics,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# only-linux
2+
3+
include ../tools.mk
4+
5+
all:
6+
$(RUSTC) no_verbatim.rs -Z unstable-options -C linker-flavor=gnu-cc --print link-args | $(CGREP) -e 'l1.*-Wl,a1.*l2.*-Wl,a2.*d1.*-Wl,a3'
7+
$(RUSTC) verbatim.rs -Z unstable-options -C linker-flavor=gnu-cc --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*-Wl,a2.*d1.*-Wl,a3'
8+
$(RUSTC) no_verbatim.rs -C linker-flavor=ld --print link-args | $(CGREP) -e 'l1.*"a1".*l2.*"a2".*d1.*"a3"'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "l1")]
4+
#[link(kind = "link-arg", name = "a1")]
5+
#[link(kind = "static", name = "l2")]
6+
#[link(kind = "link-arg", name = "a2")]
7+
#[link(kind = "dylib", name = "d1")]
8+
#[link(kind = "link-arg", name = "a3")]
9+
extern "C" {}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "l1")]
4+
#[link(kind = "link-arg", name = "a1", modifiers = "+verbatim")]
5+
#[link(kind = "static", name = "l2")]
6+
#[link(kind = "link-arg", name = "a2")]
7+
#[link(kind = "dylib", name = "d1")]
8+
#[link(kind = "link-arg", name = "a3")]
9+
extern "C" {}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include ../tools.mk
2+
3+
all:
4+
# Build deps
5+
$(RUSTC) native_dep_1.rs --crate-type=staticlib
6+
$(RUSTC) native_dep_2.rs --crate-type=staticlib
7+
$(RUSTC) rust_dep.rs --crate-type=lib
8+
9+
# Check sequence of linker args
10+
$(RUSTC) main.rs --extern lib=$(TMPDIR)/librust_dep.rlib --crate-type=bin --print link-args | $(CGREP) -e 'native_dep_1.*some_flag.*native_dep_2'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
lib::f();
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn f1() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn f2() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "native_dep_1", modifiers = "-bundle")]
4+
#[link(kind = "link-arg", name = "some_flag")]
5+
#[link(kind = "static", name = "native_dep_2", modifiers = "-bundle")]
6+
extern "C" {
7+
pub fn foo();
8+
}
9+
10+
pub fn f() {
11+
unsafe {
12+
foo();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include ../tools.mk
2+
3+
all:
4+
$(RUSTC) rs.rs --print link-args | $(CGREP) -e 'l1.*a1.*l2.*a2.*d1.*a3'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(link_arg_attribute)]
2+
3+
#[link(kind = "static", name = "l1")]
4+
#[link(kind = "link-arg", name = "a1")]
5+
#[link(kind = "static", name = "l2")]
6+
#[link(kind = "link-arg", name = "a2")]
7+
#[link(kind = "dylib", name = "d1")]
8+
#[link(kind = "link-arg", name = "a3")]
9+
extern "C" {}
10+
11+
fn main() {}

tests/ui/error-codes/E0458.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0458]: unknown link kind `wonderful_unicorn`, expected one of: static, dylib, framework, raw-dylib
1+
error[E0458]: unknown link kind `wonderful_unicorn`, expected one of: static, dylib, framework, raw-dylib, link-arg
22
--> $DIR/E0458.rs:1:15
33
|
44
LL | #[link(kind = "wonderful_unicorn")] extern "C" {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[link(kind = "link-arg", name = "foo")]
2+
//~^ ERROR: is unstable
3+
extern "C" {}
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: link-arg link kind is unstable
2+
--> $DIR/feature-gate-link-arg-attribute.rs:1:8
3+
|
4+
LL | #[link(kind = "link-arg", name = "foo")]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #99427 <https://github.com/rust-lang/rust/issues/99427> for more information
8+
= help: add `#![feature(link_arg_attribute)]` to the crate attributes to enable
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// link-arg is not supposed to be usable in #[link] attributes
1+
#![feature(link_arg_attribute)]
22

33
// compile-flags:
4-
// error-pattern: error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib
4+
// error-pattern: linking modifier `bundle` is only compatible with `static` linking kind
55

6-
#[link(kind = "link-arg")]
6+
#[link(kind = "link-arg", name = "arg", modifiers = "+bundle")]
77
extern "C" {}
88
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
error[E0458]: unknown link kind `link-arg`, expected one of: static, dylib, framework, raw-dylib
2-
--> $DIR/link-arg-from-rs.rs:6:15
1+
error: linking modifier `bundle` is only compatible with `static` linking kind
2+
--> $DIR/link-arg-from-rs.rs:6:53
33
|
4-
LL | #[link(kind = "link-arg")]
5-
| ^^^^^^^^^^ unknown link kind
4+
LL | #[link(kind = "link-arg", name = "arg", modifiers = "+bundle")]
5+
| ^^^^^^^^^
66

7-
error[E0459]: `#[link]` attribute requires a `name = "string"` argument
8-
--> $DIR/link-arg-from-rs.rs:6:1
9-
|
10-
LL | #[link(kind = "link-arg")]
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `name` argument
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

15-
Some errors have detailed explanations: E0458, E0459.
16-
For more information about an error, try `rustc --explain E0458`.

0 commit comments

Comments
 (0)