Skip to content

Commit b63b2f1

Browse files
committed
PR feedback
- Add `:Sized` assertion in interpreter impl - Use `Scalar::from_bool` instead of `ScalarInt: From<bool>` - Remove unneeded comparison in intrinsic typeck - Make this UB to call with undef, not just return undef in that case
1 parent 2456495 commit b63b2f1

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

compiler/rustc_mir/src/interpret/intrinsics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
570570
rhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::PointerTag>,
571571
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
572572
let layout = self.layout_of(lhs.layout.ty.builtin_deref(true).unwrap().ty)?;
573+
assert!(!layout.is_unsized());
573574

574575
let lhs = self.read_scalar(lhs)?.check_init()?;
575576
let rhs = self.read_scalar(rhs)?.check_init()?;
576577
let lhs_bytes = self.memory.read_bytes(lhs, layout.size)?;
577578
let rhs_bytes = self.memory.read_bytes(rhs, layout.size)?;
578-
Ok(Scalar::Int((lhs_bytes == rhs_bytes).into()))
579+
Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
579580
}
580581
}

compiler/rustc_typeck/src/check/intrinsic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,10 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
381381
sym::nontemporal_store => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
382382

383383
sym::raw_eq => {
384-
let param_count = if intrinsic_name == sym::raw_eq { 2 } else { 1 };
385384
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0) };
386385
let param_ty =
387386
tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)), param(0));
388-
(1, vec![param_ty; param_count], tcx.types.bool)
387+
(1, vec![param_ty; 2], tcx.types.bool)
389388
}
390389

391390
other => {

library/core/src/intrinsics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,8 +1924,12 @@ extern "rust-intrinsic" {
19241924
///
19251925
/// # Safety
19261926
///
1927-
/// This doesn't take into account padding, so if `T` has padding
1928-
/// the result will be `undef`, which cannot be exposed to safe code.
1927+
/// It's UB to call this if any of the *bytes* in `*a` or `*b` are uninitialized.
1928+
/// Note that this is a stricter criterion than just the *values* being
1929+
/// fully-initialized: if `T` has padding, it's UB to call this intrinsic.
1930+
///
1931+
/// (The implementation is allowed to branch on the results of comparisons,
1932+
/// which is UB if any of their inputs are `undef`.)
19291933
#[cfg(not(bootstrap))]
19301934
#[rustc_const_unstable(feature = "const_intrinsic_raw_eq", issue = "none")]
19311935
pub fn raw_eq<T>(a: &T, b: &T) -> bool;

0 commit comments

Comments
 (0)