Skip to content

Allow constants to refer statics #66302

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

Closed
Closed
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
21 changes: 0 additions & 21 deletions src/librustc_mir/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,27 +228,6 @@ const CON : Box<i32> = box 0;
```
"##,

E0013: r##"
Static and const variables can refer to other const variables. But a const
variable cannot refer to a static variable.

Erroneous code example:

```compile_fail,E0013
static X: i32 = 42;
const Y: i32 = X;
```

In this example, `Y` cannot refer to `X` here. To fix this, the value can be
extracted as a const and then used:

```
const A: i32 = 42;
static X: i32 = A;
const Y: i32 = A;
```
"##,

// FIXME(#57563) Change the language here when const fn stabilizes
E0015: r##"
The only functions that can be called in static or constant expressions are
Expand Down
25 changes: 0 additions & 25 deletions src/librustc_mir/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,31 +262,6 @@ impl NonConstOp for RawPtrToIntCast {
}
}

/// An access to a (non-thread-local) `static`.
#[derive(Debug)]
pub struct StaticAccess;
impl NonConstOp for StaticAccess {
fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool {
item.const_kind().is_static()
}

fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
let mut err = struct_span_err!(item.tcx.sess, span, E0013,
"{}s cannot refer to statics, use \
a constant instead", item.const_kind());
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(
"Static and const variables can refer to other const variables. \
But a const variable cannot refer to a static variable."
);
err.help(
"To fix this, the value can be extracted as a const and then used."
);
}
err.emit();
}
}

/// An access to a thread-local `static`.
#[derive(Debug)]
pub struct ThreadLocalAccess;
Expand Down
2 changes: 0 additions & 2 deletions src/librustc_mir/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
self.span,
"cannot mutate statics in the initializer of another static",
);
} else {
self.check_op(ops::StaticAccess);
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,23 +798,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
return;
}
unleash_miri!(self);

if self.mode.requires_const_checking() && !self.suppress_errors {
self.record_error(ops::StaticAccess);
let mut err = struct_span_err!(self.tcx.sess, self.span, E0013,
"{}s cannot refer to statics, use \
a constant instead", self.mode);
if self.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(
"Static and const variables can refer to other const variables. \
But a const variable cannot refer to a static variable."
);
err.help(
"To fix this, the value can be extracted as a const and then used."
);
}
err.emit()
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/consts/const-can-refer-to-sync-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// build-pass

use std::sync::atomic::AtomicUsize;

static FOO: AtomicUsize = AtomicUsize::new(0);

const X: &AtomicUsize = &FOO;

fn main() {}
2 changes: 0 additions & 2 deletions src/test/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 E0013
}

const fn get_Y_addr() -> &'static u32 {
&Y
//~^ ERROR E0013
}

const fn get() -> u32 {
Expand Down
17 changes: 2 additions & 15 deletions src/test/ui/consts/const-fn-not-safe-for-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ error[E0015]: calls in constant functions are limited to constant functions, tup
LL | random()
| ^^^^^^^^

error[E0013]: constant functions cannot refer to statics, use a constant instead
--> $DIR/const-fn-not-safe-for-const.rs:20:5
|
LL | Y
| ^

error[E0013]: constant functions cannot refer to statics, use a constant instead
--> $DIR/const-fn-not-safe-for-const.rs:25:5
|
LL | &Y
| ^^

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

Some errors have detailed explanations: E0013, E0015.
For more information about an error, try `rustc --explain E0013`.
For more information about this error, try `rustc --explain E0015`.
12 changes: 0 additions & 12 deletions src/test/ui/consts/const-prop-read-static-in-const.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/ui/consts/const-prop-read-static-in-const.stderr

This file was deleted.

10 changes: 10 additions & 0 deletions src/test/ui/consts/const-read-extern-static-from-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(dead_code)]

extern {
static FOO: u8;
}

const X: u8 = unsafe { FOO };
//~^ any use of this value will cause an error [const_err]

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/consts/const-read-extern-static-from-const.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: any use of this value will cause an error
--> $DIR/const-read-extern-static-from-const.rs:7:24
|
LL | const X: u8 = unsafe { FOO };
| -----------------------^^^---
| |
| tried to read from foreign (extern) static
|
= note: `#[deny(const_err)]` on by default

error: aborting due to previous error

3 changes: 1 addition & 2 deletions src/test/ui/issues/issue-17718-const-bad-values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const C1: &'static mut [usize] = &mut [];

static mut S: usize = 3;
const C2: &'static mut usize = unsafe { &mut S };
//~^ ERROR: constants cannot refer to statics
//~| ERROR: references in constants may only refer to immutable values
//~^ ERROR: references in constants may only refer to immutable values

fn main() {}
11 changes: 2 additions & 9 deletions src/test/ui/issues/issue-17718-const-bad-values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ error[E0017]: references in constants may only refer to immutable values
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^^^^^^ constants require immutable values

error[E0013]: constants cannot refer to statics, use a constant instead
--> $DIR/issue-17718-const-bad-values.rs:5:41
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^^^^^^

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

Some errors have detailed explanations: E0013, E0017.
For more information about an error, try `rustc --explain E0013`.
For more information about this error, try `rustc --explain E0017`.
7 changes: 4 additions & 3 deletions src/test/ui/issues/issue-17718-references.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// build-pass

#![allow(warnings)]

struct Struct { a: usize }
Expand All @@ -6,18 +8,17 @@ const C: usize = 1;
static S: usize = 1;

const T1: &'static usize = &C;
const T2: &'static usize = &S; //~ ERROR: constants cannot refer to statics
const T2: &'static usize = &S;
static T3: &'static usize = &C;
static T4: &'static usize = &S;

const T5: usize = C;
const T6: usize = S; //~ ERROR: constants cannot refer to statics
const T6: usize = S;
static T7: usize = C;
static T8: usize = S;

const T9: Struct = Struct { a: C };
const T10: Struct = Struct { a: S };
//~^ ERROR: constants cannot refer to statics
static T11: Struct = Struct { a: C };
static T12: Struct = Struct { a: S };

Expand Down
21 changes: 0 additions & 21 deletions src/test/ui/issues/issue-17718-references.stderr

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/ui/issues/issue-18118-2.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/test/ui/issues/issue-18118-2.stderr

This file was deleted.

5 changes: 4 additions & 1 deletion src/test/ui/issues/issue-52060.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// build-pass

#![allow(dead_code)]

// Regression test for https://github.com/rust-lang/rust/issues/52060
// The compiler shouldn't ICE in this case
static A: &'static [u32] = &[1];
static B: [u32; 1] = [0; A.len()];
//~^ ERROR [E0013]

fn main() {}
9 changes: 0 additions & 9 deletions src/test/ui/issues/issue-52060.stderr

This file was deleted.