Skip to content

Commit 31bc929

Browse files
committed
Rename has_provance and tweaks comments.
1 parent c80595c commit 31bc929

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

compiler/rustc_const_eval/src/interpret/intern.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,12 @@ pub fn intern_const_alloc_for_constprop<
474474

475475
alloc.mutability = Mutability::Not;
476476

477-
// link the alloc id to the actual allocation
477+
// We are not doing recursive interning, so we don't currently support provenance.
478+
// (If this assertion ever triggers, we should just implement a
479+
// proper recursive interning loop.)
478480
assert!(alloc.provenance().ptrs().is_empty());
479481

482+
// Link the alloc id to the actual allocation
480483
let alloc = ecx.tcx.mk_const_alloc(alloc);
481484
ecx.tcx.set_alloc_id_memory(alloc_id, alloc);
482485

compiler/rustc_middle/src/mir/consts.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,19 @@ impl<'tcx> ConstValue<'tcx> {
173173
Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end))
174174
}
175175

176-
pub fn has_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool {
177-
let (alloc, start, end) = match *self {
176+
/// Check if a constant may contain provenance information. This is used by MIR opts.
177+
pub fn may_have_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool {
178+
match *self {
178179
ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false,
179180
ConstValue::Scalar(Scalar::Ptr(..)) => return true,
180-
ConstValue::Slice { data, meta } => (data, Size::ZERO, Size::from_bytes(meta)),
181-
ConstValue::Indirect { alloc_id, offset } => {
182-
(tcx.global_alloc(alloc_id).unwrap_memory(), offset, offset + size)
183-
}
184-
};
185-
!alloc.inner().provenance().range_empty(super::AllocRange::from(start..end), &tcx)
181+
ConstValue::Slice { data, meta: _ } => !data.inner().provenance().ptrs().is_empty(),
182+
ConstValue::Indirect { alloc_id, offset } => !tcx
183+
.global_alloc(alloc_id)
184+
.unwrap_memory()
185+
.inner()
186+
.provenance()
187+
.range_empty(super::AllocRange::from(offset..offset + size), &tcx),
188+
}
186189
}
187190
}
188191

compiler/rustc_mir_transform/src/gvn.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -910,11 +910,13 @@ fn op_to_prop_const<'tcx>(
910910
return Some(ConstValue::Scalar(scalar));
911911
}
912912

913-
// If this constant is a projection of another, we can return it directly.
913+
// If this constant is already represented as an `Allocation`,
914+
// try putting it into global memory to return it.
914915
if let Either::Left(mplace) = op.as_mplace_or_imm() {
915916
let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??;
916917

917918
// Do not try interning a value that contains provenance.
919+
// Due to https://github.com/rust-lang/rust/issues/79738, doing so could lead to bugs.
918920
let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??;
919921
if alloc_ref.has_provenance() {
920922
return None;
@@ -935,7 +937,10 @@ fn op_to_prop_const<'tcx>(
935937
ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?;
936938
let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO };
937939

938-
if !value.has_provenance(*ecx.tcx, op.layout.size) {
940+
// Check that we do not leak a pointer.
941+
// Those pointers may lose part of their identity in codegen.
942+
// See https://github.com/rust-lang/rust/issues/79738.
943+
if !value.may_have_provenance(*ecx.tcx, op.layout.size) {
939944
return Some(value);
940945
}
941946

@@ -964,7 +969,8 @@ impl<'tcx> VnState<'_, 'tcx> {
964969

965970
// Check that we do not leak a pointer.
966971
// Those pointers may lose part of their identity in codegen.
967-
assert!(!value.has_provenance(self.tcx, op.layout.size));
972+
// See https://github.com/rust-lang/rust/issues/79738.
973+
assert!(!value.may_have_provenance(self.tcx, op.layout.size));
968974

969975
let const_ = Const::Val(value, op.layout.ty);
970976
Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ })

0 commit comments

Comments
 (0)