Skip to content

Commit 774445d

Browse files
committed
Allow constants to refer statics
1 parent 9248b01 commit 774445d

16 files changed

+13
-161
lines changed

src/librustc_mir/error_codes.rs

-21
Original file line numberDiff line numberDiff line change
@@ -228,27 +228,6 @@ const CON : Box<i32> = box 0;
228228
```
229229
"##,
230230

231-
E0013: r##"
232-
Static and const variables can refer to other const variables. But a const
233-
variable cannot refer to a static variable.
234-
235-
Erroneous code example:
236-
237-
```compile_fail,E0013
238-
static X: i32 = 42;
239-
const Y: i32 = X;
240-
```
241-
242-
In this example, `Y` cannot refer to `X` here. To fix this, the value can be
243-
extracted as a const and then used:
244-
245-
```
246-
const A: i32 = 42;
247-
static X: i32 = A;
248-
const Y: i32 = A;
249-
```
250-
"##,
251-
252231
// FIXME(#57563) Change the language here when const fn stabilizes
253232
E0015: r##"
254233
The only functions that can be called in static or constant expressions are

src/librustc_mir/transform/check_consts/ops.rs

-25
Original file line numberDiff line numberDiff line change
@@ -262,31 +262,6 @@ impl NonConstOp for RawPtrToIntCast {
262262
}
263263
}
264264

265-
/// An access to a (non-thread-local) `static`.
266-
#[derive(Debug)]
267-
pub struct StaticAccess;
268-
impl NonConstOp for StaticAccess {
269-
fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool {
270-
item.const_kind().is_static()
271-
}
272-
273-
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
274-
let mut err = struct_span_err!(item.tcx.sess, span, E0013,
275-
"{}s cannot refer to statics, use \
276-
a constant instead", item.const_kind());
277-
if item.tcx.sess.teach(&err.get_code().unwrap()) {
278-
err.note(
279-
"Static and const variables can refer to other const variables. \
280-
But a const variable cannot refer to a static variable."
281-
);
282-
err.help(
283-
"To fix this, the value can be extracted as a const and then used."
284-
);
285-
}
286-
err.emit();
287-
}
288-
}
289-
290265
/// An access to a thread-local `static`.
291266
#[derive(Debug)]
292267
pub struct ThreadLocalAccess;

src/librustc_mir/transform/check_consts/validation.rs

-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
336336
self.span,
337337
"cannot mutate statics in the initializer of another static",
338338
);
339-
} else {
340-
self.check_op(ops::StaticAccess);
341339
}
342340
}
343341
}

src/librustc_mir/transform/qualify_consts.rs

-17
Original file line numberDiff line numberDiff line change
@@ -798,23 +798,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
798798
return;
799799
}
800800
unleash_miri!(self);
801-
802-
if self.mode.requires_const_checking() && !self.suppress_errors {
803-
self.record_error(ops::StaticAccess);
804-
let mut err = struct_span_err!(self.tcx.sess, self.span, E0013,
805-
"{}s cannot refer to statics, use \
806-
a constant instead", self.mode);
807-
if self.tcx.sess.teach(&err.get_code().unwrap()) {
808-
err.note(
809-
"Static and const variables can refer to other const variables. \
810-
But a const variable cannot refer to a static variable."
811-
);
812-
err.help(
813-
"To fix this, the value can be extracted as a const and then used."
814-
);
815-
}
816-
err.emit()
817-
}
818801
}
819802
}
820803
}

src/test/ui/consts/const-fn-not-safe-for-const.rs

-2
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 E0013
2221
}
2322

2423
const fn get_Y_addr() -> &'static u32 {
2524
&Y
26-
//~^ ERROR E0013
2725
}
2826

2927
const fn get() -> u32 {

src/test/ui/consts/const-fn-not-safe-for-const.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ error[E0015]: calls in constant functions are limited to constant functions, tup
44
LL | random()
55
| ^^^^^^^^
66

7-
error[E0013]: constant functions cannot refer to statics, use a constant instead
8-
--> $DIR/const-fn-not-safe-for-const.rs:20:5
9-
|
10-
LL | Y
11-
| ^
12-
13-
error[E0013]: constant functions cannot refer to statics, use a constant instead
14-
--> $DIR/const-fn-not-safe-for-const.rs:25:5
15-
|
16-
LL | &Y
17-
| ^^
18-
19-
error: aborting due to 3 previous errors
7+
error: aborting due to previous error
208

21-
Some errors have detailed explanations: E0013, E0015.
22-
For more information about an error, try `rustc --explain E0013`.
9+
For more information about this error, try `rustc --explain E0015`.

src/test/ui/consts/const-prop-read-static-in-const.rs

-12
This file was deleted.

src/test/ui/consts/const-prop-read-static-in-const.stderr

-6
This file was deleted.

src/test/ui/issues/issue-17718-const-bad-values.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const C1: &'static mut [usize] = &mut [];
33

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

98
fn main() {}

src/test/ui/issues/issue-17718-const-bad-values.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ error[E0017]: references in constants may only refer to immutable values
1010
LL | const C2: &'static mut usize = unsafe { &mut S };
1111
| ^^^^^^ constants require immutable values
1212

13-
error[E0013]: constants cannot refer to statics, use a constant instead
14-
--> $DIR/issue-17718-const-bad-values.rs:5:41
15-
|
16-
LL | const C2: &'static mut usize = unsafe { &mut S };
17-
| ^^^^^^
18-
19-
error: aborting due to 3 previous errors
13+
error: aborting due to 2 previous errors
2014

21-
Some errors have detailed explanations: E0013, E0017.
22-
For more information about an error, try `rustc --explain E0013`.
15+
For more information about this error, try `rustc --explain E0017`.

src/test/ui/issues/issue-17718-references.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// build-pass
2+
13
#![allow(warnings)]
24

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

810
const T1: &'static usize = &C;
9-
const T2: &'static usize = &S; //~ ERROR: constants cannot refer to statics
11+
const T2: &'static usize = &S;
1012
static T3: &'static usize = &C;
1113
static T4: &'static usize = &S;
1214

1315
const T5: usize = C;
14-
const T6: usize = S; //~ ERROR: constants cannot refer to statics
16+
const T6: usize = S;
1517
static T7: usize = C;
1618
static T8: usize = S;
1719

1820
const T9: Struct = Struct { a: C };
1921
const T10: Struct = Struct { a: S };
20-
//~^ ERROR: constants cannot refer to statics
2122
static T11: Struct = Struct { a: C };
2223
static T12: Struct = Struct { a: S };
2324

src/test/ui/issues/issue-17718-references.stderr

-21
This file was deleted.

src/test/ui/issues/issue-18118-2.rs

-7
This file was deleted.

src/test/ui/issues/issue-18118-2.stderr

-9
This file was deleted.

src/test/ui/issues/issue-52060.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// build-pass
2+
3+
#![allow(dead_code)]
4+
15
// Regression test for https://github.com/rust-lang/rust/issues/52060
26
// The compiler shouldn't ICE in this case
37
static A: &'static [u32] = &[1];
48
static B: [u32; 1] = [0; A.len()];
5-
//~^ ERROR [E0013]
69

710
fn main() {}

src/test/ui/issues/issue-52060.stderr

-9
This file was deleted.

0 commit comments

Comments
 (0)