Skip to content

Commit 866dc5a

Browse files
Rollup merge of #82175 - RalfJung:invalid-fn-ptr, r=oli-obk
validation: fix invalid-fn-ptr error message #82061 changed the code here to print an `ImmTy` instead of a `ScalarMaybeUninit`; that was an accident. So go back to printing a `ScalarMaybeUninit`. r? ```@oli-obk```
2 parents 46b93b2 + e5514ef commit 866dc5a

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

compiler/rustc_mir/src/interpret/validity.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
586586
self.path,
587587
err_unsup!(ReadPointerAsBytes) => { "part of a pointer" } expected { "a proper pointer or integer value" },
588588
);
589+
// Make sure we print a `ScalarMaybeUninit` (and not an `ImmTy`) in the error
590+
// message below.
591+
let value = value.to_scalar_or_uninit();
589592
let _fn = try_validation!(
590-
value.to_scalar().and_then(|ptr| self.ecx.memory.get_fn(ptr)),
593+
value.check_init().and_then(|ptr| self.ecx.memory.get_fn(ptr)),
591594
self.path,
592595
err_ub!(DanglingIntPointer(..)) |
593596
err_ub!(InvalidFunctionPointer(..)) |

src/test/ui/consts/const-eval/ub-ref.rs renamed to src/test/ui/consts/const-eval/ub-ref-ptr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
use std::mem;
55

6+
#[repr(C)]
7+
union MaybeUninit<T: Copy> {
8+
uninit: (),
9+
init: T,
10+
}
11+
612
const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
713
//~^ ERROR it is undefined behavior to use this value
814
//~| type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
@@ -35,4 +41,9 @@ const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
3541
const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
3642
//~^ ERROR it is undefined behavior to use this value
3743

44+
const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
45+
//~^ ERROR it is undefined behavior to use this value
46+
const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
47+
//~^ ERROR it is undefined behavior to use this value
48+
3849
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,91 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/ub-ref.rs:6:1
2+
--> $DIR/ub-ref-ptr.rs:12:1
33
|
44
LL | const UNALIGNED: &u16 = unsafe { mem::transmute(&[0u8; 4]) };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 2 byte alignment but found 1)
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

99
error[E0080]: it is undefined behavior to use this value
10-
--> $DIR/ub-ref.rs:10:1
10+
--> $DIR/ub-ref-ptr.rs:16:1
1111
|
1212
LL | const UNALIGNED_BOX: Box<u16> = unsafe { mem::transmute(&[0u8; 4]) };
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered an unaligned box (required 2 byte alignment but found 1)
1414
|
1515
= 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.
1616

1717
error[E0080]: it is undefined behavior to use this value
18-
--> $DIR/ub-ref.rs:14:1
18+
--> $DIR/ub-ref-ptr.rs:20:1
1919
|
2020
LL | const NULL: &u16 = unsafe { mem::transmute(0usize) };
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL reference
2222
|
2323
= 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.
2424

2525
error[E0080]: it is undefined behavior to use this value
26-
--> $DIR/ub-ref.rs:17:1
26+
--> $DIR/ub-ref-ptr.rs:23:1
2727
|
2828
LL | const NULL_BOX: Box<u16> = unsafe { mem::transmute(0usize) };
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a NULL box
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

3333
error[E0080]: it is undefined behavior to use this value
34-
--> $DIR/ub-ref.rs:23:1
34+
--> $DIR/ub-ref-ptr.rs:29:1
3535
|
3636
LL | const REF_AS_USIZE: usize = unsafe { mem::transmute(&0) };
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered pointer to alloc14, but expected initialized plain (non-pointer) bytes
3838
|
3939
= 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.
4040

4141
error[E0080]: it is undefined behavior to use this value
42-
--> $DIR/ub-ref.rs:26:1
42+
--> $DIR/ub-ref-ptr.rs:32:1
4343
|
4444
LL | const REF_AS_USIZE_SLICE: &[usize] = &[unsafe { mem::transmute(&0) }];
4545
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
4646
|
4747
= 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.
4848

4949
error[E0080]: it is undefined behavior to use this value
50-
--> $DIR/ub-ref.rs:29:1
50+
--> $DIR/ub-ref-ptr.rs:35:1
5151
|
5252
LL | const REF_AS_USIZE_BOX_SLICE: Box<[usize]> = unsafe { mem::transmute::<&[usize], _>(&[mem::transmute(&0)]) };
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer at .<deref>, but expected plain (non-pointer) bytes
5454
|
5555
= 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.
5656

5757
error[E0080]: it is undefined behavior to use this value
58-
--> $DIR/ub-ref.rs:32:1
58+
--> $DIR/ub-ref-ptr.rs:38:1
5959
|
6060
LL | const USIZE_AS_REF: &'static u8 = unsafe { mem::transmute(1337usize) };
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling reference (created from integer)
6262
|
6363
= 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.
6464

6565
error[E0080]: it is undefined behavior to use this value
66-
--> $DIR/ub-ref.rs:35:1
66+
--> $DIR/ub-ref-ptr.rs:41:1
6767
|
6868
LL | const USIZE_AS_BOX: Box<u8> = unsafe { mem::transmute(1337usize) };
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a dangling box (created from integer)
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

73-
error: aborting due to 9 previous errors
73+
error[E0080]: it is undefined behavior to use this value
74+
--> $DIR/ub-ref-ptr.rs:44:1
75+
|
76+
LL | const UNINIT_PTR: *const i32 = unsafe { MaybeUninit { uninit: () }.init };
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
78+
|
79+
= 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.
80+
81+
error[E0080]: it is undefined behavior to use this value
82+
--> $DIR/ub-ref-ptr.rs:46:1
83+
|
84+
LL | const UNINIT_FN_PTR: fn() = unsafe { MaybeUninit { uninit: () }.init };
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a function pointer
86+
|
87+
= 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+
89+
error: aborting due to 11 previous errors
7490

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

0 commit comments

Comments
 (0)