Skip to content

Commit 92fa367

Browse files
committed
Add raw_dylib_macho feature gate
1 parent de44e81 commit 92fa367

File tree

9 files changed

+103
-20
lines changed

9 files changed

+103
-20
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ attr_parsing_raw_dylib_no_nul =
242242
attr_parsing_raw_dylib_elf_unstable =
243243
link kind `raw-dylib` is unstable on ELF platforms
244244
245+
attr_parsing_raw_dylib_macho_unstable =
246+
link kind `raw-dylib` is unstable on Mach-O platforms
247+
248+
attr_parsing_raw_dylib_macho_use_verbatim =
249+
link kind `raw-dylib` should use the `+verbatim` linkage modifier
250+
245251
attr_parsing_raw_dylib_only_windows =
246252
link kind `raw-dylib` is only supported on Windows targets
247253

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::fluent_generated;
1313
use crate::session_diagnostics::{
1414
AsNeededCompatibility, BundleNeedsStatic, EmptyLinkName, ImportNameTypeRaw, ImportNameTypeX86,
1515
IncompatibleWasmLink, InvalidLinkModifier, LinkFrameworkApple, LinkOrdinalOutOfRange,
16-
LinkRequiresName, MultipleModifiers, NullOnLinkSection, RawDylibNoNul, RawDylibOnlyWindows,
17-
WholeArchiveNeedsStatic,
16+
LinkRequiresName, MultipleModifiers, NullOnLinkSection, RawDylibMachoUseVerbatim,
17+
RawDylibNoNul, RawDylibOnlyWindows, WholeArchiveNeedsStatic,
1818
};
1919

2020
pub(crate) struct LinkNameParser;
@@ -218,6 +218,17 @@ impl<S: Stage> CombineAttributeParser<S> for LinkParser {
218218
cx.emit_err(RawDylibNoNul { span: name_span });
219219
}
220220

