Skip to content

Commit 71138e9

Browse files
committed
Make HasTop and HasBottom consts.
1 parent f7b831a commit 71138e9

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

compiler/rustc_mir_dataflow/src/framework/lattice.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ pub trait MeetSemiLattice: Eq {
7575

7676
/// A set that has a "bottom" element, which is less than or equal to any other element.
7777
pub trait HasBottom {
78-
fn bottom() -> Self;
78+
const BOTTOM: Self;
7979
}
8080

8181
/// A set that has a "top" element, which is greater than or equal to any other element.
8282
pub trait HasTop {
83-
fn top() -> Self;
83+
const TOP: Self;
8484
}
8585

8686
/// A `bool` is a "two-point" lattice with `true` as the top element and `false` as the bottom:
@@ -113,15 +113,11 @@ impl MeetSemiLattice for bool {
113113
}
114114

115115
impl HasBottom for bool {
116-
fn bottom() -> Self {
117-
false
118-
}
116+
const BOTTOM: Self = false;
119117
}
120118

121119
impl HasTop for bool {
122-
fn top() -> Self {
123-
true
124-
}
120+
const TOP: Self = true;
125121
}
126122

127123
/// A tuple (or list) of lattices is itself a lattice whose least upper bound is the concatenation
@@ -274,13 +270,9 @@ impl<T: Clone + Eq> MeetSemiLattice for FlatSet<T> {
274270
}
275271

276272
impl<T> HasBottom for FlatSet<T> {
277-
fn bottom() -> Self {
278-
Self::Bottom
279-
}
273+
const BOTTOM: Self = Self::Bottom;
280274
}
281275

282276
impl<T> HasTop for FlatSet<T> {
283-
fn top() -> Self {
284-
Self::Top
285-
}
277+
const TOP: Self = Self::Top;
286278
}

compiler/rustc_mir_dataflow/src/value_analysis.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ pub trait ValueAnalysis<'tcx> {
7474
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
7575
// StorageLive leaves the local in an uninitialized state.
7676
// StorageDead makes it UB to access the local afterwards.
77-
state.flood_with(Place::from(*local).as_ref(), self.map(), Self::Value::bottom());
77+
state.flood_with(Place::from(*local).as_ref(), self.map(), Self::Value::BOTTOM);
7878
}
7979
StatementKind::Deinit(box place) => {
8080
// Deinit makes the place uninitialized.
81-
state.flood_with(place.as_ref(), self.map(), Self::Value::bottom());
81+
state.flood_with(place.as_ref(), self.map(), Self::Value::BOTTOM);
8282
}
8383
StatementKind::Retag(..) => {
8484
// We don't track references.
@@ -154,7 +154,7 @@ pub trait ValueAnalysis<'tcx> {
154154
Rvalue::CopyForDeref(place) => self.handle_operand(&Operand::Copy(*place), state),
155155
Rvalue::Ref(..) | Rvalue::AddressOf(..) => {
156156
// We don't track such places.
157-
ValueOrPlace::top()
157+
ValueOrPlace::TOP
158158
}
159159
Rvalue::Repeat(..)
160160
| Rvalue::ThreadLocalRef(..)
@@ -168,7 +168,7 @@ pub trait ValueAnalysis<'tcx> {
168168
| Rvalue::Aggregate(..)
169169
| Rvalue::ShallowInitBox(..) => {
170170
// No modification is possible through these r-values.
171-
ValueOrPlace::top()
171+
ValueOrPlace::TOP
172172
}
173173
}
174174
}
@@ -196,7 +196,7 @@ pub trait ValueAnalysis<'tcx> {
196196
self.map()
197197
.find(place.as_ref())
198198
.map(ValueOrPlace::Place)
199-
.unwrap_or(ValueOrPlace::top())
199+
.unwrap_or(ValueOrPlace::TOP)
200200
}
201201
}
202202
}
@@ -214,7 +214,7 @@ pub trait ValueAnalysis<'tcx> {
214214
_constant: &Constant<'tcx>,
215215
_state: &mut State<Self::Value>,
216216
) -> Self::Value {
217-
Self::Value::top()
217+
Self::Value::TOP
218218
}
219219

