Skip to content

Commit a00b977

Browse files
authored
Unrolled build for #143140
Rollup merge of #143140 - RalfJung:ptr-into-parts, r=oli-obk give Pointer::into_parts a more scary name and offer a safer alternative `into_parts` is a bit too innocent of a name for a somewhat subtle operation. r? `@oli-obk`
2 parents f26e580 + 75c6e14 commit a00b977

File tree

20 files changed

+55
-46
lines changed

20 files changed

+55
-46
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub(crate) fn codegen_const_value<'tcx>(
133133
}
134134
}
135135
Scalar::Ptr(ptr, _size) => {
136-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
136+
let (prov, offset) = ptr.prov_and_relative_offset();
137137
let alloc_id = prov.alloc_id();
138138
let base_addr = match fx.tcx.global_alloc(alloc_id) {
139139
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_gcc/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
240240
}
241241
}
242242
Scalar::Ptr(ptr, _size) => {
243-
let (prov, offset) = ptr.into_parts(); // we know the `offset` is relative
243+
let (prov, offset) = ptr.prov_and_relative_offset();
244244
let alloc_id = prov.alloc_id();
245245
let base_addr = match self.tcx.global_alloc(alloc_id) {
246246
GlobalAlloc::Memory(alloc) => {

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
268268
}
269269
}
270270
Scalar::Ptr(ptr, _size) => {
271-
let (prov, offset) = ptr.into_parts();
271+
let (prov, offset) = ptr.prov_and_relative_offset();
272272
let global_alloc = self.tcx.global_alloc(prov.alloc_id());
273273
let base_addr = match global_alloc {
274274
GlobalAlloc::Memory(alloc) => {

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ pub(super) fn op_to_const<'tcx>(
209209

210210
match immediate {
211211
Left(ref mplace) => {
212-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
213-
let (prov, offset) = mplace.ptr().into_parts();
214-
let alloc_id = prov.expect("cannot have `fake` place for non-ZST type").alloc_id();
212+
let (prov, offset) =
213+
mplace.ptr().into_pointer_or_addr().unwrap().prov_and_relative_offset();
214+
let alloc_id = prov.alloc_id();
215215
ConstValue::Indirect { alloc_id, offset }
216216
}
217217
// see comment on `let force_as_immediate` above
@@ -232,9 +232,10 @@ pub(super) fn op_to_const<'tcx>(
232232
imm.layout.ty,
233233
);
234234
let msg = "`op_to_const` on an immediate scalar pair must only be used on slice references to the beginning of an actual allocation";
235-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
236-
let (prov, offset) = a.to_pointer(ecx).expect(msg).into_parts();
237-
let alloc_id = prov.expect(msg).alloc_id();
235+
let ptr = a.to_pointer(ecx).expect(msg);
236+
let (prov, offset) =
237+
ptr.into_pointer_or_addr().expect(msg).prov_and_relative_offset();
238+
let alloc_id = prov.alloc_id();
238239
let data = ecx.tcx.global_alloc(alloc_id).unwrap_memory();
239240
assert!(offset == abi::Size::ZERO, "{}", msg);
240241
let meta = b.to_target_usize(ecx).expect(msg);

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
574574
if addr != 0 {
575575
diag.arg(
576576
"pointer",
577-
Pointer::<Option<CtfeProvenance>>::from_addr_invalid(addr).to_string(),
577+
Pointer::<Option<CtfeProvenance>>::without_provenance(addr).to_string(),
578578
);
579579
}
580580

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
747747
// Allow these casts, but make the pointer not dereferenceable.
748748
// (I.e., they behave like transmutation.)
749749
// This is correct because no pointers can ever be exposed in compile-time evaluation.
750-
interp_ok(Pointer::from_addr_invalid(addr))
750+
interp_ok(Pointer::without_provenance(addr))
751751
}
752752

753753
#[inline(always)]
@@ -756,8 +756,7 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
756756
ptr: Pointer<CtfeProvenance>,
757757
_size: i64,
758758
) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
759-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
760-
let (prov, offset) = ptr.into_parts();
759+
let (prov, offset) = ptr.prov_and_relative_offset();
761760
Some((prov.alloc_id(), offset, prov.immutable()))
762761
}
763762

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
15961596
Some((alloc_id, offset, extra)) => Ok((alloc_id, offset, extra)),
15971597
None => {
15981598
assert!(M::Provenance::OFFSET_IS_ADDR);
1599-
let (_, addr) = ptr.into_parts();
1599+
// Offset is absolute, as we just asserted.
1600+
let (_, addr) = ptr.into_raw_parts();
16001601
Err(addr.bytes())
16011602
}
16021603
},

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
118118
pub fn fake_alloc_zst(layout: TyAndLayout<'tcx>) -> Self {
119119
assert!(layout.is_zst());
120120
let align = layout.align.abi;
121-
let ptr = Pointer::from_addr_invalid(align.bytes()); // no provenance, absolute address
121+
let ptr = Pointer::without_provenance(align.bytes()); // no provenance, absolute address
122122
MPlaceTy { mplace: MemPlace { ptr, meta: MemPlaceMeta::None, misaligned: None }, layout }
123123
}
124124

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
518518
Ub(DanglingIntPointer { addr: i, .. }) => DanglingPtrNoProvenance {
519519
ptr_kind,
520520
// FIXME this says "null pointer" when null but we need translate
521-
pointer: format!("{}", Pointer::<Option<AllocId>>::from_addr_invalid(i))
521+
pointer: format!("{}", Pointer::<Option<AllocId>>::without_provenance(i))
522522
},
523523
Ub(PointerOutOfBounds { .. }) => DanglingPtrOutOfBounds {
524524
ptr_kind
@@ -868,7 +868,9 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
868868
fn add_data_range(&mut self, ptr: Pointer<Option<M::Provenance>>, size: Size) {
869869
if let Some(data_bytes) = self.data_bytes.as_mut() {
870870
// We only have to store the offset, the rest is the same for all pointers here.
871-
let (_prov, offset) = ptr.into_parts();
871+
// The logic is agnostic to wether the offset is relative or absolute as long as
872+
// it is consistent.
873+
let (_prov, offset) = ptr.into_raw_parts();
872874
// Add this.
873875
data_bytes.add_range(offset, size);
874876
};
@@ -894,7 +896,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
894896
.as_mplace_or_imm()
895897
.expect_left("place must be in memory")
896898
.ptr();
897-
let (_prov, offset) = ptr.into_parts();
899+
let (_prov, offset) = ptr.into_raw_parts();
898900
offset
899901
}
900902

@@ -903,7 +905,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
903905
// Our value must be in memory, otherwise we would not have set up `data_bytes`.
904906
let mplace = self.ecx.force_allocation(place)?;
905907
// Determine starting offset and size.
906-
let (_prov, start_offset) = mplace.ptr().into_parts();
908+
let (_prov, start_offset) = mplace.ptr().into_raw_parts();
907909
let (size, _align) = self
908910
.ecx
909911
.size_and_align_of_val(&mplace)?

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,9 @@ impl<'tcx> ConstValue<'tcx> {
168168
return Some(&[]);
169169
}
170170
// Non-empty slice, must have memory. We know this is a relative pointer.
171-
let (inner_prov, offset) = ptr.into_parts();
172-
let data = tcx.global_alloc(inner_prov?.alloc_id()).unwrap_memory();
171+
let (inner_prov, offset) =
172+
ptr.into_pointer_or_addr().ok()?.prov_and_relative_offset();
173+
let data = tcx.global_alloc(inner_prov.alloc_id()).unwrap_memory();
173174
(data, offset.bytes(), offset.bytes() + len)
174175
}
175176
};

0 commit comments

Comments
 (0)