Skip to content

Commit 41dc5d3

Browse files
Stabilize const_refs_to_static
1 parent 33855f8 commit 41dc5d3

38 files changed

+86
-386
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 1 deletion
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) {

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 0 additions & 27 deletions
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;

compiler/rustc_error_codes/src/error_codes/E0013.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ variable cannot refer to a static variable.
55

66
Erroneous code example:
77

8-
```compile_fail,E0658
8+
```
99
static X: i32 = 42;
1010
const Y: i32 = X;
1111
```

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
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.

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
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.

tests/ui/asm/const-refs-to-static.rs

Lines changed: 0 additions & 2 deletions
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

tests/ui/asm/const-refs-to-static.stderr

Lines changed: 2 additions & 2 deletions
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
| ^^^^^^-------------

tests/ui/asm/x86_64/type-check-4.rs

Lines changed: 2 additions & 7 deletions
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
Lines changed: 3 additions & 36 deletions
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`.

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

Lines changed: 0 additions & 2 deletions
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 {

0 commit comments

Comments
 (0)