Skip to content

Commit 3f46b82

Browse files
committed
Auto merge of #85332 - RalfJung:ptr-in-str, r=oli-obk
CTFE validation: handle pointers in str I also finally learned how I can match *some* NOTEs in a ui test without matching all of them, and applied that to some const tests in the 2nd commit where I added NOTE because I did not know what I was doing. I can separate this into its own PR if you prefer. Fixes #83182 r? `@oli-obk`
2 parents 94ecdfd + 70c1cf1 commit 3f46b82

15 files changed

+111
-119
lines changed

compiler/rustc_mir/src/interpret/validity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
833833
self.ecx.memory.read_bytes(mplace.ptr, Size::from_bytes(len)),
834834
self.path,
835835
err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" },
836+
err_unsup!(ReadPointerAsBytes) => { "a pointer in `str`" },
836837
);
837838
}
838839
ty::Array(tys, ..) | ty::Slice(tys)

src/test/ui/consts/const-eval/dangling.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
use std::mem;
44

55
// Make sure we error with the right kind of error on a too large slice.
6-
const TEST: () = { unsafe { //~ NOTE
6+
const TEST: () = { unsafe {
77
let slice: *const [u8] = mem::transmute((1usize, usize::MAX));
88
let _val = &*slice; //~ ERROR: any use of this value will cause an error
9-
//~| NOTE: slice is bigger than largest supported object
10-
//~| on by default
9+
//~| slice is bigger than largest supported object
1110
//~| WARN this was previously accepted by the compiler but is being phased out
12-
//~| NOTE
1311
} };
1412

1513
fn main() {}

src/test/ui/consts/const-eval/dangling.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | | let slice: *const [u8] = mem::transmute((1usize, usize::MAX));
66
LL | | let _val = &*slice;
77
| | ^^^^^^^ invalid metadata in wide pointer: slice is bigger than largest supported object
88
LL | |
9-
... |
109
LL | |
1110
LL | | } };
1211
| |____-

src/test/ui/consts/const-points-to-static.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
const TEST: &u8 = &MY_STATIC;
77
//~^ ERROR it is undefined behavior to use this value
8-
//~| NOTE encountered a reference pointing to a static variable
9-
//~| NOTE undefined behavior
10-
//~| NOTE the raw bytes of the constant
8+
//~| encountered a reference pointing to a static variable
119

1210
static MY_STATIC: u8 = 4;
1311

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/issue-83182.rs:5:1
3+
|
4+
LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer in `str` at .<deref>.0
6+
|
7+
= 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.
8+
= note: the raw bytes of the constant (size: 8, align: 4) {
9+
╾─alloc3──╼ 01 00 00 00 │ ╾──╼....
10+
}
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/issue-83182.rs:5:1
3+
|
4+
LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer in `str` at .<deref>.0
6+
|
7+
= 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.
8+
= note: the raw bytes of the constant (size: 16, align: 8) {
9+
╾───────alloc3────────╼ 01 00 00 00 00 00 00 00 │ ╾──────╼........
10+
}
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/issue-83182.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// stderr-per-bitwidth
2+
3+
use std::mem;
4+
struct MyStr(str);
5+
const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[&()]) };
6+
//~^ ERROR: it is undefined behavior to use this value
7+
//~| type validation failed: encountered a pointer in `str`
8+
fn main() {}

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0080]: it is undefined behavior to use this value
33
|
44
LL | / const REF_INTERIOR_MUT: &usize = {
55
LL | |
6-
LL | |
7-
LL | |
86
LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
97
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
108
LL | | };
@@ -16,12 +14,10 @@ LL | | };
1614
}
1715

