Skip to content

Commit 13b168c

Browse files
Stabilize const_refs_to_static
1 parent 33855f8 commit 13b168c

39 files changed

+85
-406
lines changed

Diff for: compiler/rustc_const_eval/src/check_consts/check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
355355
{
356356
self.error_emitted = Some(guar);
357357
}
358-
self.check_op_spanned(ops::StaticAccess, span)
359358
}
360359

361360
fn check_local_or_return_ty(&mut self, ty: Ty<'tcx>, local: Local) {

Diff for: compiler/rustc_const_eval/src/check_consts/ops.rs

-27
Original file line numberDiff line numberDiff line change
@@ -553,33 +553,6 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
553553
}
554554
}
555555

556-
/// An access to a (non-thread-local) `static`.
557-
#[derive(Debug)]
558-
pub(crate) struct StaticAccess;
559-
impl<'tcx> NonConstOp<'tcx> for StaticAccess {
560-
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
561-
if let hir::ConstContext::Static(_) = ccx.const_kind() {
562-
Status::Allowed
563-
} else {
564-
Status::Unstable(sym::const_refs_to_static)
565-
}
566-
}
567-
568-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
569-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
570-
let mut err = feature_err(
571-
&ccx.tcx.sess,
572-
sym::const_refs_to_static,
573-
span,
574-
format!("referencing statics in {}s is unstable", ccx.const_kind(),),
575-
);
576-
err
577-
.note("`static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.")
578-
.help("to fix this, the value can be extracted to a `const` and then used.");
579-
err
580-
}
581-
}
582-
583556
/// An access to a thread-local `static`.
584557
#[derive(Debug)]
585558
pub(crate) struct ThreadLocalAccess;

Diff for: compiler/rustc_error_codes/src/error_codes/E0013.md

-20
This file was deleted.

Diff for: compiler/rustc_error_codes/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ E0005: 0005,
3535
E0007: 0007,
3636
E0009: 0009,
3737
E0010: 0010,
38-
E0013: 0013,
3938
E0014: 0014,
4039
E0015: 0015,
4140
E0023: 0023,

Diff for: compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ declare_features! (
145145
(accepted, const_panic, "1.57.0", Some(51999)),
146146
/// Allows dereferencing raw pointers during const eval.
147147
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911)),
148+
/// Allows creating pointers and references to `static` items in constants.
149+
(accepted, const_refs_to_static, "CURRENT_RUSTC_VERSION", Some(119618)),
148150
/// Allows implementing `Copy` for closures where possible (RFC 2132).
149151
(accepted, copy_closures, "1.26.0", Some(44490)),
150152
/// Allows `crate` in paths.

