]> git.lizzy.rs Git - rust.git/commitdiff
Make RegionVid use newtype_index!
authorSantiago Pastorino <spastorino@gmail.com>
Fri, 10 Nov 2017 09:01:22 +0000 (10:01 +0100)
committerNiko Matsakis <niko@alum.mit.edu>
Wed, 22 Nov 2017 08:51:54 +0000 (03:51 -0500)
Closes #45843

src/librustc/infer/lexical_region_resolve/mod.rs
src/librustc/infer/region_constraints/mod.rs
src/librustc/infer/unify_key.rs
src/librustc/ty/sty.rs
src/librustc/util/ppaux.rs

index 0692d284d7c16eb4f106397a18b670cfb4f3d22f..5a4f2157298b066715a3d92740f7ebb69b80a682 100644 (file)
@@ -171,7 +171,7 @@ fn expand_givens(&mut self, graph: &RegionGraph) {
         for (r, vid) in seeds {
             // While all things transitively reachable in the graph
             // from the variable (`'0` in the example above).
-            let seed_index = NodeIndex(vid.index as usize);
+            let seed_index = NodeIndex(vid.index() as usize);
             for succ_index in graph.depth_traverse(seed_index, OUTGOING) {
                 let succ_index = succ_index.0;
 
@@ -512,16 +512,16 @@ fn construct_graph(&self) -> RegionGraph<'tcx> {
             match *constraint {
                 Constraint::VarSubVar(a_id, b_id) => {
                     graph.add_edge(
-                        NodeIndex(a_id.index as usize),
-                        NodeIndex(b_id.index as usize),
+                        NodeIndex(a_id.index() as usize),
+                        NodeIndex(b_id.index() as usize),
                         *constraint,
                     );
                 }
                 Constraint::RegSubVar(_, b_id) => {
-                    graph.add_edge(dummy_source, NodeIndex(b_id.index as usize), *constraint);
+                    graph.add_edge(dummy_source, NodeIndex(b_id.index() as usize), *constraint);
                 }
                 Constraint::VarSubReg(a_id, _) => {
-                    graph.add_edge(NodeIndex(a_id.index as usize), dummy_sink, *constraint);
+                    graph.add_edge(NodeIndex(a_id.index() as usize), dummy_sink, *constraint);
                 }
                 Constraint::RegSubReg(..) => {
                     // this would be an edge from `dummy_source` to
@@ -630,9 +630,9 @@ struct WalkState<'tcx> {
             let node_idx = state.stack.pop().unwrap();
 
             // check whether we've visited this node on some previous walk
-            if dup_vec[node_idx.index as usize] == u32::MAX {
-                dup_vec[node_idx.index as usize] = orig_node_idx.index;
-            } else if dup_vec[node_idx.index as usize] != orig_node_idx.index {
+            if dup_vec[node_idx.index() as usize] == u32::MAX {
+                dup_vec[node_idx.index() as usize] = orig_node_idx.index() as u32;
+            } else if dup_vec[node_idx.index() as usize] != orig_node_idx.index() as u32 {
                 state.dup_found = true;
             }
 
@@ -659,7 +659,7 @@ fn process_edges<'tcx>(
         ) {
             debug!("process_edges(source_vid={:?}, dir={:?})", source_vid, dir);
 
-            let source_node_index = NodeIndex(source_vid.index as usize);
+            let source_node_index = NodeIndex(source_vid.index() as usize);
             for (_, edge) in graph.adjacent_edges(source_node_index, dir) {
                 match edge.data {
                     Constraint::VarSubVar(from_vid, to_vid) => {
index 096037ebe880cae5a08fd106b71b372d278befd6..72740dd40be2932dab0c771d6c3b9d717be5157a 100644 (file)
@@ -16,7 +16,7 @@
 use super::{MiscVariable, RegionVariableOrigin, SubregionOrigin};
 use super::unify_key;
 
-use rustc_data_structures::indexed_vec::IndexVec;
+use rustc_data_structures::indexed_vec::{IndexVec, Idx};
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_data_structures::unify::{self, UnificationTable};
 use ty::{self, Ty, TyCtxt};
@@ -404,7 +404,7 @@ fn rollback_undo_entry(&mut self, undo_entry: UndoLogEntry<'tcx>) {
             }
             AddVar(vid) => {
                 self.var_origins.pop().unwrap();
-                assert_eq!(self.var_origins.len(), vid.index as usize);
+                assert_eq!(self.var_origins.len(), vid.index() as usize);
             }
             AddConstraint(ref constraint) => {
                 self.data.constraints.remove(constraint);
index d7e3a53ff25c92dca5f4129e81480fbfb3873287..99b11794cc5b577632b83161652f11e356248260 100644 (file)
@@ -33,7 +33,7 @@ pub struct RegionVidKey {
 
 impl Combine for RegionVidKey {
     fn combine(&self, other: &RegionVidKey) -> RegionVidKey {
-        let min_vid = if self.min_vid.index < other.min_vid.index {
+        let min_vid = if self.min_vid.index() < other.min_vid.index() {
             self.min_vid
         } else {
             other.min_vid
@@ -45,8 +45,8 @@ fn combine(&self, other: &RegionVidKey) -> RegionVidKey {
 
 impl UnifyKey for ty::RegionVid {
     type Value = RegionVidKey;
-    fn index(&self) -> u32 { self.index }
-    fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid { index: i } }
+    fn index(&self) -> u32 { self.0 }
+    fn from_index(i: u32) -> ty::RegionVid { ty::RegionVid(i) }
     fn tag(_: Option<ty::RegionVid>) -> &'static str { "RegionVid" }
 }
 
index 7406fbf820893ed2804ced1e142f08bcc1a0e475..9d393296c5b22ff5dda7486ccd5b555a46f77841 100644 (file)
@@ -998,22 +998,11 @@ pub struct FloatVid {
     pub index: u32,
 }
 
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy, PartialOrd, Ord)]
-pub struct RegionVid {
-    pub index: u32,
-}
-
-// FIXME: We could convert this to use `newtype_index!`
-impl Idx for RegionVid {
-    fn new(value: usize) -> Self {
-        assert!(value < ::std::u32::MAX as usize);
-        RegionVid { index: value as u32 }
-    }
-
-    fn index(self) -> usize {
-        self.index as usize
-    }
-}
+newtype_index!(RegionVid
+    {
+        pub idx
+        DEBUG_FORMAT = custom,
+    });
 
 #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, PartialOrd, Ord)]
 pub struct SkolemizedRegionVid {
index 2c3a32b2d159ee8a6403faf2c720169329bced7f..9ff3d73f5c40e95678b06e40bd1f042145e8f67c 100644 (file)
@@ -726,7 +726,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                     }
                 }
                 ty::ReVar(region_vid) if cx.identify_regions => {
-                    write!(f, "'{}rv", region_vid.index)
+                    write!(f, "'{}rv", region_vid.index())
                 }
                 ty::ReScope(_) |
                 ty::ReVar(_) |
@@ -850,7 +850,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 impl fmt::Debug for ty::RegionVid {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "'_#{}r", self.index)
+        write!(f, "'_#{}r", self.index())
     }
 }