-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #63831 - eddyb:proc-macro-statecheck, r=<try>
[WIP] rustc_mir: disallow global mutable state in proc macros. Along the lines of #63809, this PR attempts to get rid of the main (or only?) place `proc_macro` handles could be leaked to, *and* further disallow/discourage sharing (other) state between invocations. The approach of banning (interior-)mutable `static`s was most recently mentioned in #63804 (comment), but it's likely been brought up several times, we just never tried it. (Note that this is not foolproof: one would have to scan all dependencies for such `static`s, modulo `proc_macro`/`std`, and even then it's possible there would be a lot of false positives) So this is mostly for a check-only crater run, to see what (if anything) breaks.
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
|
||
#![crate_type = "proc-macro"] | ||
#![allow(warnings)] | ||
|
||
use std::cell::Cell; | ||
use std::sync::atomic::AtomicBool; | ||
|
||
static mut FOO: u8 = 0; | ||
//~^ ERROR mutable global state in a proc-macro | ||
|
||
static BAR: AtomicBool = AtomicBool::new(false); | ||
//~^ ERROR mutable global state in a proc-macro | ||
|
||
thread_local!(static BAZ: Cell<String> = Cell::new(String::new())); | ||
//~^ ERROR mutable global state in a proc-macro | ||
|
||
static FROZEN: &str = "snow"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: mutable global state in a proc-macro | ||
--> $DIR/global-mut-state.rs:10:1 | ||
| | ||
LL | static mut FOO: u8 = 0; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: mutable global state in a proc-macro | ||
--> $DIR/global-mut-state.rs:13:1 | ||
| | ||
LL | static BAR: AtomicBool = AtomicBool::new(false); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: mutable global state in a proc-macro | ||
--> $DIR/global-mut-state.rs:16:1 | ||
| | ||
LL | thread_local!(static BAZ: Cell<String> = Cell::new(String::new())); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error: aborting due to 3 previous errors | ||
|