const validity checking: do not recurse to references inside MaybeDangling

This commit is contained in:
Ralf Jung
2026-03-19 12:19:59 +01:00
parent 8b86f48958
commit b3e4ebd9d2
2 changed files with 9 additions and 2 deletions
@@ -680,8 +680,11 @@ fn check_safe_pointer(
format!("encountered a {ptr_kind} pointing to uninhabited type {ty}")
)
}
// Recursive checking
if let Some(ref_tracking) = self.ref_tracking.as_deref_mut() {
// Recursive checking (but not inside `MaybeDangling` of course).
if let Some(ref_tracking) = self.ref_tracking.as_deref_mut()
&& !self.may_dangle
{
// Proceed recursively even for ZST, no reason to skip them!
// `!` is a ZST and we want to validate it.
if let Some(ctfe_mode) = self.ctfe_mode {
@@ -1,10 +1,12 @@
//@ check-pass
//
// Some constants that *are* valid
#![feature(maybe_dangling)]
use std::mem;
use std::ptr::NonNull;
use std::num::NonZero;
use std::mem::MaybeDangling;
const NON_NULL_PTR1: NonNull<u8> = unsafe { mem::transmute(1usize) };
const NON_NULL_PTR2: NonNull<u8> = unsafe { mem::transmute(&0) };
@@ -14,4 +16,6 @@
const UNIT: () = ();
const INVALID_INSIDE_MAYBE_DANGLING: MaybeDangling<&bool> = unsafe { std::mem::transmute(&5u8) };
fn main() {}