Skip to content

Commit a03355d

Browse files
committed
some more test cases
1 parent 979bbf2 commit a03355d

File tree

3 files changed

+109
-14
lines changed

3 files changed

+109
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
pub static mut ZERO: [u8; 1] = [0];
2+
pub static ZERO_REF: &[u8; 1] = unsafe { &ZERO };
3+
pub static mut OPT_ZERO: Option<u8> = Some(0);

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

+37-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// aux-build:static_cross_crate.rs
33
#![allow(const_err)]
44

5-
#![feature(exclusive_range_pattern)]
6-
#![feature(half_open_range_patterns)]
5+
#![feature(exclusive_range_pattern, half_open_range_patterns, const_if_match, const_panic)]
76

87
extern crate static_cross_crate;
98

@@ -16,13 +15,29 @@ const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value
1615
//~^ WARN skipping const checks
1716
};
1817

19-
const SLICE_MUT2: &u8 = { //~ ERROR undefined behavior to use this value
18+
const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value
2019
//~| NOTE encountered a reference pointing to a static variable
2120
//~| NOTE
2221
unsafe { &static_cross_crate::ZERO[0] }
2322
//~^ WARN skipping const checks
2423
};
2524

25+
// Also test indirection that reads from other static. This causes a const_err.
26+
#[warn(const_err)] //~ NOTE
27+
const U8_MUT2: &u8 = { //~ NOTE
28+
unsafe { &(*static_cross_crate::ZERO_REF)[0] }
29+
//~^ WARN skipping const checks
30+
//~| WARN [const_err]
31+
//~| NOTE constant accesses static
32+
};
33+
#[warn(const_err)] //~ NOTE
34+
const U8_MUT3: &u8 = { //~ NOTE
35+
unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
36+
//~^ WARN skipping const checks
37+
//~| WARN [const_err]
38+
//~| NOTE constant accesses static
39+
};
40+
2641
pub fn test(x: &[u8; 1]) -> bool {
2742
match x {
2843
SLICE_MUT => true,
@@ -33,7 +48,24 @@ pub fn test(x: &[u8; 1]) -> bool {
3348

3449
pub fn test2(x: &u8) -> bool {
3550
match x {
36-
SLICE_MUT2 => true,
51+
U8_MUT => true,
52+
//~^ ERROR could not evaluate constant pattern
53+
&(1..) => false,
54+
}
55+
}
56+
57+
// We need to use these *in a pattern* to trigger the failure... likely because
58+
// the errors above otherwise stop compilation too early?
59+
pub fn test3(x: &u8) -> bool {
60+
match x {
61+
U8_MUT2 => true,
62+
//~^ ERROR could not evaluate constant pattern
63+
&(1..) => false,
64+
}
65+
}
66+
pub fn test4(x: &u8) -> bool {
67+
match x {
68+
U8_MUT3 => true,
3769
//~^ ERROR could not evaluate constant pattern
3870
&(1..) => false,
3971
}
@@ -45,4 +77,5 @@ fn main() {
4577
}
4678
// Now the pattern is not exhaustive any more!
4779
test(&[0]);
80+
test2(&0);
4881
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
warning: skipping const checks
2-
--> $DIR/const_refers_to_static_cross_crate.rs:15:15
2+
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
33
|
44
LL | unsafe { &static_cross_crate::ZERO }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error[E0080]: it is undefined behavior to use this value
8-
--> $DIR/const_refers_to_static_cross_crate.rs:12:1
8+
--> $DIR/const_refers_to_static_cross_crate.rs:11:1
99
|
1010
LL | / const SLICE_MUT: &[u8; 1] = {
1111
LL | |
@@ -18,21 +18,21 @@ LL | | };
1818
= 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.
1919

2020
error: could not evaluate constant pattern
21-
--> $DIR/const_refers_to_static_cross_crate.rs:28:9
21+
--> $DIR/const_refers_to_static_cross_crate.rs:43:9
2222
|
2323
LL | SLICE_MUT => true,
2424
| ^^^^^^^^^
2525

2626
warning: skipping const checks
27-
--> $DIR/const_refers_to_static_cross_crate.rs:22:15
27+
--> $DIR/const_refers_to_static_cross_crate.rs:21:15
2828
|
2929
LL | unsafe { &static_cross_crate::ZERO[0] }
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^
3131

3232
error[E0080]: it is undefined behavior to use this value
33-
--> $DIR/const_refers_to_static_cross_crate.rs:19:1
33+
--> $DIR/const_refers_to_static_cross_crate.rs:18:1
3434
|
35-
LL | / const SLICE_MUT2: &u8 = {
35+
LL | / const U8_MUT: &u8 = {
3636
LL | |
3737
LL | |
3838
LL | | unsafe { &static_cross_crate::ZERO[0] }
@@ -43,11 +43,71 @@ LL | | };
4343
= 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.
4444

4545
error: could not evaluate constant pattern
46-
--> $DIR/const_refers_to_static_cross_crate.rs:36:9
46+
--> $DIR/const_refers_to_static_cross_crate.rs:51:9
4747
|
48-
LL | SLICE_MUT2 => true,
49-
| ^^^^^^^^^^
48+
LL | U8_MUT => true,
49+
| ^^^^^^
5050

51-
error: aborting due to 4 previous errors; 2 warnings emitted
51+
warning: skipping const checks
52+
--> $DIR/const_refers_to_static_cross_crate.rs:28:17
53+
|
54+
LL | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
warning: any use of this value will cause an error
58+
--> $DIR/const_refers_to_static_cross_crate.rs:28:14
59+
|
60+
LL | / const U8_MUT2: &u8 = {
61+
LL | | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
62+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static
63+
LL | |
64+
LL | |
65+
LL | |
66+
LL | | };
67+
| |__-
68+
|
69+
note: the lint level is defined here
70+
--> $DIR/const_refers_to_static_cross_crate.rs:26:8
71+
|
72+
LL | #[warn(const_err)]
73+
| ^^^^^^^^^
74+
75+
error: could not evaluate constant pattern
76+
--> $DIR/const_refers_to_static_cross_crate.rs:61:9
77+
|
78+
LL | U8_MUT2 => true,
79+
| ^^^^^^^
80+
81+
warning: skipping const checks
82+
--> $DIR/const_refers_to_static_cross_crate.rs:35:20
83+
|
84+
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
87+
warning: any use of this value will cause an error
88+
--> $DIR/const_refers_to_static_cross_crate.rs:35:51
89+
|
90+
LL | / const U8_MUT3: &u8 = {
91+
LL | | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
92+
| | ^^^^^^^^^^^ constant accesses static
93+
LL | |
94+
LL | |
95+
LL | |
96+
LL | | };
97+
| |__-
98+
|
99+
note: the lint level is defined here
100+
--> $DIR/const_refers_to_static_cross_crate.rs:33:8
101+
|
102+
LL | #[warn(const_err)]
103+
| ^^^^^^^^^
104+
105+
error: could not evaluate constant pattern
106+
--> $DIR/const_refers_to_static_cross_crate.rs:68:9
107+
|
108+
LL | U8_MUT3 => true,
109+
| ^^^^^^^
110+
111+
error: aborting due to 6 previous errors; 6 warnings emitted
52112

53113
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)