1816
error[E0080]: it is undefined behavior to use this value
19-
--> $DIR/const_refers_to_static2.rs:20:1
17+
--> $DIR/const_refers_to_static2.rs:18:1
2018
|
2119
LL | / const READ_IMMUT: &usize = {
2220
LL | |
23-
LL | |
24-
LL | |
2521
LL | | static FOO: usize = 0;
2622
LL | | &FOO
2723
LL | | };
@@ -35,17 +31,17 @@ LL | | };
3531
warning: skipping const checks
3632
|
3733
help: skipping check that does not even have a feature gate
38-
--> $DIR/const_refers_to_static2.rs:16:18
34+
--> $DIR/const_refers_to_static2.rs:14:18
3935
|
4036
LL | unsafe { &*(&FOO as *const _ as *const usize) }
4137
| ^^^
4238
help: skipping check for `const_raw_ptr_deref` feature
43-
--> $DIR/const_refers_to_static2.rs:16:14
39+
--> $DIR/const_refers_to_static2.rs:14:14
4440
|
4541
LL | unsafe { &*(&FOO as *const _ as *const usize) }
4642
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4743
help: skipping check that does not even have a feature gate
48-
--> $DIR/const_refers_to_static2.rs:25:6
44+
--> $DIR/const_refers_to_static2.rs:21:6
4945
|
5046
LL | &FOO
5147
| ^^^

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0080]: it is undefined behavior to use this value
33
|
44
LL | / const REF_INTERIOR_MUT: &usize = {
55
LL | |
6-
LL | |
7-
LL | |
86
LL | | static FOO: AtomicUsize = AtomicUsize::new(0);
97
LL | | unsafe { &*(&FOO as *const _ as *const usize) }
108
LL | | };
@@ -16,12 +14,10 @@ LL | | };
1614
}
1715

1816
error[E0080]: it is undefined behavior to use this value
19-
--> $DIR/const_refers_to_static2.rs:20:1
17+
--> $DIR/const_refers_to_static2.rs:18:1
2018
|
2119
LL | / const READ_IMMUT: &usize = {
2220
LL | |
23-
LL | |
24-
LL | |
2521
LL | | static FOO: usize = 0;
2622
LL | | &FOO
2723
LL | | };
@@ -35,17 +31,17 @@ LL | | };
3531
warning: skipping const checks
3632
|
3733
help: skipping check that does not even have a feature gate
38-
--> $DIR/const_refers_to_static2.rs:16:18
34+
--> $DIR/const_refers_to_static2.rs:14:18
3935
|
4036
LL | unsafe { &*(&FOO as *const _ as *const usize) }
4137
| ^^^
4238
help: skipping check for `const_raw_ptr_deref` feature
43-
--> $DIR/const_refers_to_static2.rs:16:14
39+
--> $DIR/const_refers_to_static2.rs:14:14
4440
|
4541
LL | unsafe { &*(&FOO as *const _ as *const usize) }
4642
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4743
help: skipping check that does not even have a feature gate
48-
--> $DIR/const_refers_to_static2.rs:25:6
44+
--> $DIR/const_refers_to_static2.rs:21:6
4945
|
5046
LL | &FOO
5147
| ^^^

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ use std::sync::atomic::Ordering;
99
// so they cause an immediate error when *defining* the const.
1010

1111
const REF_INTERIOR_MUT: &usize = { //~ ERROR undefined behavior to use this value
12-
//~| NOTE encountered a reference pointing to a static variable
13-
//~| NOTE undefined behavior
14-
//~| NOTE the raw bytes of the constant
12+
//~| encountered a reference pointing to a static variable
1513
static FOO: AtomicUsize = AtomicUsize::new(0);
1614
unsafe { &*(&FOO as *const _ as *const usize) }
1715
};
1816

1917
// ok some day perhaps
2018
const READ_IMMUT: &usize = { //~ ERROR it is undefined behavior to use this value
21-
//~| NOTE encountered a reference pointing to a static variable
22-
//~| NOTE undefined behavior
23-
//~| NOTE the raw bytes of the constant
19+
//~| encountered a reference pointing to a static variable
2420
static FOO: usize = 0;
2521
&FOO
2622
};

src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.32bit.stderr

