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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 14 additions & 0 deletions
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`.
Lines changed: 14 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 8 deletions
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

Lines changed: 4 additions & 8 deletions
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

Lines changed: 2 additions & 6 deletions
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
};

0 commit comments

Comments
 (0)