Skip to content

Commit

Permalink
Allow checking whether a type allows being uninitialized
Browse files Browse the repository at this point in the history
This is useful for clippy and for the future `MaybeUninit::assume_init`
panics.
  • Loading branch information
Noratrieb committed Mar 2, 2023
1 parent 7e966bc commit 10a69de
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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")
}
}
};

Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
}
}
}
Expand Down

0 comments on commit 10a69de

Please sign in to comment.