Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize const_refs_to_static #129759

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::{
suggest_constraining_type_param,
};
use rustc_middle::util::{CallDesugaringKind, CallKind, call_kind};
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 @@ -407,8 +407,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
2 changes: 0 additions & 2 deletions src/tools/tidy/src/issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ ui/consts/issue-46553.rs
ui/consts/issue-47789.rs
ui/consts/issue-50439.rs
ui/consts/issue-52023-array-size-pointer-cast.rs
ui/consts/issue-52060.rs
ui/consts/issue-54224.rs
ui/consts/issue-54348.rs
ui/consts/issue-54387.rs
Expand Down Expand Up @@ -3830,7 +3829,6 @@ ui/stability-attribute/issue-28388-3.rs
ui/stability-attribute/issue-99286-stable-intrinsics.rs
ui/static/auxiliary/issue_24843.rs
ui/static/issue-1660.rs
ui/static/issue-18118-2.rs
ui/static/issue-18118.rs
ui/static/issue-24446.rs
ui/static/issue-24843.rs
Expand Down
28 changes: 12 additions & 16 deletions tests/ui/asm/aarch64/type-check-4.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
//@ only-aarch64
//@ compile-flags: -C target-feature=+neon
//@ build-fail

#![feature(repr_simd)]

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

#[repr(simd)]
#[derive(Copy, Clone)]
struct Simd256bit([f64; 4]);
use std::arch::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
global_asm!("{}", const unsafe { S });
//~^ ERROR: evaluation of constant value failed
//~| mutable global memory
global_asm!("{}", const const_foo(0));
global_asm!("{}", const const_foo(S));
//~^ ERROR referencing statics
global_asm!("{}", const const_foo(unsafe { S }));
//~^ ERROR: evaluation of constant value failed
//~| mutable global memory
global_asm!("{}", const const_bar(0));
global_asm!("{}", const const_bar(S));
//~^ ERROR referencing statics
global_asm!("{}", const const_bar(unsafe { S }));
//~^ ERROR: evaluation of constant value failed
//~| mutable global memory
44 changes: 13 additions & 31 deletions tests/ui/asm/aarch64/type-check-4.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:24:25
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:17:34
|
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.
LL | global_asm!("{}", const unsafe { S });
| ^ constant accesses mutable global memory

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:27:35
|
LL | global_asm!("{}", const const_foo(S));
| ^
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:21:44
|
= 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.
LL | global_asm!("{}", const const_foo(unsafe { S }));
| ^ constant accesses mutable global memory

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:30:35
|
LL | global_asm!("{}", const const_bar(S));
| ^
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:25:44
|
= 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.
LL | global_asm!("{}", const const_bar(unsafe { S }));
| ^ constant accesses mutable global memory

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 E0080`.
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
23 changes: 12 additions & 11 deletions tests/ui/asm/x86_64/type-check-4.rs
Copy link
Member

@RalfJung RalfJung Sep 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk you added this as a failing ui test, but now it no longer fails. Is this test intended to check "what happens when an asm block references a failing const", or something else? It's a bit unclear what this test is even intended to test. But if it is about asm + failing const, we should probably adjust the test so that it still shows an error, we just need a different operation to cause the error?

Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
//@ only-x86_64
//@ compile-flags: -C target-feature=+avx512f
//@ build-fail

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

use std::arch::x86_64::{_mm256_setzero_ps, _mm_setzero_ps};
use std::arch::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
global_asm!("{}", const unsafe { S });
//~^ ERROR evaluation of constant value failed
//~| mutable global memory
global_asm!("{}", const const_foo(0));
global_asm!("{}", const const_foo(S));
//~^ ERROR referencing statics
global_asm!("{}", const const_foo(unsafe { S }));
//~^ ERROR evaluation of constant value failed
//~| mutable global memory
global_asm!("{}", const const_bar(0));
global_asm!("{}", const const_bar(S));
//~^ ERROR referencing statics
global_asm!("{}", const const_bar(unsafe { S }));
//~^ ERROR evaluation of constant value failed
//~| mutable global memory
44 changes: 13 additions & 31 deletions tests/ui/asm/x86_64/type-check-4.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:19:25
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:17:34
|
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.
LL | global_asm!("{}", const unsafe { S });
| ^ constant accesses mutable global memory

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:22:35
|
LL | global_asm!("{}", const const_foo(S));
| ^
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:21:44
|
= 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.
LL | global_asm!("{}", const const_foo(unsafe { S }));
| ^ constant accesses mutable global memory

error[E0658]: referencing statics in constants is unstable
--> $DIR/type-check-4.rs:25:35
|
LL | global_asm!("{}", const const_bar(S));
| ^
error[E0080]: evaluation of constant value failed
--> $DIR/type-check-4.rs:25:44
|
= 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.
LL | global_asm!("{}", const const_bar(unsafe { S }));
| ^ constant accesses mutable global memory

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 E0080`.
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
Loading
Loading