Skip to content

Commit 59ed245

Browse files
Reject mutable externally implementable statics
1 parent fca29ad commit 59ed245

11 files changed

Lines changed: 97 additions & 7 deletions

File tree

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc_ast::token::{Delimiter, TokenKind};
22
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
33
use rustc_ast::{
4-
Attribute, DUMMY_NODE_ID, EiiDecl, EiiImpl, ItemKind, MetaItem, Path, StmtKind, Visibility, ast,
4+
Attribute, DUMMY_NODE_ID, EiiDecl, EiiImpl, ItemKind, MetaItem, Mutability, Path, StmtKind,
5+
Visibility, ast,
56
};
67
use rustc_ast_pretty::pprust::path_to_string;
78
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -11,7 +12,7 @@ use thin_vec::{ThinVec, thin_vec};
1112
use crate::errors::{
1213
EiiExternTargetExpectedList, EiiExternTargetExpectedMacro, EiiExternTargetExpectedUnsafe,
1314
EiiMacroExpectedMaxOneArgument, EiiOnlyOnce, EiiSharedMacroInStatementPosition,
14-
EiiSharedMacroTarget, EiiStaticArgumentRequired, EiiStaticDefault,
15+
EiiSharedMacroTarget, EiiStaticArgumentRequired, EiiStaticDefault, EiiStaticMutable,
1516
};
1617

1718
/// ```rust
@@ -100,6 +101,15 @@ fn eii_(
100101
});
101102
return vec![];
102103
}
104+
105+
// Mut statics are currently not supported
106+
if stat.mutability == Mutability::Mut {
107+
ecx.dcx().emit_err(EiiStaticMutable {
108+
span: eii_attr_span,
109+
name: path_to_string(&meta_item.path),
110+
});
111+
}
112+
103113
(item.span, stat.ident)
104114
}
105115
_ => {

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,14 @@ pub(crate) struct EiiStaticArgumentRequired {
11401140
pub name: String,
11411141
}
11421142

1143+
#[derive(Diagnostic)]
1144+
#[diag("`#[{$name}]` cannot be used on mutable statics")]
1145+
pub(crate) struct EiiStaticMutable {
1146+
#[primary_span]
1147+
pub span: Span,
1148+
pub name: String,
1149+
}
1150+
11431151
#[derive(Diagnostic)]
11441152
#[diag("`#[{$name}]` can only be used on functions inside a module")]
11451153
pub(crate) struct EiiSharedMacroInStatementPosition {

tests/ui/eii/static/mismatch_mut.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(extern_item_impls)]
66

77
#[eii(hello)]
8+
//~^ ERROR `#[eii]` cannot be used on mutable statics
89
static mut HELLO: u64;
910

1011
#[hello]
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
error: `#[eii]` cannot be used on mutable statics
2+
--> $DIR/mismatch_mut.rs:7:1
3+
|
4+
LL | #[eii(hello)]
5+
| ^^^^^^^^^^^^^
6+
17
error: mutability does not match with the definition of`#[hello]`
2-
--> $DIR/mismatch_mut.rs:10:1
8+
--> $DIR/mismatch_mut.rs:11:1
39
|
410
LL | #[hello]
511
| ^^^^^^^^
612

7-
error: aborting due to 1 previous error
13+
error: aborting due to 2 previous errors
814

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ ignore-backends: gcc
2+
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
3+
//@ ignore-windows
4+
// Tests whether EIIs work on statics
5+
#![feature(extern_item_impls)]
6+
7+
#[eii(hello)]
8+
static HELLO: u64;
9+
10+
#[hello]
11+
//~^ ERROR mutability does not match with the definition of`#[hello]`
12+
static mut HELLO_IMPL: u64 = 5;
13+
14+
// what you would write:
15+
fn main() {
16+
// directly
17+
println!("{}", unsafe { HELLO_IMPL });
18+
19+
// through the alias
20+
println!("{HELLO}");
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: mutability does not match with the definition of`#[hello]`
2+
--> $DIR/mismatch_mut2.rs:10:1
3+
|
4+
LL | #[hello]
5+
| ^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

tests/ui/eii/static/mismatch_safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![feature(extern_item_impls)]
66

77
#[eii(hello)]
8-
unsafe static mut HELLO: u64;
8+
unsafe static HELLO: u64;
99

1010
#[hello]
1111
//~^ ERROR safety does not match with the definition of`#[hello]`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ ignore-backends: gcc
2+
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
3+
//@ ignore-windows
4+
// Tests whether EIIs work on statics
5+
#![feature(extern_item_impls)]
6+
7+
#[eii(hello)]
8+
static HELLO: u64;
9+
10+
#[hello]
11+
unsafe static HELLO_IMPL: u64 = 5;
12+
//~^ ERROR static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
13+
14+
// what you would write:
15+
fn main() {
16+
// directly
17+
println!("{HELLO_IMPL}");
18+
19+
// through the alias
20+
println!("{}", unsafe { HELLO });
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: static items cannot be declared with `unsafe` safety qualifier outside of `extern` block
2+
--> $DIR/mismatch_safety2.rs:11:1
3+
|
4+
LL | unsafe static HELLO_IMPL: u64 = 5;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

tests/ui/eii/static/mut.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
//@ run-pass
2-
//@ check-run-results
31
//@ ignore-backends: gcc
42
// FIXME: linking on windows (specifically mingw) not yet supported, see tracking issue #125418
53
//@ ignore-windows
64
// Tests whether EIIs work on statics
75
#![feature(extern_item_impls)]
86

97
#[eii(hello)]
8+
//~^ ERROR `#[eii]` cannot be used on mutable statics
109
static mut HELLO: u64;
1110

1211
#[hello]

0 commit comments

Comments
 (0)