Skip to content

Commit 6ce78a3

Browse files
authored
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](rust-lang/rust-clippy#10407)) and for the future `MaybeUninit::assume_init` panics (#100423).
2 parents e700d02 + 10a69de commit 6ce78a3

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
444444
"aborted execution: attempted to leave type `{}` uninitialized, which is invalid",
445445
ty
446446
),
447+
ValidityRequirement::Uninit => bug!("assert_uninit_valid doesn't exist"),
447448
};
448449

449450
M::abort(self, msg)?;

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn check_validity_requirement<'tcx>(
3030
return Ok(!layout.abi.is_uninhabited());
3131
}
3232

33-
if tcx.sess.opts.unstable_opts.strict_init_checks {
33+
if kind == ValidityRequirement::Uninit || tcx.sess.opts.unstable_opts.strict_init_checks {
3434
might_permit_raw_init_strict(layout, tcx, kind)
3535
} else {
3636
let layout_cx = LayoutCx { tcx, param_env: param_env_and_ty.param_env };
@@ -99,6 +99,9 @@ fn might_permit_raw_init_lax<'tcx>(
9999
}
100100
s.valid_range(cx).contains(val)
101101
}
102+
ValidityRequirement::Uninit => {
103+
bug!("ValidityRequirement::Uninit should have been handled above")
104+
}
102105
}
103106
};
104107

compiler/rustc_middle/src/ty/layout.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,17 @@ pub const FAT_PTR_EXTRA: usize = 1;
170170
/// * Cranelift stores the base-2 log of the lane count in a 4 bit integer.
171171
pub const MAX_SIMD_LANES: u64 = 1 << 0xF;
172172

173-
/// Used in `might_permit_raw_init` to indicate the kind of initialisation
173+
/// Used in `check_validity_requirement` to indicate the kind of initialization
174174
/// that is checked to be valid
175175
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
176176
pub enum ValidityRequirement {
177177
Inhabited,
178178
Zero,
179+
/// The return value of mem::uninitialized, 0x01
180+
/// (unless -Zstrict-init-checks is on, in which case it's the same as Uninit).
179181
UninitMitigated0x01Fill,
182+
/// True uninitialized memory.
183+
Uninit,
180184
}
181185

182186
impl ValidityRequirement {
@@ -196,6 +200,7 @@ impl fmt::Display for ValidityRequirement {
196200
Self::Inhabited => f.write_str("is inhabited"),
197201
Self::Zero => f.write_str("allows being left zeroed"),
198202
Self::UninitMitigated0x01Fill => f.write_str("allows being filled with 0x01"),
203+
Self::Uninit => f.write_str("allows being left uninitialized"),
199204
}
200205
}
201206
}

0 commit comments

Comments
 (0)