220220
/// The effect of a successful function call return should not be
@@ -229,7 +229,7 @@ pub trait ValueAnalysis<'tcx> {
229229
// Effect is applied by `handle_call_return`.
230230
}
231231
TerminatorKind::Drop { place, .. } => {
232-
state.flood_with(place.as_ref(), self.map(), Self::Value::bottom());
232+
state.flood_with(place.as_ref(), self.map(), Self::Value::BOTTOM);
233233
}
234234
TerminatorKind::Yield { .. } => {
235235
// They would have an effect, but are not allowed in this phase.
@@ -307,7 +307,7 @@ impl<'tcx, T: ValueAnalysis<'tcx>> AnalysisDomain<'tcx> for ValueAnalysisWrapper
307307
fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
308308
// The initial state maps all tracked places of argument projections to ⊤ and the rest to ⊥.
309309
assert!(matches!(state.0, StateData::Unreachable));
310-
let values = IndexVec::from_elem_n(T::Value::bottom(), self.0.map().value_count);
310+
let values = IndexVec::from_elem_n(T::Value::BOTTOM, self.0.map().value_count);
311311
*state = State(StateData::Reachable(values));
312312
for arg in body.args_iter() {
313313
state.flood(PlaceRef { local: arg, projection: &[] }, self.0.map());
@@ -437,7 +437,7 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
437437
}
438438

439439
pub fn flood_all(&mut self) {
440-
self.flood_all_with(V::top())
440+
self.flood_all_with(V::TOP)
441441
}
442442

443443
pub fn flood_all_with(&mut self, value: V) {
@@ -455,7 +455,7 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
455455
}
456456

457457
pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map) {
458-
self.flood_with(place, map, V::top())
458+
self.flood_with(place, map, V::TOP)
459459
}
460460

461461
pub fn flood_discr_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
@@ -468,7 +468,7 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
468468
}
469469

470470
pub fn flood_discr(&mut self, place: PlaceRef<'_>, map: &Map) {
471-
self.flood_discr_with(place, map, V::top())
471+
self.flood_discr_with(place, map, V::TOP)
472472
}
473473

474474
/// Low-level method that assigns to a place.
@@ -538,26 +538,26 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
538538

539539
/// Retrieve the value stored for a place, or ⊤ if it is not tracked.
540540
pub fn get(&self, place: PlaceRef<'_>, map: &Map) -> V {
541-
map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::top())
541+
map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::TOP)
542542
}
543543

544544
/// Retrieve the value stored for a place, or ⊤ if it is not tracked.
545545
pub fn get_discr(&self, place: PlaceRef<'_>, map: &Map) -> V {
546546
match map.find_discr(place) {
547547
Some(place) => self.get_idx(place, map),
548-
None => V::top(),
548+
None => V::TOP,
549549
}
550550
}
551551

552552
/// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
553553
pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V {
554554
match &self.0 {
555555
StateData::Reachable(values) => {
556-
map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::top())
556+
map.places[place].value_index.map(|v| values[v].clone()).unwrap_or(V::TOP)
557557
}
558558
StateData::Unreachable => {
559559
// Because this is unreachable, we can return any value we want.
560-
V::bottom()
560+
V::BOTTOM
561561
}
562562
}
563563
}
@@ -909,9 +909,7 @@ pub enum ValueOrPlace<V> {
909909
}
910910

911911
impl<V: HasTop> ValueOrPlace<V> {
912-
pub fn top() -> Self {
913-
ValueOrPlace::Value(V::top())
914-
}
912+
pub const TOP: Self = ValueOrPlace::Value(V::TOP);
915913
}
916914

917915
/// The set of projection elements that can be used by a tracked place.

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
208208
_ => unreachable!(),
209209
}
210210
.map(|result| ValueOrPlace::Value(self.wrap_immediate(result, *ty)))
211-
.unwrap_or(ValueOrPlace::top()),
212-
_ => ValueOrPlace::top(),
211+
.unwrap_or(ValueOrPlace::TOP),
212+
_ => ValueOrPlace::TOP,
213213
},
214214
Rvalue::BinaryOp(op, box (left, right)) => {
215215
// Overflows must be ignored here.

0 commit comments

Comments
 (0)