mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Reject mutable externally implementable statics
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#![feature(extern_item_impls)]
|
||||
|
||||
#[eii(hello)]
|
||||
//~^ ERROR `#[eii]` cannot be used on mutable statics
|
||||
static mut HELLO: u64;
|
||||
|
||||
#[hello]
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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]`
|
||||
|
||||
@@ -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,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]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user