From 1f82a9f89e3c0388442835df632b56ccea08a971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Christopher=20K=C3=B6hl?= Date: Tue, 25 Oct 2022 00:56:51 +0200 Subject: [PATCH] Move HasTop and HasBottom into lattice.rs --- .../src/framework/lattice.rs | 34 +++++++++++++++++++ .../rustc_mir_dataflow/src/value_analysis.rs | 33 ++++-------------- .../src/dataflow_const_prop.rs | 2 +- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_mir_dataflow/src/framework/lattice.rs b/compiler/rustc_mir_dataflow/src/framework/lattice.rs index d6b89eb8227..f0e75c53ea1 100644 --- a/compiler/rustc_mir_dataflow/src/framework/lattice.rs +++ b/compiler/rustc_mir_dataflow/src/framework/lattice.rs @@ -73,6 +73,16 @@ pub trait MeetSemiLattice: Eq { fn meet(&mut self, other: &Self) -> bool; } +/// A set that has a "bottom" element, which is less than or equal to any other element. +pub trait HasBottom { + fn bottom() -> Self; +} + +/// A set that has a "top" element, which is greater than or equal to any other element. +pub trait HasTop { + fn top() -> Self; +} + /// A `bool` is a "two-point" lattice with `true` as the top element and `false` as the bottom: /// /// ```text @@ -102,6 +112,18 @@ impl MeetSemiLattice for bool { } } +impl HasBottom for bool { + fn bottom() -> Self { + false + } +} + +impl HasTop for bool { + fn top() -> Self { + true + } +} + /// A tuple (or list) of lattices is itself a lattice whose least upper bound is the concatenation /// of the least upper bounds of each element of the tuple (or list). /// @@ -250,3 +272,15 @@ impl MeetSemiLattice for FlatSet { true } } + +impl HasBottom for FlatSet { + fn bottom() -> Self { + Self::Bottom + } +} + +impl HasTop for FlatSet { + fn top() -> Self { + Self::Top + } +} diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index bfe714342e2..a3194ebc30c 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -56,9 +56,10 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::DUMMY_SP; use rustc_target::abi::VariantIdx; +use crate::lattice::{HasBottom, HasTop}; use crate::{ - fmt::DebugWithContext, lattice::FlatSet, Analysis, AnalysisDomain, CallReturnPlaces, - JoinSemiLattice, SwitchIntEdgeEffects, + fmt::DebugWithContext, Analysis, AnalysisDomain, CallReturnPlaces, JoinSemiLattice, + SwitchIntEdgeEffects, }; pub trait ValueAnalysis<'tcx> { @@ -846,8 +847,8 @@ pub enum ValueOrPlace { Place(PlaceIndex), } -impl HasTop for ValueOrPlace { - fn top() -> Self { +impl ValueOrPlace { + pub fn top() -> Self { ValueOrPlace::Value(V::top()) } } @@ -859,8 +860,8 @@ pub enum ValueOrPlaceOrRef { Ref(PlaceIndex), } -impl HasTop for ValueOrPlaceOrRef { - fn top() -> Self { +impl ValueOrPlaceOrRef { + pub fn top() -> Self { ValueOrPlaceOrRef::Value(V::top()) } } @@ -874,26 +875,6 @@ impl From> for ValueOrPlaceOrRef { } } -pub trait HasBottom { - fn bottom() -> Self; -} - -pub trait HasTop { - fn top() -> Self; -} - -impl HasBottom for FlatSet { - fn bottom() -> Self { - Self::Bottom - } -} - -impl HasTop for FlatSet { - fn top() -> Self { - Self::Top - } -} - /// The set of projection elements that can be used by a tracked place. /// /// For now, downcast is not allowed due to aliasing between variants (see #101168). diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index 62b0902e60a..93f7ddbee79 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -8,7 +8,7 @@ use rustc_middle::mir::visit::{MutVisitor, Visitor}; use rustc_middle::mir::*; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_mir_dataflow::value_analysis::{ - HasTop, Map, State, TrackElem, ValueAnalysis, ValueOrPlace, ValueOrPlaceOrRef, + Map, State, TrackElem, ValueAnalysis, ValueOrPlace, ValueOrPlaceOrRef, }; use rustc_mir_dataflow::{lattice::FlatSet, Analysis, ResultsVisitor, SwitchIntEdgeEffects}; use rustc_span::DUMMY_SP;