+24-30
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0080]: it is undefined behavior to use this value
33
|
44
LL | / const SLICE_MUT: &[u8; 1] = {
55
LL | |
6-
LL | |
7-
LL | |
86
LL | | unsafe { &static_cross_crate::ZERO }
97
LL | | };
108
| |__^ type validation failed: encountered a reference pointing to a static variable
@@ -15,18 +13,16 @@ LL | | };
1513
}
1614

1715
error: could not evaluate constant pattern
18-
--> $DIR/const_refers_to_static_cross_crate.rs:47:9
16+
--> $DIR/const_refers_to_static_cross_crate.rs:40:9
1917
|
2018
LL | SLICE_MUT => true,
2119
| ^^^^^^^^^
2220

2321
error[E0080]: it is undefined behavior to use this value
24-
--> $DIR/const_refers_to_static_cross_crate.rs:19:1
22+
--> $DIR/const_refers_to_static_cross_crate.rs:17:1
2523
|
2624
LL | / const U8_MUT: &u8 = {
2725
LL | |
28-
LL | |
29-
LL | |
3026
LL | | unsafe { &static_cross_crate::ZERO[0] }
3127
LL | | };
3228
| |__^ type validation failed: encountered a reference pointing to a static variable
@@ -37,143 +33,141 @@ LL | | };
3733
}
3834

3935
error: could not evaluate constant pattern
40-
--> $DIR/const_refers_to_static_cross_crate.rs:56:9
36+
--> $DIR/const_refers_to_static_cross_crate.rs:49:9
4137
|
4238
LL | U8_MUT => true,
4339
| ^^^^^^
4440

4541
warning: any use of this value will cause an error
46-
--> $DIR/const_refers_to_static_cross_crate.rs:29:15
42+
--> $DIR/const_refers_to_static_cross_crate.rs:25:15
4743
|
4844
LL | / const U8_MUT2: &u8 = {
4945
LL | | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
5046
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static
5147
LL | |
5248
LL | |
5349
LL | |
54-
LL | |
5550
LL | | };
5651
| |__-
5752
|
5853
note: the lint level is defined here
59-
--> $DIR/const_refers_to_static_cross_crate.rs:27:8
54+
--> $DIR/const_refers_to_static_cross_crate.rs:23:8
6055
|
6156
LL | #[warn(const_err)]
6257
| ^^^^^^^^^
6358
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6459
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
6560

6661
error: could not evaluate constant pattern
67-
--> $DIR/const_refers_to_static_cross_crate.rs:67:9
62+
--> $DIR/const_refers_to_static_cross_crate.rs:60:9
6863
|
6964
LL | U8_MUT2 => true,
7065
| ^^^^^^^
7166

7267
warning: any use of this value will cause an error
73-
--> $DIR/const_refers_to_static_cross_crate.rs:37:51
68+
--> $DIR/const_refers_to_static_cross_crate.rs:32:51
7469
|
7570
LL | / const U8_MUT3: &u8 = {
7671
LL | | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
7772
| | ^^^^^^^^^^^ constant accesses static
7873
LL | |
7974
LL | |
80-
... |
8175
LL | |
8276
LL | | };
8377
| |__-
8478
|
8579
note: the lint level is defined here
86-
--> $DIR/const_refers_to_static_cross_crate.rs:35:8
80+
--> $DIR/const_refers_to_static_cross_crate.rs:30:8
8781
|
8882
LL | #[warn(const_err)]
8983
| ^^^^^^^^^
9084
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9185
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
9286

9387
error: could not evaluate constant pattern
94-
--> $DIR/const_refers_to_static_cross_crate.rs:75:9
88+
--> $DIR/const_refers_to_static_cross_crate.rs:68:9
9589
|
9690
LL | U8_MUT3 => true,
9791
| ^^^^^^^
9892

9993
error: could not evaluate constant pattern
100-
--> $DIR/const_refers_to_static_cross_crate.rs:47:9
94+
--> $DIR/const_refers_to_static_cross_crate.rs:40:9
10195
|
10296
LL | SLICE_MUT => true,
10397
| ^^^^^^^^^
10498

