Skip to content

Commit 24be433

Browse files
cjgillotRalfJung
andauthored
Apply suggestions from code review
Co-authored-by: Ralf Jung <[email protected]>
1 parent 8561618 commit 24be433

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

compiler/rustc_middle/src/mir/consts.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ impl<'tcx> ConstValue<'tcx> {
174174
}
175175

176176
/// Check if a constant may contain provenance information. This is used by MIR opts.
177+
/// Can return `true` even if there is no provenance.
177178
pub fn may_have_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool {
178179
match *self {
179180
ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
180181
ConstValue::Scalar(Scalar::Ptr(..)) => return true,
182+
// It's hard to find out the part of the allocation we point to;
183+
// just conservatively check everything.
181184
ConstValue::Slice { data, meta: _ } => !data.inner().provenance().ptrs().is_empty(),
182185
ConstValue::Indirect { alloc_id, offset } => !tcx
183186
.global_alloc(alloc_id)
@@ -504,10 +507,10 @@ impl<'tcx> Const<'tcx> {
504507
/// Return true if any evaluation of this constant always returns the same value,
505508
/// taking into account even pointer identity tests.
506509
pub fn is_deterministic(&self) -> bool {
507-
// Some constants may contain pointers. We need to preserve the provenance of these
508-
// pointers, but not all constants guarantee this:
509-
// - valtrees purposefully do not;
510-
// - ConstValue::Slice does not either.
510+
// Some constants may generate fresh allocations for pointers they contain,
511+
// so using the same constant twice can yield two different results:
512+
// - valtrees purposefully generate new allocations
513+
// - ConstValue::Slice also generate new allocations
511514
match self {
512515
Const::Ty(c) => match c.kind() {
513516
ty::ConstKind::Param(..) => true,

compiler/rustc_mir_transform/src/gvn.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ fn op_to_prop_const<'tcx>(
917917

918918
// Do not try interning a value that contains provenance.
919919
// Due to https://github.com/rust-lang/rust/issues/79738, doing so could lead to bugs.
920+
// FIXME: remove this hack once that issue is fixed.
920921
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??;
921922
if alloc_ref.has_provenance() {
922923
return None;
@@ -928,6 +929,8 @@ fn op_to_prop_const<'tcx>(
928929
if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) {
929930
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
930931
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
932+
// FIXME: find a way to treat this more uniformly
933+
// (probably by fixing codegen)
931934
return Some(ConstValue::Indirect { alloc_id, offset });
932935
}
933936
}
@@ -939,7 +942,7 @@ fn op_to_prop_const<'tcx>(
939942

940943
// Check that we do not leak a pointer.
941944
// Those pointers may lose part of their identity in codegen.
942-
// See https://github.com/rust-lang/rust/issues/79738.
945+
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
943946
if ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty() {
944947
return Some(value);
945948
}
@@ -969,7 +972,7 @@ impl<'tcx> VnState<'_, 'tcx> {
969972

970973
// Check that we do not leak a pointer.
971974
// Those pointers may lose part of their identity in codegen.
972-
// See https://github.com/rust-lang/rust/issues/79738.
975+
// FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed.
973976
assert!(!value.may_have_provenance(self.tcx, op.layout.size));
974977

975978
let const_ = Const::Val(value, op.layout.ty);

0 commit comments

Comments
 (0)