Skip to content

Commit d21fc9d

Browse files
authoredMar 6, 2025
Rollup merge of rust-lang#137920 - RalfJung:provenance-map-emptiness, r=oli-obk
interpret/provenance_map: consistently use range_is_empty rust-lang#137704 started using this for per-ptr provenance; let's be consistent and use it also for the per-byte provenance check. Also rename the methods to avoid having both "get" and "is_empty" in the name. r? ````@oli-obk````
2 parents 1458b35 + 0aacfe9 commit d21fc9d

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed
 

‎compiler/rustc_middle/src/mir/interpret/allocation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
470470
// Find the provenance.
471471
let (offset, _prov) = self
472472
.provenance
473-
.range_get_ptrs(range, cx)
473+
.range_ptrs_get(range, cx)
474474
.first()
475475
.copied()
476476
.expect("there must be provenance somewhere here");

‎compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl ProvenanceMap {
6767
}
6868

6969
impl<Prov: Provenance> ProvenanceMap<Prov> {
70-
fn adjusted_range(range: AllocRange, cx: &impl HasDataLayout) -> Range<Size> {
70+
fn adjusted_range_ptrs(range: AllocRange, cx: &impl HasDataLayout) -> Range<Size> {
7171
// We have to go back `pointer_size - 1` bytes, as that one would still overlap with
7272
// the beginning of this range.
7373
let adjusted_start = Size::from_bytes(
@@ -79,36 +79,36 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
7979
/// Returns all ptr-sized provenance in the given range.
8080
/// If the range has length 0, returns provenance that crosses the edge between `start-1` and
8181
/// `start`.
82-
pub(super) fn range_get_ptrs(
82+
pub(super) fn range_ptrs_get(
8383
&self,
8484
range: AllocRange,
8585
cx: &impl HasDataLayout,
8686
) -> &[(Size, Prov)] {
87-
self.ptrs.range(Self::adjusted_range(range, cx))
87+
self.ptrs.range(Self::adjusted_range_ptrs(range, cx))
8888
}
8989

90-
/// `pm.range_get_ptrs_is_empty(r, cx)` == `pm.range_get_ptrs(r, cx).is_empty()`, but is
91-
/// faster.
92-
pub(super) fn range_get_ptrs_is_empty(
93-
&self,
94-
range: AllocRange,
95-
cx: &impl HasDataLayout,
96-
) -> bool {
97-
self.ptrs.range_is_empty(Self::adjusted_range(range, cx))
90+
/// `pm.range_ptrs_is_empty(r, cx)` == `pm.range_ptrs_get(r, cx).is_empty()`, but is faster.
91+
pub(super) fn range_ptrs_is_empty(&self, range: AllocRange, cx: &impl HasDataLayout) -> bool {
92+
self.ptrs.range_is_empty(Self::adjusted_range_ptrs(range, cx))
9893
}
9994

10095
/// Returns all byte-wise provenance in the given range.
101-
fn range_get_bytes(&self, range: AllocRange) -> &[(Size, Prov)] {
96+
fn range_bytes_get(&self, range: AllocRange) -> &[(Size, Prov)] {
10297
if let Some(bytes) = self.bytes.as_ref() {
10398
bytes.range(range.start..range.end())
10499
} else {
105100
&[]
106101
}
107102
}
108103

104+
/// Same as `range_bytes_get(range).is_empty()`, but faster.
105+
fn range_bytes_is_empty(&self, range: AllocRange) -> bool {
106+
self.bytes.as_ref().is_none_or(|bytes| bytes.range_is_empty(range.start..range.end()))
107+
}
108+
109109
/// Get the provenance of a single byte.
110110
pub fn get(&self, offset: Size, cx: &impl HasDataLayout) -> Option<Prov> {
111-
let prov = self.range_get_ptrs(alloc_range(offset, Size::from_bytes(1)), cx);
111+
let prov = self.range_ptrs_get(alloc_range(offset, Size::from_bytes(1)), cx);
112112
debug_assert!(prov.len() <= 1);
113113
if let Some(entry) = prov.first() {
114114
// If it overlaps with this byte, it is on this byte.
@@ -132,7 +132,7 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
132132
/// limit access to provenance outside of the `Allocation` abstraction.
133133
///
134134
pub fn range_empty(&self, range: AllocRange, cx: &impl HasDataLayout) -> bool {
135-
self.range_get_ptrs_is_empty(range, cx) && self.range_get_bytes(range).is_empty()
135+
self.range_ptrs_is_empty(range, cx) && self.range_bytes_is_empty(range)
136136
}
137137

138138
/// Yields all the provenances stored in this map.
@@ -164,14 +164,14 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
164164
// provenance that overlaps with the given range.
165165
let (first, last) = {
166166
// Find all provenance overlapping the given range.
167-
if self.range_get_ptrs_is_empty(range, cx) {
167+
if self.range_ptrs_is_empty(range, cx) {
168168
// No provenance in this range, we are done. This is the common case.
169169
return Ok(());
170170
}
171171

172172
// This redoes some of the work of `range_get_ptrs_is_empty`, but this path is much
173173
// colder than the early return above, so it's worth it.
174-
let provenance = self.range_get_ptrs(range, cx);
174+
let provenance = self.range_ptrs_get(range, cx);
175175
(
176176
provenance.first().unwrap().0,
177177
provenance.last().unwrap().0 + cx.data_layout().pointer_size,
@@ -284,8 +284,8 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
284284
// This includes the existing bytewise provenance in the range, and ptr provenance
285285
// that overlaps with the begin/end of the range.
286286
let mut dest_bytes_box = None;
287-
let begin_overlap = self.range_get_ptrs(alloc_range(src.start, Size::ZERO), cx).first();
288-
let end_overlap = self.range_get_ptrs(alloc_range(src.end(), Size::ZERO), cx).first();
287+
let begin_overlap = self.range_ptrs_get(alloc_range(src.start, Size::ZERO), cx).first();
288+
let end_overlap = self.range_ptrs_get(alloc_range(src.end(), Size::ZERO), cx).first();
289289
if !Prov::OFFSET_IS_ADDR {
290290
// There can't be any bytewise provenance, and we cannot split up the begin/end overlap.
291291
if let Some(entry) = begin_overlap {
@@ -308,10 +308,10 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
308308
} else {
309309
trace!("no start overlapping entry");
310310
}
311+
311312
// Then the main part, bytewise provenance from `self.bytes`.
312-
if let Some(all_bytes) = self.bytes.as_ref() {
313-
bytes.extend(all_bytes.range(src.start..src.end()));
314-
}
313+
bytes.extend(self.range_bytes_get(src));
314+
315315
// And finally possibly parts of a pointer at the end.
316316
if let Some(entry) = end_overlap {
317317
trace!("end overlapping entry: {entry:?}");

0 commit comments

Comments
 (0)
Please sign in to comment.