From cdb6acc4810adeff79d2fe92db8d0f7cdf2d1a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=9D=E5=80=89=E6=B0=B4=E5=B8=8C?= Date: Tue, 9 Jun 2026 20:03:53 +0800 Subject: [PATCH] Reject static EII defaults on Apple targets --- compiler/rustc_builtin_macros/src/diagnostics.rs | 9 +++++++++ compiler/rustc_builtin_macros/src/eii.rs | 13 ++++++++++++- tests/ui/eii/static/auxiliary/decl_with_default.rs | 1 + .../eii/static/auxiliary/impl_default_override.rs | 1 + tests/ui/eii/static/default.rs | 2 ++ tests/ui/eii/static/default_apple.rs | 8 ++++++++ tests/ui/eii/static/default_apple.stderr | 10 ++++++++++ tests/ui/eii/static/default_cross_crate.rs | 2 ++ tests/ui/eii/static/default_cross_crate_explicit.rs | 2 ++ tests/ui/eii/static/default_explicit.rs | 2 ++ 10 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/ui/eii/static/default_apple.rs create mode 100644 tests/ui/eii/static/default_apple.stderr diff --git a/compiler/rustc_builtin_macros/src/diagnostics.rs b/compiler/rustc_builtin_macros/src/diagnostics.rs index afd9aea3efe..6ae0f908a88 100644 --- a/compiler/rustc_builtin_macros/src/diagnostics.rs +++ b/compiler/rustc_builtin_macros/src/diagnostics.rs @@ -1134,6 +1134,15 @@ pub(crate) struct EiiStaticMultipleImplementations { pub span: Span, } +#[derive(Diagnostic)] +#[diag("`#[{$name}]` cannot be used on statics with a value on Apple targets")] +#[note("see issue #157649 for more information")] +pub(crate) struct EiiStaticDefaultApple { + #[primary_span] + pub span: Span, + pub name: String, +} + #[derive(Diagnostic)] #[diag("`#[{$name}]` requires the name as an explicit argument when used on a static")] pub(crate) struct EiiStaticArgumentRequired { diff --git a/compiler/rustc_builtin_macros/src/eii.rs b/compiler/rustc_builtin_macros/src/eii.rs index b4828675822..68d5278c85e 100644 --- a/compiler/rustc_builtin_macros/src/eii.rs +++ b/compiler/rustc_builtin_macros/src/eii.rs @@ -13,7 +13,7 @@ use crate::diagnostics::{ EiiAttributeNotSupported, EiiExternTargetExpectedList, EiiExternTargetExpectedMacro, EiiExternTargetExpectedUnsafe, EiiMacroExpectedMaxOneArgument, EiiOnlyOnce, EiiSharedMacroInStatementPosition, EiiSharedMacroTarget, EiiStaticArgumentRequired, - EiiStaticMultipleImplementations, EiiStaticMutable, + EiiStaticDefaultApple, EiiStaticMultipleImplementations, EiiStaticMutable, }; /// ```rust @@ -86,6 +86,17 @@ fn eii_( let (item_span, foreign_item_name) = match kind { ItemKind::Fn(func) => (func.sig.span, func.ident), ItemKind::Static(stat) => { + // See https://github.com/rust-lang/rust/issues/157649 + if let Some(expr) = &stat.expr + && ecx.sess.target.is_like_darwin + { + ecx.dcx().emit_err(EiiStaticDefaultApple { + span: expr.span, + name: path_to_string(&meta_item.path), + }); + return vec![]; + } + // Statics must have an explicit name for the eii if meta_item.is_word() { ecx.dcx().emit_err(EiiStaticArgumentRequired { diff --git a/tests/ui/eii/static/auxiliary/decl_with_default.rs b/tests/ui/eii/static/auxiliary/decl_with_default.rs index ba87b35deb4..53cdc8d62bb 100644 --- a/tests/ui/eii/static/auxiliary/decl_with_default.rs +++ b/tests/ui/eii/static/auxiliary/decl_with_default.rs @@ -1,3 +1,4 @@ +//@ no-prefer-dynamic #![crate_type = "rlib"] #![feature(extern_item_impls)] diff --git a/tests/ui/eii/static/auxiliary/impl_default_override.rs b/tests/ui/eii/static/auxiliary/impl_default_override.rs index d70a137fbc4..03cb061135e 100644 --- a/tests/ui/eii/static/auxiliary/impl_default_override.rs +++ b/tests/ui/eii/static/auxiliary/impl_default_override.rs @@ -1,3 +1,4 @@ +//@ no-prefer-dynamic //@ aux-build: decl_with_default.rs #![crate_type = "rlib"] #![feature(extern_item_impls)] diff --git a/tests/ui/eii/static/default.rs b/tests/ui/eii/static/default.rs index 02307432e2f..6234ee2f0c1 100644 --- a/tests/ui/eii/static/default.rs +++ b/tests/ui/eii/static/default.rs @@ -3,6 +3,8 @@ //@ ignore-backends: gcc // FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418 //@ ignore-windows +// FIXME(#157649): static EII defaults currently fail to link on Apple targets. +//@ ignore-apple // Tests static EIIs with default implementations. #![feature(extern_item_impls)] diff --git a/tests/ui/eii/static/default_apple.rs b/tests/ui/eii/static/default_apple.rs new file mode 100644 index 00000000000..f638ee480f4 --- /dev/null +++ b/tests/ui/eii/static/default_apple.rs @@ -0,0 +1,8 @@ +//@ only-apple +//@ ignore-backends: gcc + +#![feature(extern_item_impls)] +#![crate_type = "lib"] +#[eii(eii1)] +pub static DECL1: u64 = 5; +//~^ ERROR `#[eii]` cannot be used on statics with a value on Apple targets diff --git a/tests/ui/eii/static/default_apple.stderr b/tests/ui/eii/static/default_apple.stderr new file mode 100644 index 00000000000..d4ced594897 --- /dev/null +++ b/tests/ui/eii/static/default_apple.stderr @@ -0,0 +1,10 @@ +error: `#[eii]` cannot be used on statics with a value on Apple targets + --> $DIR/default_apple.rs:7:25 + | +LL | pub static DECL1: u64 = 5; + | ^ + | + = note: see issue #157649 for more information + +error: aborting due to 1 previous error + diff --git a/tests/ui/eii/static/default_cross_crate.rs b/tests/ui/eii/static/default_cross_crate.rs index 4d6df4b7ee7..f9de906ac26 100644 --- a/tests/ui/eii/static/default_cross_crate.rs +++ b/tests/ui/eii/static/default_cross_crate.rs @@ -4,6 +4,8 @@ //@ ignore-backends: gcc // FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418 //@ ignore-windows +// FIXME(#157649): static EII defaults currently fail to link on Apple targets. +//@ ignore-apple // Tests that a static EII default can be used from another crate. extern crate decl_with_default; diff --git a/tests/ui/eii/static/default_cross_crate_explicit.rs b/tests/ui/eii/static/default_cross_crate_explicit.rs index c3139f45d19..1f534e300e0 100644 --- a/tests/ui/eii/static/default_cross_crate_explicit.rs +++ b/tests/ui/eii/static/default_cross_crate_explicit.rs @@ -5,6 +5,8 @@ //@ ignore-backends: gcc // FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418 //@ ignore-windows +// FIXME(#157649): static EII defaults currently fail to link on Apple targets. +//@ ignore-apple // Tests that an explicit static EII implementation overrides a cross-crate default. extern crate decl_with_default; diff --git a/tests/ui/eii/static/default_explicit.rs b/tests/ui/eii/static/default_explicit.rs index b18da35c3de..6cf36d8da50 100644 --- a/tests/ui/eii/static/default_explicit.rs +++ b/tests/ui/eii/static/default_explicit.rs @@ -3,6 +3,8 @@ //@ ignore-backends: gcc // FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418 //@ ignore-windows +// FIXME(#157649): static EII defaults currently fail to link on Apple targets. +//@ ignore-apple // Tests that an explicit static EII implementation overrides a local default. #![feature(extern_item_impls)]