Skip to content

Commit 09bbcd6

Browse files
authored
Rollup merge of #120883 - RalfJung:extern-static-err, r=oli-obk
interpret: rename ReadExternStatic → ExternStatic This error shows up for reads and writes, so `ReadExternStatic` is misleading.
2 parents e82e087 + d56f3b6 commit 09bbcd6

File tree

8 files changed

+49
-8
lines changed

8 files changed

+49
-8
lines changed

compiler/rustc_const_eval/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const_eval_error = {$error_kind ->
9898
const_eval_exact_div_has_remainder =
9999
exact_div: {$a} cannot be divided by {$b} without remainder
100100
101+
const_eval_extern_static =
102+
cannot access extern static ({$did})
101103
const_eval_fn_ptr_call =
102104
function pointers need an RFC before allowed to be called in {const_eval_const_context}s
103105
const_eval_for_loop_into_iter_non_const =
@@ -302,8 +304,6 @@ const_eval_raw_ptr_to_int =
302304
.note = at compile-time, pointers do not have an integer value
303305
.note2 = avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
304306
305-
const_eval_read_extern_static =
306-
cannot read from extern static ({$did})
307307
const_eval_read_pointer_as_int =
308308
unable to turn pointer into integer
309309
const_eval_realloc_or_alloc_with_offset =

compiler/rustc_const_eval/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl ReportErrorExt for UnsupportedOpInfo {
799799
UnsupportedOpInfo::ReadPartialPointer(_) => const_eval_partial_pointer_copy,
800800
UnsupportedOpInfo::ReadPointerAsInt(_) => const_eval_read_pointer_as_int,
801801
UnsupportedOpInfo::ThreadLocalStatic(_) => const_eval_thread_local_static,
802-
UnsupportedOpInfo::ReadExternStatic(_) => const_eval_read_extern_static,
802+
UnsupportedOpInfo::ExternStatic(_) => const_eval_extern_static,
803803
}
804804
}
805805
fn add_args<G: EmissionGuarantee>(self, _: &DiagCtxt, builder: &mut DiagnosticBuilder<'_, G>) {
@@ -818,7 +818,7 @@ impl ReportErrorExt for UnsupportedOpInfo {
818818
OverwritePartialPointer(ptr) | ReadPartialPointer(ptr) => {
819819
builder.arg("ptr", ptr);
820820
}
821-
ThreadLocalStatic(did) | ReadExternStatic(did) => {
821+
ThreadLocalStatic(did) | ExternStatic(did) => {
822822
builder.arg("did", format!("{did:?}"));
823823
}
824824
}

compiler/rustc_const_eval/src/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
557557
if self.tcx.is_foreign_item(def_id) {
558558
// This is unreachable in Miri, but can happen in CTFE where we actually *do* support
559559
// referencing arbitrary (declared) extern statics.
560-
throw_unsup!(ReadExternStatic(def_id));
560+
throw_unsup!(ExternStatic(def_id));
561561
}
562562

563563
// We don't give a span -- statics don't need that, they cannot be generic or associated.

compiler/rustc_middle/src/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ pub enum UnsupportedOpInfo {
470470
/// Accessing thread local statics
471471
ThreadLocalStatic(DefId),
472472
/// Accessing an unsupported extern static.
473-
ReadExternStatic(DefId),
473+
ExternStatic(DefId),
474474
}
475475

476476
/// Error information for when the program exhausted the resources granted to it
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
#![feature(thread_local)]
3+
#![allow(static_mut_ref)]
4+
5+
extern "C" {
6+
static mut DATA: u8;
7+
}
8+
9+
// Make sure we catch accessing extern static.
10+
static TEST_READ: () = {
11+
unsafe { let _val = DATA; }
12+
//~^ ERROR could not evaluate static initializer
13+
//~| NOTE cannot access extern static
14+
};
15+
static TEST_WRITE: () = {
16+
unsafe { DATA = 0; }
17+
//~^ ERROR could not evaluate static initializer
18+
//~| NOTE cannot access extern static
19+
};
20+
21+
// Just creating a reference is fine, as long as we are not reading or writing.
22+
static TEST_REF: &u8 = unsafe { &DATA };
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: could not evaluate static initializer
2+
--> $DIR/extern-static.rs:11:25
3+
|
4+
LL | unsafe { let _val = DATA; }
5+
| ^^^^ cannot access extern static (DefId(0:4 ~ extern_static[c41e]::{extern#0}::DATA))
6+
7+
error[E0080]: could not evaluate static initializer
8+
--> $DIR/extern-static.rs:16:14
9+
|
10+
LL | unsafe { DATA = 0; }
11+
| ^^^^^^^^ cannot access extern static (DefId(0:4 ~ extern_static[c41e]::{extern#0}::DATA))
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/miri_unleashed/tls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ static TEST_BAD: () = {
1414
};
1515

1616
// Make sure we catch taking a reference to thread-local storage.
17+
// The actual pointer depends on the thread, so even just taking a reference already does not make
18+
// sense at compile-time.
1719
static TEST_BAD_REF: () = {
1820
unsafe { let _val = &A; }
1921
//~^ ERROR could not evaluate static initializer

tests/ui/consts/miri_unleashed/tls.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | unsafe { let _val = A; }
55
| ^ cannot access thread local static (DefId(0:4 ~ tls[ca29]::A))
66

77
error[E0080]: could not evaluate static initializer
8-
--> $DIR/tls.rs:18:26
8+
--> $DIR/tls.rs:20:26
99
|
1010
LL | unsafe { let _val = &A; }
1111
| ^ cannot access thread local static (DefId(0:4 ~ tls[ca29]::A))
@@ -18,7 +18,7 @@ help: skipping check that does not even have a feature gate
1818
LL | unsafe { let _val = A; }
1919
| ^
2020
help: skipping check that does not even have a feature gate
21-
--> $DIR/tls.rs:18:26
21+
--> $DIR/tls.rs:20:26
2222
|
2323
LL | unsafe { let _val = &A; }
2424
| ^

0 commit comments

Comments
 (0)