1//! Traits used to represent [lattices] for use as the domain of a dataflow analysis.
2//!
3//! # Overview
4//!
5//! The most common lattice is a powerset of some set `S`, ordered by [set inclusion]. The [Hasse
6//! diagram] for the powerset of a set with two elements (`X` and `Y`) is shown below. Note that
7//! distinct elements at the same height in a Hasse diagram (e.g. `{X}` and `{Y}`) are
8//! *incomparable*, not equal.
9//!
10//! ```text
11//! {X, Y} <- top
12//! / \
13//! {X} {Y}
14//! \ /
15//! {} <- bottom
16//!
17//! ```
18//!
19//! The defining characteristic of a lattice—the one that differentiates it from a [partially
20//! ordered set][poset]—is the existence of a *unique* least upper and greatest lower bound for
21//! every pair of elements. The lattice join operator (`∨`) returns the least upper bound, and the
22//! lattice meet operator (`∧`) returns the greatest lower bound. Types that implement one operator
23//! but not the other are known as semilattices. Dataflow analysis only uses the join operator and
24//! will work with any join-semilattice, but both should be specified when possible.
25//!
26//! ## `PartialOrd`
27//!
28//! Given that it represents a partially ordered set, you may be surprised that [`JoinSemiLattice`]
29//! does not have [`PartialOrd`] as a supertrait. This
30//! is because most standard library types use lexicographic ordering instead of set inclusion for
31//! their `PartialOrd` impl. Since we do not actually need to compare lattice elements to run a
32//! dataflow analysis, there's no need for a newtype wrapper with a custom `PartialOrd` impl. The
33//! only benefit would be the ability to check that the least upper (or greatest lower) bound
34//! returned by the lattice join (or meet) operator was in fact greater (or lower) than the inputs.
35//!
36//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
37//! [set inclusion]: https://en.wikipedia.org/wiki/Subset
38//! [Hasse diagram]: https://en.wikipedia.org/wiki/Hasse_diagram
39//! [poset]: https://en.wikipedia.org/wiki/Partially_ordered_set
4041use rustc_index::Idx;
42use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
4344use crate::framework::BitSetExt;
4546/// A [partially ordered set][poset] that has a [least upper bound][lub] for any pair of elements
47/// in the set.
48///
49/// [lub]: https://en.wikipedia.org/wiki/Infimum_and_supremum
50/// [poset]: https://en.wikipedia.org/wiki/Partially_ordered_set
51pub trait JoinSemiLattice: Eq {
52/// Computes the least upper bound of two elements, storing the result in `self` and returning
53 /// `true` if `self` has changed.
54 ///
55 /// The lattice join operator is abbreviated as `∨`.
56fn join(&mut self, other: &Self) -> bool;
57}
5859/// A set that has a "bottom" element, which is less than or equal to any other element.
60pub trait HasBottom {
61const BOTTOM: Self;
6263fn is_bottom(&self) -> bool;
64}
6566/// A set that has a "top" element, which is greater than or equal to any other element.
67pub trait HasTop {
68const TOP: Self;
69}
7071/// A `DenseBitSet` represents the lattice formed by the powerset of all possible values of the
72/// index type `T` ordered by inclusion. Equivalently, it is a tuple of "two-point" lattices, one
73/// for each possible value of `T`.
74impl<T: Idx> JoinSemiLatticefor DenseBitSet<T> {
75fn join(&mut self, other: &Self) -> bool {
76self.union(other)
77 }
78}
7980impl<T: Idx> JoinSemiLatticefor MixedBitSet<T> {
81fn join(&mut self, other: &Self) -> bool {
82self.union(other)
83 }
84}
8586/// Extends a type `T` with top and bottom elements to make it a partially ordered set in which no
87/// value of `T` is comparable with any other.
88///
89/// A flat set has the following [Hasse diagram]:
90///
91/// ```text
92/// top
93/// / ... / / \ \ ... \
94/// all possible values of `T`
95/// \ ... \ \ / / ... /
96/// bottom
97/// ```
98///
99/// [Hasse diagram]: https://en.wikipedia.org/wiki/Hasse_diagram
100#[derive(#[automatically_derived]
impl<T: ::core::clone::Clone> ::core::clone::Clone for FlatSet<T> {
#[inline]
fn clone(&self) -> FlatSet<T> {
match self {
FlatSet::Bottom => FlatSet::Bottom,
FlatSet::Elem(__self_0) =>
FlatSet::Elem(::core::clone::Clone::clone(__self_0)),
FlatSet::Top => FlatSet::Top,
}
}
}Clone, #[automatically_derived]
impl<T: ::core::marker::Copy> ::core::marker::Copy for FlatSet<T> { }Copy, #[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for FlatSet<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
FlatSet::Bottom => ::core::fmt::Formatter::write_str(f, "Bottom"),
FlatSet::Elem(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Elem",
&__self_0),
FlatSet::Top => ::core::fmt::Formatter::write_str(f, "Top"),
}
}
}Debug, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for FlatSet<T> {
#[inline]
fn eq(&self, other: &FlatSet<T>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(FlatSet::Elem(__self_0), FlatSet::Elem(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for FlatSet<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<T>;
}
}Eq)]
101pub enum FlatSet<T> {
102 Bottom,
103 Elem(T),
104 Top,
105}
106107impl<T: Clone + Eq> JoinSemiLatticefor FlatSet<T> {
108fn join(&mut self, other: &Self) -> bool {
109let result = match (&*self, other) {
110 (Self::Top, _) | (_, Self::Bottom) => return false,
111 (Self::Elem(a), Self::Elem(b)) if a == b => return false,
112113 (Self::Bottom, Self::Elem(x)) => Self::Elem(x.clone()),
114115_ => Self::Top,
116 };
117118*self = result;
119true
120}
121}
122123impl<T> HasBottomfor FlatSet<T> {
124const BOTTOM: Self = Self::Bottom;
125126fn is_bottom(&self) -> bool {
127#[allow(non_exhaustive_omitted_patterns)] match self {
Self::Bottom => true,
_ => false,
}matches!(self, Self::Bottom)128 }
129}
130131impl<T> HasTopfor FlatSet<T> {
132const TOP: Self = Self::Top;
133}
134135/// Extend a lattice with a bottom value to represent an unreachable execution.
136///
137/// The only useful action on an unreachable state is joining it with a reachable one to make it
138/// reachable. All other actions, gen/kill for instance, are no-ops.
139#[derive(#[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for MaybeReachable<T> {
#[inline]
fn eq(&self, other: &MaybeReachable<T>) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(MaybeReachable::Reachable(__self_0),
MaybeReachable::Reachable(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for MaybeReachable<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<T>;
}
}Eq, #[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for MaybeReachable<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
MaybeReachable::Unreachable =>
::core::fmt::Formatter::write_str(f, "Unreachable"),
MaybeReachable::Reachable(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Reachable", &__self_0),
}
}
}Debug)]
140pub enum MaybeReachable<T> {
141 Unreachable,
142 Reachable(T),
143}
144145impl<T> MaybeReachable<T> {
146pub fn is_reachable(&self) -> bool {
147#[allow(non_exhaustive_omitted_patterns)] match self {
MaybeReachable::Reachable(_) => true,
_ => false,
}matches!(self, MaybeReachable::Reachable(_))148 }
149}
150151impl<S> MaybeReachable<S> {
152/// Return whether the current state contains the given element. If the state is unreachable,
153 /// it does no contain anything.
154pub fn contains<T>(&self, elem: T) -> bool155where
156S: BitSetExt<T>,
157 {
158match self {
159 MaybeReachable::Unreachable => false,
160 MaybeReachable::Reachable(set) => set.contains(elem),
161 }
162 }
163}
164165impl<T, S: BitSetExt<T>> BitSetExt<T> for MaybeReachable<S> {
166fn contains(&self, elem: T) -> bool {
167self.contains(elem)
168 }
169}
170171impl<V: Clone> Clonefor MaybeReachable<V> {
172fn clone(&self) -> Self {
173match self {
174 MaybeReachable::Reachable(x) => MaybeReachable::Reachable(x.clone()),
175 MaybeReachable::Unreachable => MaybeReachable::Unreachable,
176 }
177 }
178179fn clone_from(&mut self, source: &Self) {
180match (&mut *self, source) {
181 (MaybeReachable::Reachable(x), MaybeReachable::Reachable(y)) => {
182x.clone_from(y);
183 }
184_ => *self = source.clone(),
185 }
186 }
187}
188189impl<T: JoinSemiLattice + Clone> JoinSemiLatticefor MaybeReachable<T> {
190fn join(&mut self, other: &Self) -> bool {
191// Unreachable acts as a bottom.
192match (&mut *self, &other) {
193 (_, MaybeReachable::Unreachable) => false,
194 (MaybeReachable::Unreachable, _) => {
195*self = other.clone();
196true
197}
198 (MaybeReachable::Reachable(this), MaybeReachable::Reachable(other)) => this.join(other),
199 }
200 }
201}