Skip to content

Commit 5b71768

Browse files
authored
Rollup merge of #123362 - oli-obk:thread_local_nested_statics, r=estebank
Check that nested statics in thread locals are duplicated per thread. follow-up to #123310 cc ``@compiler-errors`` ``@RalfJung`` fwiw: I have no idea how thread local statics make that work under LLVM, and miri fails on this example, which I would have expected to be the correct behaviour. Since the `#[thread_local]` attribute is just an internal implementation detail, I'm just going to start hard erroring on nested mutable statics in thread locals.
2 parents 464f264 + 64b75f7 commit 5b71768

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

Diff for: compiler/rustc_const_eval/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ const_eval_mut_deref =
222222
223223
const_eval_mutable_ptr_in_final = encountered mutable pointer in final value of {const_eval_intern_kind}
224224
225+
const_eval_nested_static_in_thread_local = #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead
225226
const_eval_non_const_fmt_macro_call =
226227
cannot call non-const formatting macro in {const_eval_const_context}s
227228

Diff for: compiler/rustc_const_eval/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ pub(crate) struct DanglingPtrInFinal {
2525
pub kind: InternKind,
2626
}
2727

28+
#[derive(Diagnostic)]
29+
#[diag(const_eval_nested_static_in_thread_local)]
30+
pub(crate) struct NestedStaticInThreadLocal {
31+
#[primary_span]
32+
pub span: Span,
33+
}
34+
2835
#[derive(LintDiagnostic)]
2936
#[diag(const_eval_mutable_ptr_in_final)]
3037
pub(crate) struct MutablePtrInFinal {

Diff for: compiler/rustc_const_eval/src/interpret/intern.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_span::sym;
2828

2929
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy};
3030
use crate::const_eval;
31-
use crate::errors::{DanglingPtrInFinal, MutablePtrInFinal};
31+
use crate::errors::{DanglingPtrInFinal, MutablePtrInFinal, NestedStaticInThreadLocal};
3232

3333
pub trait CompileTimeMachine<'mir, 'tcx: 'mir, T> = Machine<
3434
'mir,
@@ -108,6 +108,10 @@ fn intern_as_new_static<'tcx>(
108108
);
109109
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
110110

111+
if tcx.is_thread_local_static(static_id.into()) {
112+
tcx.dcx().emit_err(NestedStaticInThreadLocal { span: tcx.def_span(static_id) });
113+
}
114+
111115
// These do not inherit the codegen attrs of the parent static allocation, since
112116
// it doesn't make sense for them to inherit their `#[no_mangle]` and `#[link_name = ..]`
113117
// and the like.

Diff for: tests/ui/statics/nested_thread_local.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Check that we forbid nested statics in `thread_local` statics.
2+
3+
#![feature(const_refs_to_cell)]
4+
#![feature(thread_local)]
5+
6+
#[thread_local]
7+
static mut FOO: &u32 = {
8+
//~^ ERROR: does not support implicit nested statics
9+
// Prevent promotion (that would trigger on `&42` as an expression)
10+
let x = 42;
11+
&{ x }
12+
};
13+
14+
fn main() {}

Diff for: tests/ui/statics/nested_thread_local.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: #[thread_local] does not support implicit nested statics, please create explicit static items and refer to them instead
2+
--> $DIR/nested_thread_local.rs:7:1
3+
|
4+
LL | static mut FOO: &u32 = {
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)