Reject mutable externally implementable statics

This commit is contained in:
Jonathan Brouwer
2026-04-10 15:05:07 +02:00
parent fca29ada74
commit 59ed245945
11 changed files with 97 additions and 7 deletions
+12 -2
View File
@@ -1,7 +1,8 @@
use rustc_ast::token::{Delimiter, TokenKind};
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::{
Attribute, DUMMY_NODE_ID, EiiDecl, EiiImpl, ItemKind, MetaItem, Path, StmtKind, Visibility, ast,
Attribute, DUMMY_NODE_ID, EiiDecl, EiiImpl, ItemKind, MetaItem, Mutability, Path, StmtKind,
Visibility, ast,
};
use rustc_ast_pretty::pprust::path_to_string;
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -11,7 +12,7 @@
use crate::errors::{
EiiExternTargetExpectedList, EiiExternTargetExpectedMacro, EiiExternTargetExpectedUnsafe,
EiiMacroExpectedMaxOneArgument, EiiOnlyOnce, EiiSharedMacroInStatementPosition,
EiiSharedMacroTarget, EiiStaticArgumentRequired, EiiStaticDefault,
EiiSharedMacroTarget, EiiStaticArgumentRequired, EiiStaticDefault, EiiStaticMutable,
};
/// ```rust
@@ -100,6 +101,15 @@ fn eii_(
});
return vec![];
}
// Mut statics are currently not supported
if stat.mutability == Mutability::Mut {
ecx.dcx().emit_err(EiiStaticMutable {
span: eii_attr_span,
name: path_to_string(&meta_item.path),
});
}
(item.span, stat.ident)
}
_ => {
@@ -1140,6 +1140,14 @@ pub(crate) struct EiiStaticArgumentRequired {
pub name: String,
}
#[derive(Diagnostic)]
#[diag("`#[{$name}]` cannot be used on mutable statics")]
pub(crate) struct EiiStaticMutable {
#[primary_span]
pub span: Span,
pub name: String,
}
#[derive(Diagnostic)]
#[diag("`#[{$name}]` can only be used on functions inside a module")]
pub(crate) struct EiiSharedMacroInStatementPosition {
+1
View File
@@ -5,6 +5,7 @@
#![feature(extern_item_impls)]
#[eii(hello)]
//~^ ERROR `#[eii]` cannot be used on mutable statics
static mut HELLO: u64;
#[hello]
+8 -2
View File
@@ -1,8 +1,14 @@
error: `#[eii]` cannot be used on mutable statics
--> $DIR/mismatch_mut.rs:7:1
|
LL | #[eii(hello)]
| ^^^^^^^^^^^^^
error: mutability does not match with the definition of`#[hello]`
--> $DIR/mismatch_mut.rs:10:1
--> $DIR/mismatch_mut.rs:11:1
|
LL | #[hello]
| ^^^^^^^^
error: aborting due to 1 previous error
error: aborting due to 2 previous errors
+21
View File
@@ -0,0 +1,21 @@
//@ ignore-backends: gcc
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
//@ ignore-windows
// Tests whether EIIs work on statics
#![feature(extern_item_impls)]
#[eii(hello)]
static HELLO: u64;
#[hello]
//~^ ERROR mutability does not match with the definition of`#[hello]`
static mut HELLO_IMPL: u64 = 5;
// what you would write:
fn main() {
// directly
println!("{}", unsafe { HELLO_IMPL });
// through the alias
println!("{HELLO}");
}
+8
View File
@@ -0,0 +1,8 @@
error: mutability does not match with the definition of`#[hello]`
--> $DIR/mismatch_mut2.rs:10:1
|
LL | #[hello]
| ^^^^^^^^
error: aborting due to 1 previous error
+1 -1
View File
@@ -5,7 +5,7 @@
#![feature(extern_item_impls)]
#[eii(hello)]
unsafe static mut HELLO: u64;
unsafe static HELLO: u64;
#[hello]
//~^ ERROR safety does not match with the definition of`#[hello]`
+21
View File
@@ -0,0 +1,21 @@
//@ ignore-backends: gcc
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
//@ ignore-windows
// Tests whether EIIs work on statics
#![feature(extern_item_impls)]
#[eii(hello)]
static HELLO: u64;
#[hello]
unsafe static HELLO_IMPL: u64 = 5;
//~^ ERROR static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
// what you would write:
fn main() {
// directly
println!("{HELLO_IMPL}");
// through the alias
println!("{}", unsafe { HELLO });
}
@@ -0,0 +1,8 @@
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
--> $DIR/mismatch_safety2.rs:11:1
|
LL | unsafe static HELLO_IMPL: u64 = 5;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
+1 -2
View File
@@ -1,5 +1,3 @@
//@ run-pass
//@ check-run-results
//@ ignore-backends: gcc
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
//@ ignore-windows
@@ -7,6 +5,7 @@
#![feature(extern_item_impls)]
#[eii(hello)]
//~^ ERROR `#[eii]` cannot be used on mutable statics
static mut HELLO: u64;
#[hello]
+8
View File
@@ -0,0 +1,8 @@
error: `#[eii]` cannot be used on mutable statics
--> $DIR/mut.rs:7:1
|
LL | #[eii(hello)]
| ^^^^^^^^^^^^^
error: aborting due to 1 previous error