Diff for: compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,6 @@ declare_features! (
413413
(unstable, const_precise_live_drops, "1.46.0", Some(73255)),
414414
/// Allows references to types with interior mutability within constants
415415
(unstable, const_refs_to_cell, "1.51.0", Some(80384)),
416-
/// Allows creating pointers and references to `static` items in constants.
417-
(unstable, const_refs_to_static, "1.78.0", Some(119618)),
418416
/// Allows `impl const Trait for T` syntax.
419417
(unstable, const_trait_impl, "1.42.0", Some(67792)),
420418
/// Allows the `?` operator in const contexts.

Diff for: tests/ui/asm/const-refs-to-static.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//@ ignore-nvptx64
33
//@ ignore-spirv
44

5-
#![feature(const_refs_to_static)]
6-
75
use std::arch::{asm, global_asm};
86
use std::ptr::addr_of;
97

Diff for: tests/ui/asm/const-refs-to-static.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: invalid type for `const` operand
2-
--> $DIR/const-refs-to-static.rs:12:19
2+
--> $DIR/const-refs-to-static.rs:10:19
33
|
44
LL | global_asm!("{}", const addr_of!(FOO));
55
| ^^^^^^-------------
@@ -9,7 +9,7 @@ LL | global_asm!("{}", const addr_of!(FOO));
99
= help: `const` operands must be of an integer type
1010

1111
error: invalid type for `const` operand
12-
--> $DIR/const-refs-to-static.rs:17:25
12+
--> $DIR/const-refs-to-static.rs:15:25
1313
|
1414
LL | unsafe { asm!("{}", const addr_of!(FOO)) };
1515
| ^^^^^^-------------

Diff for: tests/ui/asm/x86_64/type-check-4.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//@ only-x86_64
22
//@ compile-flags: -C target-feature=+avx512f
3-
4-
use std::arch::{asm, global_asm};
3+
//@ check-pass
54

65
use std::arch::x86_64::{_mm256_setzero_ps, _mm_setzero_ps};
6+
use std::arch::{asm, global_asm};
77

88
fn main() {}
99

10-
// Constants must be... constant
11-
1210
static S: i32 = 1;
1311
const fn const_foo(x: i32) -> i32 {
1412
x
@@ -17,10 +15,7 @@ const fn const_bar<T>(x: T) -> T {
1715
x
1816
}
1917
global_asm!("{}", const S);
20-
//~^ ERROR referencing statics
2118
global_asm!("{}", const const_foo(0));
2219
global_asm!("{}", const const_foo(S));
23-
//~^ ERROR referencing statics
2420
global_asm!("{}", const const_bar(0));
2521
global_asm!("{}", const const_bar(S));
26-
//~^ ERROR referencing statics

Diff for: tests/ui/asm/x86_64/type-check-4.stderr

+3-36
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,6 @@
1-
error[E0658]: referencing statics in constants is unstable
2-
--> $DIR/type-check-4.rs:19:25
1+
warning: unstable feature specified for `-Ctarget-feature`: `avx512f`
32
|
4-
LL | global_asm!("{}", const S);
5-
| ^
6-
|
7-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
8-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
9-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
11-
= help: to fix this, the value can be extracted to a `const` and then used.
12-
13-
error[E0658]: referencing statics in constants is unstable
14-
--> $DIR/type-check-4.rs:22:35
15-
|
16-
LL | global_asm!("{}", const const_foo(S));
17-
| ^
18-
|
19-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
20-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
21-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
22-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
23-
= help: to fix this, the value can be extracted to a `const` and then used.
24-
25-
error[E0658]: referencing statics in constants is unstable
26-
--> $DIR/type-check-4.rs:25:35
27-
|
28-
LL | global_asm!("{}", const const_bar(S));
29-
| ^
30-
|
31-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
32-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
33-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
34-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
35-
= help: to fix this, the value can be extracted to a `const` and then used.
3+
= note: this feature is not stably supported; its behavior can change in the future
364

37-
error: aborting due to 3 previous errors
5+
warning: 1 warning emitted
386

39-
For more information about this error, try `rustc --explain E0658`.

Diff for: tests/ui/consts/const-fn-not-safe-for-const.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ static Y: u32 = 0;
1818

1919
const fn get_Y() -> u32 {
2020
Y
21-
//~^ ERROR referencing statics in constant functions
2221
}
2322

2423
const fn get_Y_addr() -> &'static u32 {
2524
&Y
26-
//~^ ERROR referencing statics in constant functions
2725
}
2826

2927
const fn get() -> u32 {

Diff for: tests/ui/consts/const-fn-not-safe-for-const.stderr

+2-27
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ LL | random()
66
|
77
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
88

9-
error[E0658]: referencing statics in constant functions is unstable
10-
--> $DIR/const-fn-not-safe-for-const.rs:20:5
11-
|
12-
LL | Y
13-
| ^
14-
|
15-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
16-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
17-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
18-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
19-
= help: to fix this, the value can be extracted to a `const` and then used.
20-
21-
error[E0658]: referencing statics in constant functions is unstable
22-
--> $DIR/const-fn-not-safe-for-const.rs:25:6
23-
|
24-
LL | &Y
25-
| ^
26-
|
27-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
28-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
29-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
31-
= help: to fix this, the value can be extracted to a `const` and then used.
32-
33-
error: aborting due to 3 previous errors
9+
error: aborting due to 1 previous error
3410

35-
Some errors have detailed explanations: E0015, E0658.
36-
For more information about an error, try `rustc --explain E0015`.
11+
For more information about this error, try `rustc --explain E0015`.

Diff for: tests/ui/consts/const-mut-refs/mut_ref_in_final_dynamic_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
22
//@ normalize-stderr-test: "( 0x[0-9a-f][0-9a-f] │)? ([0-9a-f][0-9a-f] |__ |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> " HEX_DUMP"
33
//@ normalize-stderr-test: "HEX_DUMP\s*\n\s*HEX_DUMP" -> "HEX_DUMP"
4-
#![feature(const_mut_refs, const_refs_to_static)]
4+
#![feature(const_mut_refs)]
55

66
use std::sync::Mutex;
77

Diff for: tests/ui/consts/const-ref-to-static-linux-vtable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//@check-pass
1+
//@ check-pass
22
//! This is the reduced version of the "Linux kernel vtable" use-case.
3-
#![feature(const_mut_refs, const_refs_to_static)]
3+
#![feature(const_mut_refs)]
44
use std::ptr::addr_of_mut;
55

66
#[repr(C)]

Diff for: tests/ui/consts/const_refs_to_static-ice-121413.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// issue: rust-lang/rust#121413
44
//@ compile-flags: -Zextra-const-ub-checks
55
// ignore-tidy-linelength
6-
#![feature(const_refs_to_static)]
76
const REF_INTERIOR_MUT: &usize = {
87
//~^ HELP consider importing this struct
98
static FOO: Sync = AtomicUsize::new(0);

Diff for: tests/ui/consts/const_refs_to_static-ice-121413.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0433]: failed to resolve: use of undeclared type `AtomicUsize`
2-
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
2+
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
33
|
44
LL | static FOO: Sync = AtomicUsize::new(0);
55
| ^^^^^^^^^^^ use of undeclared type `AtomicUsize`
@@ -10,7 +10,7 @@ LL + use std::sync::atomic::AtomicUsize;
1010
|
1111

1212
warning: trait objects without an explicit `dyn` are deprecated
13-
--> $DIR/const_refs_to_static-ice-121413.rs:9:17
13+
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
1414
|
1515
LL | static FOO: Sync = AtomicUsize::new(0);
1616
| ^^^^
@@ -24,15 +24,15 @@ LL | static FOO: dyn Sync = AtomicUsize::new(0);
2424
| +++
2525

2626
error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
27-
--> $DIR/const_refs_to_static-ice-121413.rs:9:17
27+
--> $DIR/const_refs_to_static-ice-121413.rs:8:17
2828
|
2929
LL | static FOO: Sync = AtomicUsize::new(0);
3030
| ^^^^ doesn't have a size known at compile-time
3131
|
3232
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
3333

3434
error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
35-
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
35+
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
3636
|
3737
LL | static FOO: Sync = AtomicUsize::new(0);
3838
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time

Diff for: tests/ui/consts/const_refs_to_static.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ run-pass
2-
#![feature(const_refs_to_static)]
32

43
static S: i32 = 0;
54
static mut S_MUT: i32 = 0;

Diff for: tests/ui/consts/const_refs_to_static_fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
22
//@ normalize-stderr-test: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
3-
#![feature(const_refs_to_static, const_mut_refs, sync_unsafe_cell)]
3+
#![feature(const_mut_refs, sync_unsafe_cell)]
44
use std::cell::SyncUnsafeCell;
55

66
static S: SyncUnsafeCell<i32> = SyncUnsafeCell::new(0);

Diff for: tests/ui/consts/const_refs_to_static_fail_invalid.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
22
//@ normalize-stderr-test: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
3-
#![feature(const_refs_to_static)]
43
#![allow(static_mut_refs)]
54

65
fn invalid() {

Diff for: tests/ui/consts/const_refs_to_static_fail_invalid.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/const_refs_to_static_fail_invalid.rs:9:5
2+
--> $DIR/const_refs_to_static_fail_invalid.rs:8:5
33
|
44
LL | const C: &bool = unsafe { std::mem::transmute(&S) };
55
| ^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered 0x0a, but expected a boolean
@@ -10,7 +10,7 @@ LL | const C: &bool = unsafe { std::mem::transmute(&S) };
1010
}
1111

1212
error[E0080]: it is undefined behavior to use this value
13-
--> $DIR/const_refs_to_static_fail_invalid.rs:25:5
13+
--> $DIR/const_refs_to_static_fail_invalid.rs:24:5
1414
|
1515
LL | const C: &i8 = unsafe { &S };
1616
| ^^^^^^^^^^^^ constructing invalid value: encountered reference to `extern` static in `const`
@@ -21,7 +21,7 @@ LL | const C: &i8 = unsafe { &S };
2121
}
2222

2323
error[E0080]: it is undefined behavior to use this value
24-
--> $DIR/const_refs_to_static_fail_invalid.rs:39:5
24+
--> $DIR/const_refs_to_static_fail_invalid.rs:38:5
2525
|
2626
LL | const C: &i32 = unsafe { &S_MUT };
2727
| ^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
@@ -32,19 +32,19 @@ LL | const C: &i32 = unsafe { &S_MUT };
3232
}
3333

