Skip to content

Commit bf45371

Browse files
committed
Auto merge of rust-lang#98520 - RalfJung:invalid, r=compiler-errors
interpret: adjust error from constructing an invalid value
2 parents ddcbba0 + 549940c commit bf45371

File tree

57 files changed

+196
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+196
-194
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
373373
),
374374
WriteToReadOnly(a) => write!(f, "writing to {} which is read-only", a),
375375
DerefFunctionPointer(a) => write!(f, "accessing {} which contains a function", a),
376-
ValidationFailure { path: None, msg } => write!(f, "type validation failed: {}", msg),
376+
ValidationFailure { path: None, msg } => {
377+
write!(f, "constructing invalid value: {}", msg)
378+
}
377379
ValidationFailure { path: Some(path), msg } => {
378-
write!(f, "type validation failed at {}: {}", path, msg)
380+
write!(f, "constructing invalid value at {}: {}", path, msg)
379381
}
380382
InvalidBool(b) => {
381383
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{:02x}", b)

src/test/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ error[E0080]: it is undefined behavior to use this value
2626
--> $DIR/invalid-patterns.rs:38:21
2727
|
2828
LL | get_flag::<false, { unsafe { char_raw.character } }>();
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
3030
|
3131
= 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.
3232
= note: the raw bytes of the constant (size: 4, align: 4) {
@@ -37,7 +37,7 @@ error[E0080]: it is undefined behavior to use this value
3737
--> $DIR/invalid-patterns.rs:40:14
3838
|
3939
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x42, but expected a boolean
4141
|
4242
= 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.
4343
= note: the raw bytes of the constant (size: 1, align: 1) {
@@ -48,7 +48,7 @@ error[E0080]: it is undefined behavior to use this value
4848
--> $DIR/invalid-patterns.rs:42:14
4949
|
5050
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x42, but expected a boolean
5252
|
5353
= 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.
5454
= note: the raw bytes of the constant (size: 1, align: 1) {
@@ -59,7 +59,7 @@ error[E0080]: it is undefined behavior to use this value
5959
--> $DIR/invalid-patterns.rs:42:47
6060
|
6161
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
6363
|
6464
= 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.
6565
= note: the raw bytes of the constant (size: 4, align: 4) {

src/test/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ error[E0080]: it is undefined behavior to use this value
2626
--> $DIR/invalid-patterns.rs:38:21
2727
|
2828
LL | get_flag::<false, { unsafe { char_raw.character } }>();
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
3030
|
3131
= 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.
3232
= note: the raw bytes of the constant (size: 4, align: 4) {
@@ -37,7 +37,7 @@ error[E0080]: it is undefined behavior to use this value
3737
--> $DIR/invalid-patterns.rs:40:14
3838
|
3939
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x42, but expected a boolean
4141
|
4242
= 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.
4343
= note: the raw bytes of the constant (size: 1, align: 1) {
@@ -48,7 +48,7 @@ error[E0080]: it is undefined behavior to use this value
4848
--> $DIR/invalid-patterns.rs:42:14
4949
|
5050
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x42, but expected a boolean
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x42, but expected a boolean
5252
|
5353
= 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.
5454
= note: the raw bytes of the constant (size: 1, align: 1) {
@@ -59,7 +59,7 @@ error[E0080]: it is undefined behavior to use this value
5959
--> $DIR/invalid-patterns.rs:42:47
6060
|
6161
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected a valid unicode scalar value (in `0..=0x10FFFF` but not in `0xD800..=0xDFFF`)
6363
|
6464
= 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.
6565
= note: the raw bytes of the constant (size: 4, align: 4) {

src/test/ui/const-ptr/forbidden_slices.32bit.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ error[E0080]: it is undefined behavior to use this value
4444
--> $DIR/forbidden_slices.rs:26:1
4545
|
4646
LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) };
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>[0]: encountered uninitialized bytes
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
4848
|
4949
= 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.
5050
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -55,7 +55,7 @@ error[E0080]: it is undefined behavior to use this value
5555
--> $DIR/forbidden_slices.rs:28:1
5656
|
5757
LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) };
58-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
5959
|
6060
= 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.
6161
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -66,7 +66,7 @@ error[E0080]: it is undefined behavior to use this value
6666
--> $DIR/forbidden_slices.rs:30:1
6767
|
6868
LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) };
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>[0]: encountered 0x11, but expected a boolean
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
7070
|
7171
= 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.
7272
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -82,7 +82,7 @@ LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
8282
LL | |
8383
LL | | from_raw_parts(ptr, 4)
8484
LL | | };
85-
| |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
85+
| |__^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
8686
|
8787
= 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.
8888
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -168,7 +168,7 @@ LL | |
168168
LL | | let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8;
169169
LL | | from_ptr_range(ptr..ptr.add(1))
170170
LL | | };
171-
| |__^ type validation failed at .<deref>[0]: encountered uninitialized bytes
171+
| |__^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
172172
|
173173
= 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.
174174
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -183,7 +183,7 @@ LL | |
183183
LL | | let ptr = &D3 as *const &u32;
184184
LL | | from_ptr_range(ptr.cast()..ptr.add(1).cast())
185185
LL | | };
186-
| |__^ type validation failed at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
186+
| |__^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
187187
|
188188
= 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.
189189
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -198,7 +198,7 @@ LL | |
198198
LL | | let ptr = &D0 as *const u32 as *const bool;
199199
LL | | from_ptr_range(ptr..ptr.add(4))
200200
LL | | };
201-
| |__^ type validation failed at .<deref>[0]: encountered 0x11, but expected a boolean
201+
| |__^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
202202
|
203203
= 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.
204204
= note: the raw bytes of the constant (size: 8, align: 4) {
@@ -213,7 +213,7 @@ LL | |
213213
LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
214214
LL | | from_ptr_range(ptr..ptr.add(4))
215215
LL | | };
216-
| |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
216+
| |__^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
217217
|
218218
= 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.
219219
= note: the raw bytes of the constant (size: 8, align: 4) {

src/test/ui/const-ptr/forbidden_slices.64bit.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ error[E0080]: it is undefined behavior to use this value
4444
--> $DIR/forbidden_slices.rs:26:1
4545
|
4646
LL | pub static S4: &[u8] = unsafe { from_raw_parts((&D1) as *const _ as _, 1) };
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>[0]: encountered uninitialized bytes
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
4848
|
4949
= 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.
5050
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -55,7 +55,7 @@ error[E0080]: it is undefined behavior to use this value
5555
--> $DIR/forbidden_slices.rs:28:1
5656
|
5757
LL | pub static S5: &[u8] = unsafe { from_raw_parts((&D3) as *const _ as _, size_of::<&u32>()) };
58-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
5959
|
6060
= 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.
6161
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -66,7 +66,7 @@ error[E0080]: it is undefined behavior to use this value
6666
--> $DIR/forbidden_slices.rs:30:1
6767
|
6868
LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) };
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed at .<deref>[0]: encountered 0x11, but expected a boolean
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
7070
|
7171
= 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.
7272
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -82,7 +82,7 @@ LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
8282
LL | |
8383
LL | | from_raw_parts(ptr, 4)
8484
LL | | };
85-
| |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
85+
| |__^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
8686
|
8787
= 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.
8888
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -168,7 +168,7 @@ LL | |
168168
LL | | let ptr = (&D1) as *const MaybeUninit<&u32> as *const u8;
169169
LL | | from_ptr_range(ptr..ptr.add(1))
170170
LL | | };
171-
| |__^ type validation failed at .<deref>[0]: encountered uninitialized bytes
171+
| |__^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
172172
|
173173
= 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.
174174
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -183,7 +183,7 @@ LL | |
183183
LL | | let ptr = &D3 as *const &u32;
184184
LL | | from_ptr_range(ptr.cast()..ptr.add(1).cast())
185185
LL | | };
186-
| |__^ type validation failed at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
186+
| |__^ constructing invalid value at .<deref>: encountered a pointer, but expected plain (non-pointer) bytes
187187
|
188188
= 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.
189189
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -198,7 +198,7 @@ LL | |
198198
LL | | let ptr = &D0 as *const u32 as *const bool;
199199
LL | | from_ptr_range(ptr..ptr.add(4))
200200
LL | | };
201-
| |__^ type validation failed at .<deref>[0]: encountered 0x11, but expected a boolean
201+
| |__^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
202202
|
203203
= 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.
204204
= note: the raw bytes of the constant (size: 16, align: 8) {
@@ -213,7 +213,7 @@ LL | |
213213
LL | | let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
214214
LL | | from_ptr_range(ptr..ptr.add(4))
215215
LL | | };
216-
| |__^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
216+
| |__^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
217217
|
218218
= 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.
219219
= note: the raw bytes of the constant (size: 16, align: 8) {

src/test/ui/consts/const-err4.32bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/const-err4.rs:9:11
33
|
44
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected initialized bytes
66
|
77
= 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.
88
= note: the raw bytes of the constant (size: 4, align: 4) {

src/test/ui/consts/const-err4.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/const-err4.rs:9:11
33
|
44
LL | Boo = [unsafe { Foo { b: () }.a }; 4][3],
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected initialized bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered uninitialized bytes, but expected initialized bytes
66
|
77
= 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.
88
= note: the raw bytes of the constant (size: 8, align: 8) {

0 commit comments

Comments
 (0)