Skip to content

Commit a84e2a0

Browse files
committed
add test for const-ref-to-cross-crate-mutable-static
1 parent e8ffa21 commit a84e2a0

7 files changed

+101
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub static mut ZERO: [u8; 1] = [0];

src/test/ui/consts/miri_unleashed/const_refers_to_static.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use std::sync::atomic::AtomicUsize;
88
use std::sync::atomic::Ordering;
99

10-
// These tests only cause an error when *using* the const.
10+
// These fail during CTFE (as they read a static), so they only cause an error
11+
// when *using* the const.
1112

1213
const MUTATE_INTERIOR_MUT: usize = {
1314
static FOO: AtomicUsize = AtomicUsize::new(0);

src/test/ui/consts/miri_unleashed/const_refers_to_static.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
warning: skipping const checks
2-
--> $DIR/const_refers_to_static.rs:14:5
2+
--> $DIR/const_refers_to_static.rs:15:5
33
|
44
LL | FOO.fetch_add(1, Ordering::Relaxed)
55
| ^^^
66

77
warning: skipping const checks
8-
--> $DIR/const_refers_to_static.rs:14:5
8+
--> $DIR/const_refers_to_static.rs:15:5
99
|
1010
LL | FOO.fetch_add(1, Ordering::Relaxed)
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
warning: skipping const checks
14-
--> $DIR/const_refers_to_static.rs:21:17
14+
--> $DIR/const_refers_to_static.rs:22:17
1515
|
1616
LL | unsafe { *(&FOO as *const _ as *const usize) }
1717
| ^^^
1818

1919
warning: skipping const checks
20-
--> $DIR/const_refers_to_static.rs:26:32
20+
--> $DIR/const_refers_to_static.rs:27:32
2121
|
2222
LL | const READ_MUT: u32 = unsafe { MUTABLE };
2323
| ^^^^^^^
2424

2525
warning: skipping const checks
26-
--> $DIR/const_refers_to_static.rs:26:32
26+
--> $DIR/const_refers_to_static.rs:27:32
2727
|
2828
LL | const READ_MUT: u32 = unsafe { MUTABLE };
2929
| ^^^^^^^
3030

3131
error[E0080]: erroneous constant used
32-
--> $DIR/const_refers_to_static.rs:31:5
32+
--> $DIR/const_refers_to_static.rs:32:5
3333
|
3434
LL | MUTATE_INTERIOR_MUT;
3535
| ^^^^^^^^^^^^^^^^^^^ referenced constant has errors
3636

3737
error[E0080]: erroneous constant used
38-
--> $DIR/const_refers_to_static.rs:33:5
38+
--> $DIR/const_refers_to_static.rs:34:5
3939
|
4040
LL | READ_INTERIOR_MUT;
4141
| ^^^^^^^^^^^^^^^^^ referenced constant has errors
4242

4343
error[E0080]: erroneous constant used
44-
--> $DIR/const_refers_to_static.rs:35:5
44+
--> $DIR/const_refers_to_static.rs:36:5
4545
|
4646
LL | READ_MUT;
4747
| ^^^^^^^^ referenced constant has errors

src/test/ui/consts/miri_unleashed/const_refers_to_static2.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
use std::sync::atomic::AtomicUsize;
77
use std::sync::atomic::Ordering;
88

9-
// These tests cause immediate error when *defining* the const.
9+
// These only fail during validation (they do not use but just create a reference to a static),
10+
// so they cause an immediate error when *defining* the const.
1011

1112
const REF_INTERIOR_MUT: &usize = { //~ ERROR undefined behavior to use this value
13+
//~| NOTE encountered a reference pointing to a static variable
14+
//~| NOTE
1215
static FOO: AtomicUsize = AtomicUsize::new(0);
1316
unsafe { &*(&FOO as *const _ as *const usize) }
1417
//~^ WARN skipping const checks
1518
};
1619

1720
// ok some day perhaps
1821
const READ_IMMUT: &usize = { //~ ERROR it is undefined behavior to use this value
22+
//~| NOTE encountered a reference pointing to a static variable
23+
//~| NOTE
1924
static FOO: usize = 0;
2025
&FOO
2126
//~^ WARN skipping const checks

src/test/ui/consts/miri_unleashed/const_refers_to_static2.stderr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
warning: skipping const checks
2-
--> $DIR/const_refers_to_static2.rs:13:18
2+
--> $DIR/const_refers_to_static2.rs:16:18
33
|
44
LL | unsafe { &*(&FOO as *const _ as *const usize) }
55
| ^^^
66

77
warning: skipping const checks
8-
--> $DIR/const_refers_to_static2.rs:20:6
8+
--> $DIR/const_refers_to_static2.rs:25:6
99
|
1010
LL | &FOO
1111
| ^^^
1212

1313
error[E0080]: it is undefined behavior to use this value
14-
--> $DIR/const_refers_to_static2.rs:11:1
14+
--> $DIR/const_refers_to_static2.rs:12:1
1515
|
1616
LL | / const REF_INTERIOR_MUT: &usize = {
17+
LL | |
18+
LL | |
1719
LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
1820
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
1921
LL | |
@@ -23,9 +25,11 @@ LL | | };
2325
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
2426

2527
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/const_refers_to_static2.rs:18:1
28+
--> $DIR/const_refers_to_static2.rs:21:1
2729
|
2830
LL | / const READ_IMMUT: &usize = {
31+
LL | |
32+
LL | |
2933
LL | | static FOO: usize = 0;
3034
LL | | &FOO
3135
LL | |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// compile-flags: -Zunleash-the-miri-inside-of-you
2+
// aux-build:static_cross_crate.rs
3+
#![allow(const_err)]
4+
5+
#![feature(exclusive_range_pattern)]
6+
#![feature(half_open_range_patterns)]
7+
8+
extern crate static_cross_crate;
9+
10+
// Sneaky: reference to a mutable static.
11+
// Allowing this would be a disaster for pattern matching, we could violate exhaustiveness checking!
12+
const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value
13+
//~| NOTE encountered a reference pointing to a static variable
14+
//~| NOTE
15+
unsafe { &static_cross_crate::ZERO }
16+
//~^ WARN skipping const checks
17+
//~| WARN skipping const checks
18+
};
19+
20+
pub fn test(x: &[u8; 1]) -> bool {
21+
match x {
22+
SLICE_MUT => true,
23+
//~^ ERROR could not evaluate constant pattern
24+
//~| ERROR could not evaluate constant pattern
25+
&[1..] => false,
26+
}
27+
}
28+
29+
fn main() {
30+
unsafe {
31+
static_cross_crate::ZERO[0] = 1;
32+
}
33+
// Now the pattern is not exhaustive any more!
34+
test(&[0]);
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
warning: skipping const checks
2+
--> $DIR/const_refers_to_static_cross_crate.rs:15:15
3+
|
4+
LL | unsafe { &static_cross_crate::ZERO }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
warning: skipping const checks
8+
--> $DIR/const_refers_to_static_cross_crate.rs:15:15
9+
|
10+
LL | unsafe { &static_cross_crate::ZERO }
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0080]: it is undefined behavior to use this value
14+
--> $DIR/const_refers_to_static_cross_crate.rs:12:1
15+
|
16+
LL | / const SLICE_MUT: &[u8; 1] = {
17+
LL | |
18+
LL | |
19+
LL | | unsafe { &static_cross_crate::ZERO }
20+
LL | |
21+
LL | |
22+
LL | | };
23+
| |__^ type validation failed: encountered a reference pointing to a static variable
24+
|
25+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
26+
27+
error: could not evaluate constant pattern
28+
--> $DIR/const_refers_to_static_cross_crate.rs:22:9
29+
|
30+
LL | SLICE_MUT => true,
31+
| ^^^^^^^^^
32+
33+
error: could not evaluate constant pattern
34+
--> $DIR/const_refers_to_static_cross_crate.rs:22:9
35+
|
36+
LL | SLICE_MUT => true,
37+
| ^^^^^^^^^
38+
39+
error: aborting due to 3 previous errors; 2 warnings emitted
40+
41+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)