3434
error: could not evaluate constant pattern
35-
--> $DIR/const_refs_to_static_fail_invalid.rs:15:9
35+
--> $DIR/const_refs_to_static_fail_invalid.rs:14:9
3636
|
3737
LL | C => {}
3838
| ^
3939

4040
error: could not evaluate constant pattern
41-
--> $DIR/const_refs_to_static_fail_invalid.rs:31:9
41+
--> $DIR/const_refs_to_static_fail_invalid.rs:30:9
4242
|
4343
LL | C => {}
4444
| ^
4545

4646
error: could not evaluate constant pattern
47-
--> $DIR/const_refs_to_static_fail_invalid.rs:46:9
47+
--> $DIR/const_refs_to_static_fail_invalid.rs:45:9
4848
|
4949
LL | C => {}
5050
| ^

Diff for: tests/ui/consts/issue-17718-const-bad-values.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const C1: &'static mut [usize] = &mut [];
55

66
static mut S: usize = 3;
77
const C2: &'static mut usize = unsafe { &mut S };
8-
//~^ ERROR: referencing statics in constants
9-
//~| ERROR: mutable references are not allowed
8+
//~^ ERROR: mutable references are not allowed in constants
109

1110
fn main() {}

Diff for: tests/ui/consts/issue-17718-const-bad-values.stderr

+1-13
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ error[E0764]: mutable references are not allowed in the final value of constants
44
LL | const C1: &'static mut [usize] = &mut [];
55
| ^^^^^^^
66

7-
error[E0658]: referencing statics in constants is unstable
8-
--> $DIR/issue-17718-const-bad-values.rs:7:46
9-
|
10-
LL | const C2: &'static mut usize = unsafe { &mut S };
11-
| ^
12-
|
13-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
14-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
15-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
16-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
17-
= help: to fix this, the value can be extracted to a `const` and then used.
18-
197
error[E0658]: mutable references are not allowed in constants
208
--> $DIR/issue-17718-const-bad-values.rs:7:41
219
|
@@ -26,7 +14,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
2614
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
2715
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2816

29-
error: aborting due to 3 previous errors
17+
error: aborting due to 2 previous errors
3018

3119
Some errors have detailed explanations: E0658, E0764.
3220
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)