forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalizers in Alloy are run on a separate thread, so if a user tries to access a thread-local from within a drop method used as a finalizer it is almost certainly incorrect. This commit disallows this and emit an error when it happens.
- Loading branch information
1 parent
5ae18a6
commit fc9dc6e
Showing
4 changed files
with
87 additions
and
1 deletion.
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
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,27 @@ | ||
#![feature(gc)] | ||
#![feature(negative_impls)] | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
include!{"./auxiliary/types.rs"} | ||
|
||
use std::cell::Cell; | ||
|
||
thread_local! { | ||
static COUNTER: Cell<u32> = Cell::new(0); | ||
} | ||
|
||
|
||
#[derive(Debug)] | ||
struct S; | ||
|
||
impl Drop for S { | ||
fn drop(&mut self) { | ||
// Access the thread-local variable | ||
let x = COUNTER.get(); | ||
} | ||
} | ||
|
||
fn main() { | ||
Gc::new(FinalizerUnsafeWrapper(S)); | ||
//~^ ERROR: The drop method for `S` cannot be safely finalized. | ||
} |
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,13 @@ | ||
error: The drop method for `S` cannot be safely finalized. | ||
--> $DIR/thread_locals.rs:25:13 | ||
| | ||
LL | let x = COUNTER.get(); | ||
| ------- this thread-local is not safe to run in a finalizer | ||
... | ||
LL | Gc::new(FinalizerUnsafeWrapper(S)); | ||
| --------^^^^^^^^^^^^^^^^^^^^^^^^^- caused by trying to construct a `Gc<FinalizerUnsafeWrapper<S>>` here. | ||
| | ||
= help: `Gc` runs finalizers on a separate thread, so thread-locals cannot be accessed. | ||
|
||
error: aborting due to 1 previous error | ||
|