Skip to content

Commit

Permalink
Rollup merge of rust-lang#129759 - dingxiangfei2009:stabilize-const-r…
Browse files Browse the repository at this point in the history
…efs-to-static, r=petrochenkov

Stabilize `const_refs_to_static`

Close rust-lang#128183
Tracked by rust-lang#119618
cc `@nikomatsakis`

Meanwhile, I am cooking a sub-section in the language reference.
  • Loading branch information
workingjubilee committed Sep 11, 2024
2 parents 027a105 + 41dc5d3 commit 4cda52d
Show file tree
Hide file tree
Showing 38 changed files with 86 additions and 386 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_const_eval/src/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
{
self.error_emitted = Some(guar);
}
self.check_op_spanned(ops::StaticAccess, span)
}

fn check_local_or_return_ty(&mut self, ty: Ty<'tcx>, local: Local) {
Expand Down
27 changes: 0 additions & 27 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,33 +553,6 @@ impl<'tcx> NonConstOp<'tcx> for RawPtrToIntCast {
}
}

/// An access to a (non-thread-local) `static`.
#[derive(Debug)]
pub(crate) struct StaticAccess;
impl<'tcx> NonConstOp<'tcx> for StaticAccess {
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
if let hir::ConstContext::Static(_) = ccx.const_kind() {
Status::Allowed
} else {
Status::Unstable(sym::const_refs_to_static)
}
}

#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
let mut err = feature_err(
&ccx.tcx.sess,
sym::const_refs_to_static,
span,
format!("referencing statics in {}s is unstable", ccx.const_kind(),),
);
err
.note("`static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.")
.help("to fix this, the value can be extracted to a `const` and then used.");
err
}
}

/// An access to a thread-local `static`.
#[derive(Debug)]
pub(crate) struct ThreadLocalAccess;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes/E0013.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ variable cannot refer to a static variable.

Erroneous code example:

```compile_fail,E0658
```
static X: i32 = 42;
const Y: i32 = X;
```
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ declare_features! (
(accepted, const_panic, "1.57.0", Some(51999)),
/// Allows dereferencing raw pointers during const eval.
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911)),
/// Allows creating pointers and references to `static` items in constants.
(accepted, const_refs_to_static, "CURRENT_RUSTC_VERSION", Some(119618)),
/// Allows implementing `Copy` for closures where possible (RFC 2132).
(accepted, copy_closures, "1.26.0", Some(44490)),
/// Allows `crate` in paths.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,6 @@ declare_features! (
(unstable, const_precise_live_drops, "1.46.0", Some(73255)),
/// Allows references to types with interior mutability within constants
(unstable, const_refs_to_cell, "1.51.0", Some(80384)),
/// Allows creating pointers and references to `static` items in constants.
(unstable, const_refs_to_static, "1.78.0", Some(119618)),
/// Allows `impl const Trait for T` syntax.
(unstable, const_trait_impl, "1.42.0", Some(67792)),
/// Allows the `?` operator in const contexts.
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/asm/const-refs-to-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//@ ignore-nvptx64
//@ ignore-spirv

#![feature(const_refs_to_static)]

use std::arch::{asm, global_asm};
use std::ptr::addr_of;

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/asm/const-refs-to-static.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: invalid type for `const` operand
--> $DIR/const-refs-to-static.rs:12:19
--> $DIR/const-refs-to-static.rs:10:19
|
LL | global_asm!("{}", const addr_of!(FOO));
| ^^^^^^-------------
Expand All @@ -9,7 +9,7 @@ LL | global_asm!("{}", const addr_of!(FOO));
= help: `const` operands must be of an integer type

error: invalid type for `const` operand
--> $DIR/const-refs-to-static.rs:17:25
--> $DIR/const-refs-to-static.rs:15:25
|
LL | unsafe { asm!("{}", const addr_of!(FOO)) };
| ^^^^^^-------------
Expand Down
9 changes: 2 additions & 7 deletions tests/ui/asm/x86_64/type-check-4.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//@ only-x86_64
//@ compile-flags: -C target-feature=+avx512f

use std::arch::{asm, global_asm};
//@ check-pass

use std::arch::x86_64::{_mm256_setzero_ps, _mm_setzero_ps};
use std::arch::{asm, global_asm};

fn main() {}

// Constants must be... constant

static S: i32 = 1;
const fn const_foo(x: i32) -> i32 {
x
Expand All @@ -17,10 +15,7 @@ const fn const_bar<T>(x: T) -> T {
x
}
global_asm!("{}", const S);
//~^ ERROR referencing statics
global_asm!("{}", const const_foo(0));
global_asm!("{}", const const_foo(S));
//~^ ERROR referencing statics
global_asm!("{}", const const_bar(0));
global_asm!("{}", const const_bar(S));
//~^ ERROR referencing statics
39 changes: 3 additions & 36 deletions tests/ui/asm/x86_64/type-check-4.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:19:25
warning: unstable feature specified for `-Ctarget-feature`: `avx512f`
|
LL | global_asm!("{}", const S);
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:22:35
|
LL | global_asm!("{}", const const_foo(S));
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:25:35
|
LL | global_asm!("{}", const const_bar(S));
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.
= note: this feature is not stably supported; its behavior can change in the future

error: aborting due to 3 previous errors
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0658`.
2 changes: 0 additions & 2 deletions tests/ui/consts/const-fn-not-safe-for-const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ static Y: u32 = 0;

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

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

