Rollup merge of #108669 - Nilstrieb:query-my-uninitness, r=compiler-errors

Allow checking whether a type allows being uninitialized

This is useful for clippy ([rust-lang/clippy#10407](https://github.com/rust-lang/rust-clippy/issues/10407)) and for the future `MaybeUninit::assume_init` panics (#100423).
This commit is contained in:
Dylan DPC 2023-03-04 15:24:39 +05:30 committed by GitHub
commit 6ce78a31d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View File

@ -444,6 +444,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
"aborted execution: attempted to leave type `{}` uninitialized, which is invalid",
ty
),
ValidityRequirement::Uninit => bug!("assert_uninit_valid doesn't exist"),
};
M::abort(self, msg)?;

View File

@ -30,7 +30,7 @@ pub fn check_validity_requirement<'tcx>(
return Ok(!layout.abi.is_uninhabited());
}
if tcx.sess.opts.unstable_opts.strict_init_checks {
if kind == ValidityRequirement::Uninit || tcx.sess.opts.unstable_opts.strict_init_checks {
might_permit_raw_init_strict(layout, tcx, kind)
} else {
let layout_cx = LayoutCx { tcx, param_env: param_env_and_ty.param_env };
@ -99,6 +99,9 @@ fn might_permit_raw_init_lax<'tcx>(
}
s.valid_range(cx).contains(val)
}
ValidityRequirement::Uninit => {
bug!("ValidityRequirement::Uninit should have been handled above")
}
}
};

View File

@ -170,13 +170,17 @@ pub const FAT_PTR_EXTRA: usize = 1;
/// * Cranelift stores the base-2 log of the lane count in a 4 bit integer.
pub const MAX_SIMD_LANES: u64 = 1 << 0xF;
/// Used in `might_permit_raw_init` to indicate the kind of initialisation
/// Used in `check_validity_requirement` to indicate the kind of initialization
/// that is checked to be valid
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
pub enum ValidityRequirement {
Inhabited,
Zero,
/// The return value of mem::uninitialized, 0x01
/// (unless -Zstrict-init-checks is on, in which case it's the same as Uninit).
UninitMitigated0x01Fill,
/// True uninitialized memory.
Uninit,
}
impl ValidityRequirement {
@ -196,6 +200,7 @@ impl fmt::Display for ValidityRequirement {
Self::Inhabited => f.write_str("is inhabited"),
Self::Zero => f.write_str("allows being left zeroed"),
Self::UninitMitigated0x01Fill => f.write_str("allows being filled with 0x01"),
Self::Uninit => f.write_str("allows being left uninitialized"),
}
}
}