Reject static EII defaults on Apple targets

This commit is contained in:
朝倉水希
2026-06-09 20:03:53 +08:00
parent 1e125ceba9
commit cdb6acc481
10 changed files with 49 additions and 1 deletions

View File

@@ -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 <https://github.com/rust-lang/rust/issues/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 {

View File

@@ -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 {

View File

@@ -1,3 +1,4 @@
//@ no-prefer-dynamic
#![crate_type = "rlib"]
#![feature(extern_item_impls)]

View File

@@ -1,3 +1,4 @@
//@ no-prefer-dynamic
//@ aux-build: decl_with_default.rs
#![crate_type = "rlib"]
#![feature(extern_item_impls)]

View File

@@ -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)]

View File

@@ -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

View File

@@ -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 <https://github.com/rust-lang/rust/issues/157649> for more information
error: aborting due to 1 previous error

View File

@@ -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;

View File

@@ -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;

View File

@@ -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)]