221+
if sess.target.binary_format == BinaryFormat::MachO
222+
&& kind == Some(NativeLibKind::RawDylib)
223+
&& verbatim != Some(true)
224+
{
225+
// It is possible for us to emit a non-absolute path like `libSystem.dylib` in the
226+
// binary and have that work as well, though it's unclear what the use-case of that
227+
// would be, and it might lead to people accidentally specifying too "lax" linking.
228+
// So let's disallow it for now.
229+
cx.emit_err(RawDylibMachoUseVerbatim { span: cx.attr_span });
230+
}
231+
221232
result = Some(LinkEntry {
222233
span: cx.attr_span,
223234
kind: kind.unwrap_or(NativeLibKind::Unspecified),
@@ -286,22 +297,42 @@ impl LinkParser {
286297
NativeLibKind::Framework { as_needed: None }
287298
}
288299
sym::raw_dash_dylib => {
289-
if sess.target.is_like_windows {
290-
// raw-dylib is stable and working on Windows
291-
} else if sess.target.binary_format == BinaryFormat::Elf && features.raw_dylib_elf()
292-
{
293-
// raw-dylib is unstable on ELF, but the user opted in
294-
} else if sess.target.binary_format == BinaryFormat::Elf && sess.is_nightly_build()
295-
{
296-
feature_err(
297-
sess,
298-
sym::raw_dylib_elf,
299-
nv.value_span,
300-
fluent_generated::attr_parsing_raw_dylib_elf_unstable,
301-
)
302-
.emit();
303-
} else {
304-
cx.emit_err(RawDylibOnlyWindows { span: nv.value_span });
300+
match sess.target.binary_format {
301+
_ if sess.target.is_like_windows => {
302+
// raw-dylib is stable and working on Windows
303+
}
304+
305+
BinaryFormat::Elf if features.raw_dylib_elf() => {
306+
// raw-dylib is unstable on ELF, but the user opted in
307+
}
308+
BinaryFormat::Elf if sess.is_nightly_build() => {
309+
// Incomplete, so don't recommend if not nightly.
310+
feature_err(
311+
sess,
312+
sym::raw_dylib_elf,
313+
nv.value_span,
314+
fluent_generated::attr_parsing_raw_dylib_elf_unstable,
315+
)
316+
.emit();
317+
}
318+
319+
BinaryFormat::MachO if features.raw_dylib_macho() => {
320+
// raw-dylib is unstable on Mach-O, but the user opted in
321+
}
322+
BinaryFormat::MachO if sess.is_nightly_build() => {
323+
// Incomplete, so don't recommend if not nightly.
324+
feature_err(
325+
sess,
326+
sym::raw_dylib_macho,
327+
nv.value_span,
328+
fluent_generated::attr_parsing_raw_dylib_macho_unstable,
329+
)
330+
.emit();
331+
}
332+
333+
_ => {
334+
cx.emit_err(RawDylibOnlyWindows { span: nv.value_span });
335+
}
305336
}
306337

307338
NativeLibKind::RawDylib

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,13 @@ pub(crate) struct RawDylibOnlyWindows {
881881
pub span: Span,
882882
}
883883

884+
#[derive(Diagnostic)]
885+
#[diag(attr_parsing_raw_dylib_macho_use_verbatim)]
886+
pub(crate) struct RawDylibMachoUseVerbatim {
887+
#[primary_span]
888+
pub span: Span,
889+
}
890+
884891
#[derive(Diagnostic)]
885892
#[diag(attr_parsing_invalid_link_modifier)]
886893
pub(crate) struct InvalidLinkModifier {

compiler/rustc_feature/src/unstable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,10 @@ declare_features! (
611611
(unstable, postfix_match, "1.79.0", Some(121618)),
612612
/// Allows macro attributes on expressions, statements and non-inline modules.
613613
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
614-
/// Allows the use of raw-dylibs on ELF platforms
614+
/// Allows the use of raw-dylibs on ELF platforms.
615615
(incomplete, raw_dylib_elf, "1.87.0", Some(135694)),
616+
/// Allows the use of raw-dylibs on Mach-O platforms.
617+
(incomplete, raw_dylib_macho, "CURRENT_RUSTC_VERSION", Some(146356)),
616618
(unstable, reborrow, "CURRENT_RUSTC_VERSION", Some(145612)),
617619
/// Makes `&` and `&mut` patterns eat only one layer of references in Rust 2024.
618620
(incomplete, ref_pat_eat_one_layer_2024, "1.79.0", Some(123076)),

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,7 @@ symbols! {
17511751
raw_dash_dylib: "raw-dylib",
17521752
raw_dylib,
17531753
raw_dylib_elf,
1754+
raw_dylib_macho,
17541755
raw_eq,
17551756
raw_identifiers,
17561757
raw_ref_op,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ only-apple
2+
//@ needs-dynamic-linking
3+
4+
#[link(name = "uwu", kind = "raw-dylib", modifiers = "+verbatim")]
5+
//~^ ERROR: link kind `raw-dylib` is unstable on Mach-O platforms
6+
unsafe extern "C" {
7+
safe fn kawaii();
8+
}
9+
10+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: link kind `raw-dylib` is unstable on Mach-O platforms
2+
--> $DIR/feature-gate-raw-dylib-macho.rs:4:29
3+
|
4+
LL | #[link(name = "uwu", kind = "raw-dylib", modifiers = "+verbatim")]
5+
| ^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(raw_dylib_macho)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ only-apple
2+
//@ build-pass
3+
4+
#![allow(incomplete_features)]
5+
#![feature(raw_dylib_macho)]
6+
7+
#[link(name = "hack", kind = "raw-dylib", modifiers = "+verbatim")]
8+
unsafe extern "C" {}
9+
10+
fn main() {}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
//@ revisions: elf notelf
1+
//@ revisions: elf notelf macho
22
//@ [elf] only-elf
3+
//@ [macho] only-apple
34
//@ [notelf] ignore-windows
45
//@ [notelf] ignore-elf
6+
//@ [notelf] ignore-apple
57
//@ compile-flags: --crate-type lib
68
#[link(name = "foo", kind = "raw-dylib")]
79
//[notelf]~^ ERROR: link kind `raw-dylib` is only supported on Windows targets
810
//[elf]~^^ ERROR: link kind `raw-dylib` is unstable on ELF platforms
11+
//[macho]~^^^ ERROR: link kind `raw-dylib` is unstable on Mach-O platforms
12+
//[macho]~^^^^ ERROR: link kind `raw-dylib` should use the `+verbatim` linkage modifier
913
extern "C" {}

0 commit comments

Comments
 (0)