about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-10-14 13:26:14 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2023-10-21 07:01:25 +0000
commit4abea8366339b66932bf80203dd0884366fdb872 (patch)
tree80fea6f48cfe6643a7358348611925bcfc08f501
parentdf9e5ee03896889ed20d9dc8ebecaed1a429a776 (diff)
downloadrust-4abea8366339b66932bf80203dd0884366fdb872.tar.gz
rust-4abea8366339b66932bf80203dd0884366fdb872.zip
Improve naming and comments.
-rw-r--r--compiler/rustc_mir_dataflow/src/value_analysis.rs62
-rw-r--r--compiler/rustc_mir_transform/src/jump_threading.rs2
2 files changed, 46 insertions, 18 deletions
diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs
index 77c9a22df79..025d2ddfd4f 100644
--- a/compiler/rustc_mir_dataflow/src/value_analysis.rs
+++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs
@@ -496,10 +496,12 @@ impl<V: Clone> State<V> {
         values.raw.fill(value);
     }
 
+    /// Assign `value` to all places that are contained in `place` or may alias one.
     pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
-        self.flood_with_extra(place, None, map, value)
+        self.flood_with_tail_elem(place, None, map, value)
     }
 
+    /// Assign `TOP` to all places that are contained in `place` or may alias one.
     pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map)
     where
         V: HasTop,
@@ -507,10 +509,12 @@ impl<V: Clone> State<V> {
         self.flood_with(place, map, V::TOP)
     }
 
+    /// Assign `value` to the discriminant of `place` and all places that may alias it.
     pub fn flood_discr_with(&mut self, place: PlaceRef<'_>, map: &Map, value: V) {
-        self.flood_with_extra(place, Some(TrackElem::Discriminant), map, value)
+        self.flood_with_tail_elem(place, Some(TrackElem::Discriminant), map, value)
     }
 
+    /// Assign `TOP` to the discriminant of `place` and all places that may alias it.
     pub fn flood_discr(&mut self, place: PlaceRef<'_>, map: &Map)
     where
         V: HasTop,
@@ -518,7 +522,14 @@ impl<V: Clone> State<V> {
         self.flood_discr_with(place, map, V::TOP)
     }
 
-    pub fn flood_with_extra(
+    /// This method is the most general version of the `flood_*` method.
+    ///
+    /// Assign `value` on the given place and all places that may alias it. In particular, when
+    /// the given place has a variant downcast, we invoke the function on all the other variants.
+    ///
+    /// `tail_elem` allows to support discriminants that are not a place in MIR, but that we track
+    /// as such.
+    pub fn flood_with_tail_elem(
         &mut self,
         place: PlaceRef<'_>,
         tail_elem: Option<TrackElem>,
@@ -602,62 +613,79 @@ impl<V: Clone> State<V> {
         }
     }
 
-    /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
+    /// Retrieve the value stored for a place, or `None` if it is not tracked.
     pub fn try_get(&self, place: PlaceRef<'_>, map: &Map) -> Option<V> {
         let place = map.find(place)?;
         self.try_get_idx(place, map)
     }
 
-    /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
+    /// Retrieve the discriminant stored for a place, or `None` if it is not tracked.
     pub fn try_get_discr(&self, place: PlaceRef<'_>, map: &Map) -> Option<V> {
         let place = map.find_discr(place)?;
         self.try_get_idx(place, map)
     }
 
-    /// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
+    /// Retrieve the slice length stored for a place, or `None` if it is not tracked.
+    pub fn try_get_len(&self, place: PlaceRef<'_>, map: &Map) -> Option<V> {
+        let place = map.find_len(place)?;
+        self.try_get_idx(place, map)
+    }
+
+    /// Retrieve the value stored for a place index, or `None` if it is not tracked.
     pub fn try_get_idx(&self, place: PlaceIndex, map: &Map) -> Option<V> {
         match &self.0 {
             StateData::Reachable(values) => {
                 map.places[place].value_index.map(|v| values[v].clone())
             }
-            StateData::Unreachable => {
-                // Because this is unreachable, we can return any value we want.
-                None
-            }
+            StateData::Unreachable => None,
         }
     }
 
     /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
+    ///
+    /// This method returns ⊥ if the place is tracked and the state is unreachable.
     pub fn get(&self, place: PlaceRef<'_>, map: &Map) -> V
     where
         V: HasBottom + HasTop,
     {
-        map.find(place).map(|place| self.get_idx(place, map)).unwrap_or(V::TOP)
+        match &self.0 {
+            StateData::Reachable(_) => self.try_get(place, map).unwrap_or(V::TOP),
+            // Because this is unreachable, we can return any value we want.
+            StateData::Unreachable => V::BOTTOM,
+        }
     }
 
     /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
+    ///
+    /// This method returns ⊥ the current state is unreachable.
     pub fn get_discr(&self, place: PlaceRef<'_>, map: &Map) -> V
     where
         V: HasBottom + HasTop,
     {
-        match map.find_discr(place) {
-            Some(place) => self.get_idx(place, map),
-            None => V::TOP,
+        match &self.0 {
+            StateData::Reachable(_) => self.try_get_discr(place, map).unwrap_or(V::TOP),
+            // Because this is unreachable, we can return any value we want.
+            StateData::Unreachable => V::BOTTOM,
         }
     }
 
     /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
+    ///
+    /// This method returns ⊥ the current state is unreachable.
     pub fn get_len(&self, place: PlaceRef<'_>, map: &Map) -> V
     where
         V: HasBottom + HasTop,
     {
-        match map.find_len(place) {
-            Some(place) => self.get_idx(place, map),
-            None => V::TOP,
+        match &self.0 {
+            StateData::Reachable(_) => self.try_get_len(place, map).unwrap_or(V::TOP),
+            // Because this is unreachable, we can return any value we want.
+            StateData::Unreachable => V::BOTTOM,
         }
     }
 
     /// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
+    ///
+    /// This method returns ⊥ the current state is unreachable.
     pub fn get_idx(&self, place: PlaceIndex, map: &Map) -> V
     where
         V: HasBottom + HasTop,
diff --git a/compiler/rustc_mir_transform/src/jump_threading.rs b/compiler/rustc_mir_transform/src/jump_threading.rs
index 6558b04d4bf..a5ee843e23a 100644
--- a/compiler/rustc_mir_transform/src/jump_threading.rs
+++ b/compiler/rustc_mir_transform/src/jump_threading.rs
@@ -222,7 +222,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
             //   _1 = 5 // Whatever happens here, it won't change the result of a `SwitchInt`.
             //   _1 = 6
             if let Some((lhs, tail)) = self.mutated_statement(stmt) {
-                state.flood_with_extra(lhs.as_ref(), tail, self.map, ConditionSet::default());
+                state.flood_with_tail_elem(lhs.as_ref(), tail, self.map, ConditionSet::default());
             }
         }