Skip to content

Commit

Permalink
Stabilize const_refs_to_static
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Sep 16, 2024
1 parent 170d6cb commit eeaaa21
Show file tree
Hide file tree
Showing 42 changed files with 171 additions and 390 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 @@ -317,7 +317,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
{
self.error_emitted = Some(guar);
}
self.check_op_spanned(ops::StaticAccess, span)
}

/// Returns whether this place can possibly escape the evaluation of the current const/static
Expand Down
28 changes: 0 additions & 28 deletions compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc_middle::ty::{
Param, TraitRef, Ty,
};
use rustc_middle::util::{call_kind, CallDesugaringKind, CallKind};
use rustc_session::parse::feature_err;
use rustc_span::symbol::sym;
use rustc_span::{BytePos, Pos, Span, Symbol};
use rustc_trait_selection::traits::SelectionContext;
Expand Down Expand Up @@ -477,33 +476,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 @@ -151,6 +151,8 @@ declare_features! (
(accepted, const_raw_ptr_deref, "1.58.0", Some(51911)),
/// Allows references to types with interior mutability within constants
(accepted, const_refs_to_cell, "CURRENT_RUSTC_VERSION", Some(80384)),
/// 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 @@ -405,8 +405,6 @@ declare_features! (
(unstable, const_for, "1.56.0", Some(87575)),
/// Be more precise when looking for live drops in a const context.
(unstable, const_precise_live_drops, "1.46.0", Some(73255)),
/// 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
10 changes: 5 additions & 5 deletions tests/ui/asm/aarch64/type-check-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ struct Simd256bit([f64; 4]);

fn main() {}

// Constants must be... constant
// Constants should be allowed to use the *value* of immutable statics.

static S: i32 = 1;
static mut S: i32 = 1;
const fn const_foo(x: i32) -> i32 {
x
}
const fn const_bar<T>(x: T) -> T {
x
}
global_asm!("{}", const S);
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
global_asm!("{}", const const_foo(0));
global_asm!("{}", const const_foo(S));
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
global_asm!("{}", const const_bar(0));
global_asm!("{}", const const_bar(S));
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
32 changes: 10 additions & 22 deletions tests/ui/asm/aarch64/type-check-4.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
error[E0658]: referencing statics in constants is unstable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:24:25
|
LL | global_asm!("{}", const S);
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error[E0658]: referencing statics in constants is unstable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:27:35
|
LL | global_asm!("{}", const const_foo(S));
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error[E0658]: referencing statics in constants is unstable
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:30:35
|
LL | global_asm!("{}", const const_bar(S));
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0133`.
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
13 changes: 5 additions & 8 deletions tests/ui/asm/x86_64/type-check-4.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
//@ only-x86_64
//@ compile-flags: -C target-feature=+avx512f

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

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;
static mut S: i32 = 1;
const fn const_foo(x: i32) -> i32 {
x
}
const fn const_bar<T>(x: T) -> T {
x
}
global_asm!("{}", const S);
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
global_asm!("{}", const const_foo(0));
global_asm!("{}", const const_foo(S));
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
global_asm!("{}", const const_bar(0));
global_asm!("{}", const const_bar(S));
//~^ ERROR referencing statics
//~^ ERROR: use of mutable static is unsafe
38 changes: 13 additions & 25 deletions tests/ui/asm/x86_64/type-check-4.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:19:25
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:16:25
|
LL | global_asm!("{}", const S);
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:22:35
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:19:35
|
LL | global_asm!("{}", const const_foo(S));
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:25:35
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
--> $DIR/type-check-4.rs:22:35
|
LL | global_asm!("{}", const const_bar(S));
| ^
| ^ use of mutable static
|
= 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: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
For more information about this error, try `rustc --explain E0133`.
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,6 @@
//@ 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_refs_to_static)]

use std::sync::Mutex;

Expand Down
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/mut_ref_in_final_dynamic_check.rs:19:1
--> $DIR/mut_ref_in_final_dynamic_check.rs:18:1
|
LL | const MUT: Option<&mut i32> = helper();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered reference to mutable memory in `const`
Expand All @@ -10,7 +10,7 @@ LL | const MUT: Option<&mut i32> = helper();
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/mut_ref_in_final_dynamic_check.rs:26:1
--> $DIR/mut_ref_in_final_dynamic_check.rs:25:1
|
LL | const INT2PTR: Option<&mut i32> = helper_int2ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (0x2a[noalloc] has no provenance)
Expand All @@ -21,7 +21,7 @@ LL | const INT2PTR: Option<&mut i32> = helper_int2ptr();
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/mut_ref_in_final_dynamic_check.rs:28:1
--> $DIR/mut_ref_in_final_dynamic_check.rs:27:1
|
LL | static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (0x2a[noalloc] has no provenance)
Expand All @@ -32,7 +32,7 @@ LL | static INT2PTR_STATIC: Option<&mut i32> = helper_int2ptr();
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/mut_ref_in_final_dynamic_check.rs:35:1
--> $DIR/mut_ref_in_final_dynamic_check.rs:34:1
|
LL | const DANGLING: Option<&mut i32> = helper_dangling();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (use-after-free)
Expand All @@ -43,7 +43,7 @@ LL | const DANGLING: Option<&mut i32> = helper_dangling();
}

error[E0080]: it is undefined behavior to use this value
--> $DIR/mut_ref_in_final_dynamic_check.rs:36:1
--> $DIR/mut_ref_in_final_dynamic_check.rs:35:1
|
LL | static DANGLING_STATIC: Option<&mut i32> = helper_dangling();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-variant(Some)>.0: encountered a dangling reference (use-after-free)
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_refs_to_static)]

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
Loading

0 comments on commit eeaaa21

Please sign in to comment.