10599
error: could not evaluate constant pattern
106-
--> $DIR/const_refers_to_static_cross_crate.rs:56:9
100+
--> $DIR/const_refers_to_static_cross_crate.rs:49:9
107101
|
108102
LL | U8_MUT => true,
109103
| ^^^^^^
110104

111105
error: could not evaluate constant pattern
112-
--> $DIR/const_refers_to_static_cross_crate.rs:67:9
106+
--> $DIR/const_refers_to_static_cross_crate.rs:60:9
113107
|
114108
LL | U8_MUT2 => true,
115109
| ^^^^^^^
116110

117111
error: could not evaluate constant pattern
118-
--> $DIR/const_refers_to_static_cross_crate.rs:75:9
112+
--> $DIR/const_refers_to_static_cross_crate.rs:68:9
119113
|
120114
LL | U8_MUT3 => true,
121115
| ^^^^^^^
122116

123117
warning: skipping const checks
124118
|
125119
help: skipping check that does not even have a feature gate
126-
--> $DIR/const_refers_to_static_cross_crate.rs:16:15
120+
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
127121
|
128122
LL | unsafe { &static_cross_crate::ZERO }
129123
| ^^^^^^^^^^^^^^^^^^^^^^^^
130124
help: skipping check that does not even have a feature gate
131-
--> $DIR/const_refers_to_static_cross_crate.rs:16:15
125+
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
132126
|
133127
LL | unsafe { &static_cross_crate::ZERO }
134128
| ^^^^^^^^^^^^^^^^^^^^^^^^
135129
help: skipping check that does not even have a feature gate
136-
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
130+
--> $DIR/const_refers_to_static_cross_crate.rs:19:15
137131
|
138132
LL | unsafe { &static_cross_crate::ZERO[0] }
139133
| ^^^^^^^^^^^^^^^^^^^^^^^^
140134
help: skipping check that does not even have a feature gate
141-
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
135+
--> $DIR/const_refers_to_static_cross_crate.rs:19:15
142136
|
143137
LL | unsafe { &static_cross_crate::ZERO[0] }
144138
| ^^^^^^^^^^^^^^^^^^^^^^^^
145139
help: skipping check that does not even have a feature gate
146-
--> $DIR/const_refers_to_static_cross_crate.rs:23:15
140+
--> $DIR/const_refers_to_static_cross_crate.rs:19:15
147141
|
148142
LL | unsafe { &static_cross_crate::ZERO[0] }
149143
| ^^^^^^^^^^^^^^^^^^^^^^^^
150144
help: skipping check that does not even have a feature gate
151-
--> $DIR/const_refers_to_static_cross_crate.rs:29:17
145+
--> $DIR/const_refers_to_static_cross_crate.rs:25:17
152146
|
153147
LL | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
154148
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155149
help: skipping check that does not even have a feature gate
156-
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
150+
--> $DIR/const_refers_to_static_cross_crate.rs:32:20
157151
|
158152
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
159153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160154
help: skipping check that does not even have a feature gate
161-
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
155+
--> $DIR/const_refers_to_static_cross_crate.rs:32:20
162156
|
163157
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
164158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
165159
help: skipping check that does not even have a feature gate
166-
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
160+
--> $DIR/const_refers_to_static_cross_crate.rs:32:20
167161
|
168162
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
169163
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
170164
help: skipping check for `const_panic` feature
171-
--> $DIR/const_refers_to_static_cross_crate.rs:37:77
165+
--> $DIR/const_refers_to_static_cross_crate.rs:32:77
172166
|
173167
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
174168
| ^^^^^^^^
175169
help: skipping check that does not even have a feature gate
176-
--> $DIR/const_refers_to_static_cross_crate.rs:37:20
170+
--> $DIR/const_refers_to_static_cross_crate.rs:32:20
177171
|
178172
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
179173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)