const fn get() -> u32 {
Expand Down
29 changes: 2 additions & 27 deletions tests/ui/consts/const-fn-not-safe-for-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@ LL | random()
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0658]: referencing statics in constant functions is unstable
--> $DIR/const-fn-not-safe-for-const.rs:20:5
|
LL | Y
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.

error[E0658]: referencing statics in constant functions is unstable
--> $DIR/const-fn-not-safe-for-const.rs:25:6
|
LL | &Y
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.

error: aborting due to 3 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0015, E0658.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0015`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ 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"
//@ normalize-stderr-test: "HEX_DUMP\s*\n\s*HEX_DUMP" -> "HEX_DUMP"
#![feature(const_mut_refs, const_refs_to_static)]
#![feature(const_mut_refs)]

use std::sync::Mutex;

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/const-ref-to-static-linux-vtable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@check-pass
//@ check-pass
//! This is the reduced version of the "Linux kernel vtable" use-case.
#![feature(const_mut_refs, const_refs_to_static)]
#![feature(const_mut_refs)]
use std::ptr::addr_of_mut;

#[repr(C)]
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const_refs_to_static-ice-121413.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// issue: rust-lang/rust#121413
//@ compile-flags: -Zextra-const-ub-checks
// ignore-tidy-linelength
#![feature(const_refs_to_static)]
const REF_INTERIOR_MUT: &usize = {
//~^ HELP consider importing this struct
static FOO: Sync = AtomicUsize::new(0);
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/const_refs_to_static-ice-121413.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0433]: failed to resolve: use of undeclared type `AtomicUsize`
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
|
LL | static FOO: Sync = AtomicUsize::new(0);
| ^^^^^^^^^^^ use of undeclared type `AtomicUsize`
Expand All @@ -10,7 +10,7 @@ LL + use std::sync::atomic::AtomicUsize;
|

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

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

error[E0277]: the size for values of type `(dyn Sync + 'static)` cannot be known at compilation time
--> $DIR/const_refs_to_static-ice-121413.rs:9:24
--> $DIR/const_refs_to_static-ice-121413.rs:8:24
|
LL | static FOO: Sync = AtomicUsize::new(0);
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const_refs_to_static.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ run-pass
#![feature(const_refs_to_static)]

static S: i32 = 0;
static mut S_MUT: i32 = 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const_refs_to_static_fail.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ normalize-stderr-test: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
#![feature(const_refs_to_static, const_mut_refs, sync_unsafe_cell)]
#![feature(const_mut_refs, sync_unsafe_cell)]
use std::cell::SyncUnsafeCell;

static S: SyncUnsafeCell<i32> = SyncUnsafeCell::new(0);
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/const_refs_to_static_fail_invalid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ normalize-stderr-test: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
#![feature(const_refs_to_static)]
#![allow(static_mut_refs)]

fn invalid() {
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/consts/const_refs_to_static_fail_invalid.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refs_to_static_fail_invalid.rs:9:5
--> $DIR/const_refs_to_static_fail_invalid.rs:8:5
|
LL | const C: &bool = unsafe { std::mem::transmute(&S) };
| ^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered 0x0a, but expected a boolean
Expand All @@ -10,7 +10,7 @@ LL | const C: &bool = unsafe { std::mem::transmute(&S) };
}

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

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

error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:15:9
--> $DIR/const_refs_to_static_fail_invalid.rs:14:9
|
LL | C => {}
| ^

error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:31:9
--> $DIR/const_refs_to_static_fail_invalid.rs:30:9
|
LL | C => {}
| ^

error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:46:9
--> $DIR/const_refs_to_static_fail_invalid.rs:45:9
|
LL | C => {}
| ^
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/consts/issue-17718-const-bad-values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const C1: &'static mut [usize] = &mut [];

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

fn main() {}
14 changes: 1 addition & 13 deletions tests/ui/consts/issue-17718-const-bad-values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ error[E0764]: mutable references are not allowed in the final value of constants
LL | const C1: &'static mut [usize] = &mut [];
| ^^^^^^^

error[E0658]: referencing statics in constants is unstable
--> $DIR/issue-17718-const-bad-values.rs:7:46
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.

error[E0658]: mutable references are not allowed in constants
--> $DIR/issue-17718-const-bad-values.rs:7:41
|
Expand All @@ -26,7 +14,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

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

0 comments on commit 4cda52d

Please sign in to comment.