From 43dc48c7ffb45c714b33b1e47f23f6d711c89596 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sat, 5 Mar 2016 08:40:33 -0500 Subject: [PATCH] apply rustfmt to librustc_data_structures, correcting rust-lang-nursery/rustfmt#836 --- src/librustc_data_structures/bitvec.rs | 36 +++-- src/librustc_data_structures/fnv.rs | 8 +- src/librustc_data_structures/graph/mod.rs | 118 ++++++++------ src/librustc_data_structures/graph/tests.rs | 40 ++--- src/librustc_data_structures/ivar.rs | 15 +- src/librustc_data_structures/lib.rs | 3 +- .../obligation_forest/mod.rs | 152 +++++++++++------- .../obligation_forest/node_index.rs | 7 +- .../obligation_forest/test.rs | 105 ++++++------ .../obligation_forest/tree_index.rs | 3 +- src/librustc_data_structures/snapshot_vec.rs | 50 +++--- .../transitive_relation.rs | 118 +++++++------- src/librustc_data_structures/tuple_slice.rs | 14 +- src/librustc_data_structures/unify/mod.rs | 91 +++++------ src/librustc_data_structures/unify/tests.rs | 35 ++-- src/librustc_data_structures/veccell/mod.rs | 4 +- 16 files changed, 444 insertions(+), 355 deletions(-) diff --git a/src/librustc_data_structures/bitvec.rs b/src/librustc_data_structures/bitvec.rs index 7b5dacece8c..3677c8c5e59 100644 --- a/src/librustc_data_structures/bitvec.rs +++ b/src/librustc_data_structures/bitvec.rs @@ -10,7 +10,7 @@ /// A very simple BitVector type. pub struct BitVector { - data: Vec + data: Vec, } impl BitVector { @@ -40,7 +40,9 @@ pub fn insert_all(&mut self, all: &BitVector) -> bool { for (i, j) in self.data.iter_mut().zip(&all.data) { let value = *i; *i = value | *j; - if value != *i { changed = true; } + if value != *i { + changed = true; + } } changed } @@ -56,7 +58,7 @@ pub fn iter<'a>(&'a self) -> BitVectorIter<'a> { BitVectorIter { iter: self.data.iter(), current: 0, - idx: 0 + idx: 0, } } } @@ -64,7 +66,7 @@ pub fn iter<'a>(&'a self) -> BitVectorIter<'a> { pub struct BitVectorIter<'a> { iter: ::std::slice::Iter<'a, u64>, current: u64, - idx: usize + idx: usize, } impl<'a> Iterator for BitVectorIter<'a> { @@ -108,7 +110,7 @@ pub fn new(elements: usize) -> BitMatrix { let u64s_per_elem = u64s(elements); BitMatrix { elements: elements, - vector: vec![0; elements * u64s_per_elem] + vector: vec![0; elements * u64s_per_elem], } } @@ -123,9 +125,9 @@ pub fn add(&mut self, source: usize, target: usize) -> bool { let (start, _) = self.range(source); let (word, mask) = word_mask(target); let mut vector = &mut self.vector[..]; - let v1 = vector[start+word]; + let v1 = vector[start + word]; let v2 = v1 | mask; - vector[start+word] = v2; + vector[start + word] = v2; v1 != v2 } @@ -136,7 +138,7 @@ pub fn add(&mut self, source: usize, target: usize) -> bool { pub fn contains(&self, source: usize, target: usize) -> bool { let (start, _) = self.range(source); let (word, mask) = word_mask(target); - (self.vector[start+word] & mask) != 0 + (self.vector[start + word] & mask) != 0 } /// Returns those indices that are reachable from both `a` and @@ -150,8 +152,12 @@ pub fn intersection(&self, a: usize, b: usize) -> Vec { for (base, (i, j)) in (a_start..a_end).zip(b_start..b_end).enumerate() { let mut v = self.vector[i] & self.vector[j]; for bit in 0..64 { - if v == 0 { break; } - if v & 0x1 != 0 { result.push(base*64 + bit); } + if v == 0 { + break; + } + if v & 0x1 != 0 { + result.push(base * 64 + bit); + } v >>= 1; } } @@ -170,9 +176,7 @@ pub fn merge(&mut self, read: usize, write: usize) -> bool { let (write_start, write_end) = self.range(write); let vector = &mut self.vector[..]; let mut changed = false; - for (read_index, write_index) in - (read_start..read_end).zip(write_start..write_end) - { + for (read_index, write_index) in (read_start..read_end).zip(write_start..write_end) { let v1 = vector[write_index]; let v2 = v1 | vector[read_index]; vector[write_index] = v2; @@ -204,7 +208,8 @@ fn bitvec_iter_works() { bitvec.insert(65); bitvec.insert(66); bitvec.insert(99); - assert_eq!(bitvec.iter().collect::>(), [1, 10, 19, 62, 63, 64, 65, 66, 99]); + assert_eq!(bitvec.iter().collect::>(), + [1, 10, 19, 62, 63, 64, 65, 66, 99]); } #[test] @@ -217,7 +222,8 @@ fn bitvec_iter_works_2() { bitvec.insert(66); bitvec.insert(99); bitvec.insert(299); - assert_eq!(bitvec.iter().collect::>(), [1, 10, 19, 62, 66, 99, 299]); + assert_eq!(bitvec.iter().collect::>(), + [1, 10, 19, 62, 66, 99, 299]); } diff --git a/src/librustc_data_structures/fnv.rs b/src/librustc_data_structures/fnv.rs index da5f9f20892..0000c283a7a 100644 --- a/src/librustc_data_structures/fnv.rs +++ b/src/librustc_data_structures/fnv.rs @@ -36,7 +36,9 @@ pub fn FnvHashSet() -> FnvHashSet { impl Default for FnvHasher { #[inline] - fn default() -> FnvHasher { FnvHasher(0xcbf29ce484222325) } + fn default() -> FnvHasher { + FnvHasher(0xcbf29ce484222325) + } } impl Hasher for FnvHasher { @@ -51,5 +53,7 @@ fn write(&mut self, bytes: &[u8]) { } #[inline] - fn finish(&self) -> u64 { self.0 } + fn finish(&self) -> u64 { + self.0 + } } diff --git a/src/librustc_data_structures/graph/mod.rs b/src/librustc_data_structures/graph/mod.rs index f11856d7513..f33769f39e8 100644 --- a/src/librustc_data_structures/graph/mod.rs +++ b/src/librustc_data_structures/graph/mod.rs @@ -38,9 +38,9 @@ #[cfg(test)] mod tests; -pub struct Graph { - nodes: SnapshotVec> , - edges: SnapshotVec> , +pub struct Graph { + nodes: SnapshotVec>, + edges: SnapshotVec>, } pub struct Node { @@ -71,9 +71,13 @@ fn reverse(_: &mut Vec>, _: ()) {} impl Debug for Edge { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - write!(f, "Edge {{ next_edge: [{:?}, {:?}], source: {:?}, target: {:?}, data: {:?} }}", - self.next_edge[0], self.next_edge[1], self.source, - self.target, self.data) + write!(f, + "Edge {{ next_edge: [{:?}, {:?}], source: {:?}, target: {:?}, data: {:?} }}", + self.next_edge[0], + self.next_edge[1], + self.source, + self.target, + self.data) } } @@ -87,7 +91,9 @@ fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { // Use a private field here to guarantee no more instances are created: #[derive(Copy, Clone, Debug, PartialEq)] -pub struct Direction { repr: usize } +pub struct Direction { + repr: usize, +} pub const OUTGOING: Direction = Direction { repr: 0 }; @@ -95,16 +101,20 @@ pub struct Direction { repr: usize } impl NodeIndex { /// Returns unique id (unique with respect to the graph holding associated node). - pub fn node_id(&self) -> usize { self.0 } + pub fn node_id(&self) -> usize { + self.0 + } } impl EdgeIndex { /// Returns unique id (unique with respect to the graph holding associated edge). - pub fn edge_id(&self) -> usize { self.0 } + pub fn edge_id(&self) -> usize { + self.0 + } } -impl Graph { - pub fn new() -> Graph { +impl Graph { + pub fn new() -> Graph { Graph { nodes: SnapshotVec::new(), edges: SnapshotVec::new(), @@ -145,7 +155,7 @@ pub fn add_node(&mut self, data: N) -> NodeIndex { let idx = self.next_node_index(); self.nodes.push(Node { first_edge: [INVALID_EDGE_INDEX, INVALID_EDGE_INDEX], - data: data + data: data, }); idx } @@ -169,19 +179,14 @@ pub fn next_edge_index(&self) -> EdgeIndex { EdgeIndex(self.edges.len()) } - pub fn add_edge(&mut self, - source: NodeIndex, - target: NodeIndex, - data: E) -> EdgeIndex { + pub fn add_edge(&mut self, source: NodeIndex, target: NodeIndex, data: E) -> EdgeIndex { debug!("graph: add_edge({:?}, {:?}, {:?})", source, target, data); let idx = self.next_edge_index(); // read current first of the list of edges from each node - let source_first = self.nodes[source.0] - .first_edge[OUTGOING.repr]; - let target_first = self.nodes[target.0] - .first_edge[INCOMING.repr]; + let source_first = self.nodes[source.0].first_edge[OUTGOING.repr]; + let target_first = self.nodes[target.0].first_edge[INCOMING.repr]; // create the new edge, with the previous firsts from each node // as the next pointers @@ -189,7 +194,7 @@ pub fn add_edge(&mut self, next_edge: [source_first, target_first], source: source, target: target, - data: data + data: data, }); // adjust the firsts for each node target be the next object. @@ -230,38 +235,42 @@ pub fn next_adjacent(&self, edge: EdgeIndex, dir: Direction) -> EdgeIndex { /////////////////////////////////////////////////////////////////////////// // Iterating over nodes, edges - pub fn each_node<'a, F>(&'a self, mut f: F) -> bool where - F: FnMut(NodeIndex, &'a Node) -> bool, + pub fn each_node<'a, F>(&'a self, mut f: F) -> bool + where F: FnMut(NodeIndex, &'a Node) -> bool { //! Iterates over all edges defined in the graph. self.nodes.iter().enumerate().all(|(i, node)| f(NodeIndex(i), node)) } - pub fn each_edge<'a, F>(&'a self, mut f: F) -> bool where - F: FnMut(EdgeIndex, &'a Edge) -> bool, + pub fn each_edge<'a, F>(&'a self, mut f: F) -> bool + where F: FnMut(EdgeIndex, &'a Edge) -> bool { //! Iterates over all edges defined in the graph self.edges.iter().enumerate().all(|(i, edge)| f(EdgeIndex(i), edge)) } - pub fn outgoing_edges(&self, source: NodeIndex) -> AdjacentEdges { + pub fn outgoing_edges(&self, source: NodeIndex) -> AdjacentEdges { self.adjacent_edges(source, OUTGOING) } - pub fn incoming_edges(&self, source: NodeIndex) -> AdjacentEdges { + pub fn incoming_edges(&self, source: NodeIndex) -> AdjacentEdges { self.adjacent_edges(source, INCOMING) } - pub fn adjacent_edges(&self, source: NodeIndex, direction: Direction) -> AdjacentEdges { + pub fn adjacent_edges(&self, source: NodeIndex, direction: Direction) -> AdjacentEdges { let first_edge = self.node(source).first_edge[direction.repr]; - AdjacentEdges { graph: self, direction: direction, next: first_edge } + AdjacentEdges { + graph: self, + direction: direction, + next: first_edge, + } } - pub fn successor_nodes(&self, source: NodeIndex) -> AdjacentTargets { + pub fn successor_nodes(&self, source: NodeIndex) -> AdjacentTargets { self.outgoing_edges(source).targets() } - pub fn predecessor_nodes(&self, target: NodeIndex) -> AdjacentSources { + pub fn predecessor_nodes(&self, target: NodeIndex) -> AdjacentSources { self.incoming_edges(target).sources() } @@ -274,8 +283,8 @@ pub fn predecessor_nodes(&self, target: NodeIndex) -> AdjacentSources { // variables or other bitsets. This method facilitates such a // computation. - pub fn iterate_until_fixed_point<'a, F>(&'a self, mut op: F) where - F: FnMut(usize, EdgeIndex, &'a Edge) -> bool, + pub fn iterate_until_fixed_point<'a, F>(&'a self, mut op: F) + where F: FnMut(usize, EdgeIndex, &'a Edge) -> bool { let mut iteration = 0; let mut changed = true; @@ -288,7 +297,7 @@ pub fn iterate_until_fixed_point<'a, F>(&'a self, mut op: F) where } } - pub fn depth_traverse<'a>(&'a self, start: NodeIndex) -> DepthFirstTraversal<'a, N, E> { + pub fn depth_traverse<'a>(&'a self, start: NodeIndex) -> DepthFirstTraversal<'a, N, E> { DepthFirstTraversal { graph: self, stack: vec![start], @@ -300,25 +309,26 @@ pub fn depth_traverse<'a>(&'a self, start: NodeIndex) -> DepthFirstTraversal<'a, /////////////////////////////////////////////////////////////////////////// // Iterators -pub struct AdjacentEdges<'g,N,E> - where N:'g, E:'g +pub struct AdjacentEdges<'g, N, E> + where N: 'g, + E: 'g { graph: &'g Graph, direction: Direction, next: EdgeIndex, } -impl<'g,N,E> AdjacentEdges<'g,N,E> { - fn targets(self) -> AdjacentTargets<'g,N,E> { +impl<'g, N, E> AdjacentEdges<'g, N, E> { + fn targets(self) -> AdjacentTargets<'g, N, E> { AdjacentTargets { edges: self } } - fn sources(self) -> AdjacentSources<'g,N,E> { + fn sources(self) -> AdjacentSources<'g, N, E> { AdjacentSources { edges: self } } } -impl<'g, N:Debug, E:Debug> Iterator for AdjacentEdges<'g, N, E> { +impl<'g, N: Debug, E: Debug> Iterator for AdjacentEdges<'g, N, E> { type Item = (EdgeIndex, &'g Edge); fn next(&mut self) -> Option<(EdgeIndex, &'g Edge)> { @@ -333,13 +343,14 @@ fn next(&mut self) -> Option<(EdgeIndex, &'g Edge)> { } } -pub struct AdjacentTargets<'g,N:'g,E:'g> - where N:'g, E:'g +pub struct AdjacentTargets<'g, N: 'g, E: 'g> + where N: 'g, + E: 'g { - edges: AdjacentEdges<'g,N,E>, + edges: AdjacentEdges<'g, N, E>, } -impl<'g, N:Debug, E:Debug> Iterator for AdjacentTargets<'g, N, E> { +impl<'g, N: Debug, E: Debug> Iterator for AdjacentTargets<'g, N, E> { type Item = NodeIndex; fn next(&mut self) -> Option { @@ -347,13 +358,14 @@ fn next(&mut self) -> Option { } } -pub struct AdjacentSources<'g,N:'g,E:'g> - where N:'g, E:'g +pub struct AdjacentSources<'g, N: 'g, E: 'g> + where N: 'g, + E: 'g { - edges: AdjacentEdges<'g,N,E>, + edges: AdjacentEdges<'g, N, E>, } -impl<'g, N:Debug, E:Debug> Iterator for AdjacentSources<'g, N, E> { +impl<'g, N: Debug, E: Debug> Iterator for AdjacentSources<'g, N, E> { type Item = NodeIndex; fn next(&mut self) -> Option { @@ -361,13 +373,13 @@ fn next(&mut self) -> Option { } } -pub struct DepthFirstTraversal<'g, N:'g, E:'g> { +pub struct DepthFirstTraversal<'g, N: 'g, E: 'g> { graph: &'g Graph, stack: Vec, - visited: BitVector + visited: BitVector, } -impl<'g, N:Debug, E:Debug> Iterator for DepthFirstTraversal<'g, N, E> { +impl<'g, N: Debug, E: Debug> Iterator for DepthFirstTraversal<'g, N, E> { type Item = NodeIndex; fn next(&mut self) -> Option { @@ -389,8 +401,8 @@ fn next(&mut self) -> Option { } } -pub fn each_edge_index(max_edge_index: EdgeIndex, mut f: F) where - F: FnMut(EdgeIndex) -> bool, +pub fn each_edge_index(max_edge_index: EdgeIndex, mut f: F) + where F: FnMut(EdgeIndex) -> bool { let mut i = 0; let n = max_edge_index.0; diff --git a/src/librustc_data_structures/graph/tests.rs b/src/librustc_data_structures/graph/tests.rs index 33b2edd2e10..be7f48d27e0 100644 --- a/src/librustc_data_structures/graph/tests.rs +++ b/src/librustc_data_structures/graph/tests.rs @@ -64,11 +64,11 @@ fn each_edge() { }); } -fn test_adjacent_edges(graph: &Graph, - start_index: NodeIndex, - start_data: N, - expected_incoming: &[(E,N)], - expected_outgoing: &[(E,N)]) { +fn test_adjacent_edges(graph: &Graph, + start_index: NodeIndex, + start_data: N, + expected_incoming: &[(E, N)], + expected_outgoing: &[(E, N)]) { assert!(graph.node_data(start_index) == &start_data); let mut counter = 0; @@ -76,7 +76,10 @@ fn test_adjacent_edges(graph: &Graph, assert!(graph.edge_data(edge_index) == &edge.data); assert!(counter < expected_incoming.len()); debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", - counter, expected_incoming[counter], edge_index, edge); + counter, + expected_incoming[counter], + edge_index, + edge); match expected_incoming[counter] { (ref e, ref n) => { assert!(e == &edge.data); @@ -93,7 +96,10 @@ fn test_adjacent_edges(graph: &Graph, assert!(graph.edge_data(edge_index) == &edge.data); assert!(counter < expected_outgoing.len()); debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}", - counter, expected_outgoing[counter], edge_index, edge); + counter, + expected_outgoing[counter], + edge_index, + edge); match expected_outgoing[counter] { (ref e, ref n) => { assert!(e == &edge.data); @@ -109,31 +115,27 @@ fn test_adjacent_edges(graph: &Graph, #[test] fn each_adjacent_from_a() { let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(0), "A", - &[], - &[("AB", "B")]); + test_adjacent_edges(&graph, NodeIndex(0), "A", &[], &[("AB", "B")]); } #[test] fn each_adjacent_from_b() { let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(1), "B", - &[("FB", "F"), ("AB", "A"),], - &[("BD", "D"), ("BC", "C"),]); + test_adjacent_edges(&graph, + NodeIndex(1), + "B", + &[("FB", "F"), ("AB", "A")], + &[("BD", "D"), ("BC", "C")]); } #[test] fn each_adjacent_from_c() { let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(2), "C", - &[("EC", "E"), ("BC", "B")], - &[]); + test_adjacent_edges(&graph, NodeIndex(2), "C", &[("EC", "E"), ("BC", "B")], &[]); } #[test] fn each_adjacent_from_d() { let graph = create_graph(); - test_adjacent_edges(&graph, NodeIndex(3), "D", - &[("BD", "B")], - &[("DE", "E")]); + test_adjacent_edges(&graph, NodeIndex(3), "D", &[("BD", "B")], &[("DE", "E")]); } diff --git a/src/librustc_data_structures/ivar.rs b/src/librustc_data_structures/ivar.rs index dabe1b984df..f842f4a41a1 100644 --- a/src/librustc_data_structures/ivar.rs +++ b/src/librustc_data_structures/ivar.rs @@ -26,14 +26,12 @@ /// suffices for the current purposes. #[derive(PartialEq)] pub struct Ivar { - data: Cell> + data: Cell>, } impl Ivar { pub fn new() -> Ivar { - Ivar { - data: Cell::new(None) - } + Ivar { data: Cell::new(None) } } pub fn get(&self) -> Option { @@ -41,8 +39,7 @@ pub fn get(&self) -> Option { } pub fn fulfill(&self, value: T) { - assert!(self.data.get().is_none(), - "Value already set!"); + assert!(self.data.get().is_none(), "Value already set!"); self.data.set(Some(value)); } @@ -55,11 +52,11 @@ pub fn unwrap(&self) -> T { } } -impl fmt::Debug for Ivar { +impl fmt::Debug for Ivar { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.get() { Some(val) => write!(f, "Ivar({:?})", val), - None => f.write_str("Ivar()") + None => f.write_str("Ivar()"), } } } @@ -68,7 +65,7 @@ impl Clone for Ivar { fn clone(&self) -> Ivar { match self.get() { Some(val) => Ivar { data: Cell::new(Some(val)) }, - None => Ivar::new() + None => Ivar::new(), } } } diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index e4b13ff548a..2234325aa01 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -32,7 +32,8 @@ #![cfg_attr(test, feature(test))] extern crate core; -#[macro_use] extern crate log; +#[macro_use] +extern crate log; extern crate serialize as rustc_serialize; // used by deriving pub mod bitvec; diff --git a/src/librustc_data_structures/obligation_forest/mod.rs b/src/librustc_data_structures/obligation_forest/mod.rs index 25a77adba28..4f6d0d7e405 100644 --- a/src/librustc_data_structures/obligation_forest/mod.rs +++ b/src/librustc_data_structures/obligation_forest/mod.rs @@ -28,7 +28,7 @@ #[cfg(test)] mod test; -pub struct ObligationForest { +pub struct ObligationForest { /// The list of obligations. In between calls to /// `process_obligations`, this list only contains nodes in the /// `Pending` or `Success` state (with a non-zero number of @@ -43,7 +43,7 @@ pub struct ObligationForest { /// backtrace iterator (which uses `split_at`). nodes: Vec>, trees: Vec>, - snapshots: Vec + snapshots: Vec, } pub struct Snapshot { @@ -67,7 +67,9 @@ struct Node { #[derive(Debug)] enum NodeState { /// Obligation not yet resolved to success or error. - Pending { obligation: O }, + Pending { + obligation: O, + }, /// Obligation resolved to success; `num_incomplete_children` /// indicates the number of children still in an "incomplete" @@ -77,7 +79,10 @@ enum NodeState { /// /// Once all children have completed, success nodes are removed /// from the vector by the compression step. - Success { obligation: O, num_incomplete_children: usize }, + Success { + obligation: O, + num_incomplete_children: usize, + }, /// This obligation was resolved to an error. Error nodes are /// removed from the vector by the compression step. @@ -85,13 +90,13 @@ enum NodeState { } #[derive(Debug)] -pub struct Outcome { +pub struct Outcome { /// Obligations that were completely evaluated, including all /// (transitive) subobligations. pub completed: Vec, /// Backtrace of obligations that were found to be in error. - pub errors: Vec>, + pub errors: Vec>, /// If true, then we saw no successful obligations, which means /// there is no point in further iteration. This is based on the @@ -103,7 +108,7 @@ pub struct Outcome { } #[derive(Debug, PartialEq, Eq)] -pub struct Error { +pub struct Error { pub error: E, pub backtrace: Vec, } @@ -113,7 +118,7 @@ pub fn new() -> ObligationForest { ObligationForest { trees: vec![], nodes: vec![], - snapshots: vec![] + snapshots: vec![], } } @@ -148,11 +153,12 @@ pub fn rollback_snapshot(&mut self, snapshot: Snapshot) { // snapshot but pushing trees, all nodes after that should be // roots of other trees as well let first_root_index = self.trees[trees_len].root.get(); - debug_assert!( - self.nodes[first_root_index..] - .iter() - .zip(first_root_index..) - .all(|(root, root_index)| self.trees[root.tree.get()].root.get() == root_index)); + debug_assert!(self.nodes[first_root_index..] + .iter() + .zip(first_root_index..) + .all(|(root, root_index)| { + self.trees[root.tree.get()].root.get() == root_index + })); // Pop off tree/root pairs pushed during snapshot. self.trees.truncate(trees_len); @@ -169,14 +175,17 @@ pub fn in_snapshot(&self) -> bool { pub fn push_tree(&mut self, obligation: O, tree_state: T) { let index = NodeIndex::new(self.nodes.len()); let tree = TreeIndex::new(self.trees.len()); - self.trees.push(Tree { root: index, state: tree_state }); + self.trees.push(Tree { + root: index, + state: tree_state, + }); self.nodes.push(Node::new(tree, None, obligation)); } /// Convert all remaining obligations to the given error. /// /// This cannot be done during a snapshot. - pub fn to_errors(&mut self, error: E) -> Vec> { + pub fn to_errors(&mut self, error: E) -> Vec> { assert!(!self.in_snapshot()); let mut errors = vec![]; for index in 0..self.nodes.len() { @@ -184,7 +193,10 @@ pub fn to_errors(&mut self, error: E) -> Vec> { self.inherit_error(index); if let NodeState::Pending { .. } = self.nodes[index].state { let backtrace = self.backtrace(index); - errors.push(Error { error: error.clone(), backtrace: backtrace }); + errors.push(Error { + error: error.clone(), + backtrace: backtrace, + }); } } let successful_obligations = self.compress(); @@ -193,21 +205,27 @@ pub fn to_errors(&mut self, error: E) -> Vec> { } /// Returns the set of obligations that are in a pending state. - pub fn pending_obligations(&self) -> Vec where O: Clone { - self.nodes.iter() - .filter_map(|n| match n.state { - NodeState::Pending { ref obligation } => Some(obligation), - _ => None, - }) - .cloned() - .collect() + pub fn pending_obligations(&self) -> Vec + where O: Clone + { + self.nodes + .iter() + .filter_map(|n| { + match n.state { + NodeState::Pending { ref obligation } => Some(obligation), + _ => None, + } + }) + .cloned() + .collect() } /// Process the obligations. /// /// This CANNOT be unrolled (presently, at least). - pub fn process_obligations(&mut self, mut action: F) -> Outcome - where E: Debug, F: FnMut(&mut O, &mut T, Backtrace) -> Result>, E> + pub fn process_obligations(&mut self, mut action: F) -> Outcome + where E: Debug, + F: FnMut(&mut O, &mut T, Backtrace) -> Result>, E> { debug!("process_obligations(len={})", self.nodes.len()); assert!(!self.in_snapshot()); // cannot unroll this action @@ -228,7 +246,8 @@ pub fn process_obligations(&mut self, mut action: F) -> Outcome self.inherit_error(index); debug!("process_obligations: node {} == {:?}", - index, self.nodes[index].state); + index, + self.nodes[index].state); let result = { let Node { tree, parent, .. } = self.nodes[index]; @@ -236,14 +255,16 @@ pub fn process_obligations(&mut self, mut action: F) -> Outcome let backtrace = Backtrace::new(prefix, parent); match suffix[0].state { NodeState::Error | - NodeState::Success { .. } => - continue, - NodeState::Pending { ref mut obligation } => - action(obligation, &mut self.trees[tree.get()].state, backtrace), + NodeState::Success { .. } => continue, + NodeState::Pending { ref mut obligation } => { + action(obligation, &mut self.trees[tree.get()].state, backtrace) + } } }; - debug!("process_obligations: node {} got result {:?}", index, result); + debug!("process_obligations: node {} got result {:?}", + index, + result); match result { Ok(None) => { @@ -256,7 +277,10 @@ pub fn process_obligations(&mut self, mut action: F) -> Outcome } Err(err) => { let backtrace = self.backtrace(index); - errors.push(Error { error: err, backtrace: backtrace }); + errors.push(Error { + error: err, + backtrace: backtrace, + }); } } } @@ -291,20 +315,21 @@ fn success(&mut self, index: usize, children: Vec) { // create child work let tree_index = self.nodes[index].tree; let node_index = NodeIndex::new(index); - self.nodes.extend( - children.into_iter() - .map(|o| Node::new(tree_index, Some(node_index), o))); + self.nodes.extend(children.into_iter() + .map(|o| Node::new(tree_index, Some(node_index), o))); } // change state from `Pending` to `Success`, temporarily swapping in `Error` let state = mem::replace(&mut self.nodes[index].state, NodeState::Error); self.nodes[index].state = match state { - NodeState::Pending { obligation } => - NodeState::Success { obligation: obligation, - num_incomplete_children: num_incomplete_children }, + NodeState::Pending { obligation } => { + NodeState::Success { + obligation: obligation, + num_incomplete_children: num_incomplete_children, + } + } NodeState::Success { .. } | - NodeState::Error => - unreachable!() + NodeState::Error => unreachable!(), }; } @@ -358,14 +383,19 @@ fn backtrace(&mut self, mut p: usize) -> Vec { // there was an error in the ancestors, it should // have been propagated down and we should never // have tried to process this obligation - panic!("encountered error in node {:?} when collecting stack trace", p); + panic!("encountered error in node {:?} when collecting stack trace", + p); } } // loop to the parent match self.nodes[p].parent { - Some(q) => { p = q.get(); } - None => { return trace; } + Some(q) => { + p = q.get(); + } + None => { + return trace; + } } } } @@ -427,18 +457,19 @@ fn compress(&mut self) -> Vec { // Pop off all the nodes we killed and extract the success // stories. - let successful = - (0 .. dead_nodes) - .map(|_| self.nodes.pop().unwrap()) - .flat_map(|node| match node.state { - NodeState::Error => None, - NodeState::Pending { .. } => unreachable!(), - NodeState::Success { obligation, num_incomplete_children } => { - assert_eq!(num_incomplete_children, 0); - Some(obligation) - } - }) - .collect(); + let successful = (0..dead_nodes) + .map(|_| self.nodes.pop().unwrap()) + .flat_map(|node| { + match node.state { + NodeState::Error => None, + NodeState::Pending { .. } => unreachable!(), + NodeState::Success { obligation, num_incomplete_children } => { + assert_eq!(num_incomplete_children, 0); + Some(obligation) + } + } + }) + .collect(); // Adjust the various indices, since we compressed things. for tree in &mut self.trees { @@ -484,7 +515,10 @@ pub struct Backtrace<'b, O: 'b> { impl<'b, O> Backtrace<'b, O> { fn new(nodes: &'b [Node], pointer: Option) -> Backtrace<'b, O> { - Backtrace { nodes: nodes, pointer: pointer } + Backtrace { + nodes: nodes, + pointer: pointer, + } } } @@ -497,9 +531,7 @@ fn next(&mut self) -> Option<&'b O> { self.pointer = self.nodes[p.get()].parent; match self.nodes[p.get()].state { NodeState::Pending { ref obligation } | - NodeState::Success { ref obligation, .. } => { - Some(obligation) - } + NodeState::Success { ref obligation, .. } => Some(obligation), NodeState::Error => { panic!("Backtrace encountered an error."); } diff --git a/src/librustc_data_structures/obligation_forest/node_index.rs b/src/librustc_data_structures/obligation_forest/node_index.rs index 465cee0b60c..1063bb3611e 100644 --- a/src/librustc_data_structures/obligation_forest/node_index.rs +++ b/src/librustc_data_structures/obligation_forest/node_index.rs @@ -13,19 +13,16 @@ #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct NodeIndex { - index: NonZero + index: NonZero, } impl NodeIndex { pub fn new(value: usize) -> NodeIndex { assert!(value < (u32::MAX as usize)); - unsafe { - NodeIndex { index: NonZero::new((value as u32) + 1) } - } + unsafe { NodeIndex { index: NonZero::new((value as u32) + 1) } } } pub fn get(self) -> usize { (*self.index - 1) as usize } } - diff --git a/src/librustc_data_structures/obligation_forest/test.rs b/src/librustc_data_structures/obligation_forest/test.rs index 9a0a4218d45..a8c24270217 100644 --- a/src/librustc_data_structures/obligation_forest/test.rs +++ b/src/librustc_data_structures/obligation_forest/test.rs @@ -21,19 +21,23 @@ fn push_pop() { // A |-> A.1 // |-> A.2 // |-> A.3 - let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations(|obligation, tree, _| { - assert_eq!(obligation.chars().next(), tree.chars().next()); - match *obligation { - "A" => Ok(Some(vec!["A.1", "A.2", "A.3"])), - "B" => Err("B is for broken"), - "C" => Ok(Some(vec![])), - _ => unreachable!(), - } - }); + let Outcome { completed: ok, errors: err, .. } = forest.process_obligations(|obligation, + tree, + _| { + assert_eq!(obligation.chars().next(), tree.chars().next()); + match *obligation { + "A" => Ok(Some(vec!["A.1", "A.2", "A.3"])), + "B" => Err("B is for broken"), + "C" => Ok(Some(vec![])), + _ => unreachable!(), + } + }); assert_eq!(ok, vec!["C"]); - assert_eq!(err, vec![Error {error: "B is for broken", - backtrace: vec!["B"]}]); + assert_eq!(err, + vec![Error { + error: "B is for broken", + backtrace: vec!["B"], + }]); // second round: two delays, one success, creating an uneven set of subtasks: // A |-> A.1 @@ -61,33 +65,41 @@ fn push_pop() { // propagates to A.3.i, but not D.1 or D.2. // D |-> D.1 |-> D.1.i // |-> D.2 |-> D.2.i - let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations(|obligation, tree, _| { - assert_eq!(obligation.chars().next(), tree.chars().next()); - match *obligation { - "A.1" => Ok(Some(vec![])), - "A.2" => Err("A is for apple"), - "D.1" => Ok(Some(vec!["D.1.i"])), - "D.2" => Ok(Some(vec!["D.2.i"])), - _ => unreachable!(), - } - }); + let Outcome { completed: ok, errors: err, .. } = forest.process_obligations(|obligation, + tree, + _| { + assert_eq!(obligation.chars().next(), tree.chars().next()); + match *obligation { + "A.1" => Ok(Some(vec![])), + "A.2" => Err("A is for apple"), + "D.1" => Ok(Some(vec!["D.1.i"])), + "D.2" => Ok(Some(vec!["D.2.i"])), + _ => unreachable!(), + } + }); assert_eq!(ok, vec!["A.1"]); - assert_eq!(err, vec![Error { error: "A is for apple", - backtrace: vec!["A.2", "A"] }]); + assert_eq!(err, + vec![Error { + error: "A is for apple", + backtrace: vec!["A.2", "A"], + }]); // fourth round: error in D.1.i that should propagate to D.2.i - let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations(|obligation, tree, _| { - assert_eq!(obligation.chars().next(), tree.chars().next()); - match *obligation { - "D.1.i" => Err("D is for dumb"), - _ => panic!("unexpected obligation {:?}", obligation), - } - }); + let Outcome { completed: ok, errors: err, .. } = forest.process_obligations(|obligation, + tree, + _| { + assert_eq!(obligation.chars().next(), tree.chars().next()); + match *obligation { + "D.1.i" => Err("D is for dumb"), + _ => panic!("unexpected obligation {:?}", obligation), + } + }); assert_eq!(ok, Vec::<&'static str>::new()); - assert_eq!(err, vec![Error { error: "D is for dumb", - backtrace: vec!["D.1.i", "D.1", "D"] }]); + assert_eq!(err, + vec![Error { + error: "D is for dumb", + backtrace: vec!["D.1.i", "D.1", "D"], + }]); } // Test that if a tree with grandchildren succeeds, everything is @@ -104,7 +116,7 @@ fn success_in_grandchildren() { forest.push_tree("A", "A"); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, _| { + forest.process_obligations::<(), _>(|obligation, tree, _| { assert_eq!(obligation.chars().next(), tree.chars().next()); match *obligation { "A" => Ok(Some(vec!["A.1", "A.2", "A.3"])), @@ -115,7 +127,7 @@ fn success_in_grandchildren() { assert!(err.is_empty()); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, _| { + forest.process_obligations::<(), _>(|obligation, tree, _| { assert_eq!(obligation.chars().next(), tree.chars().next()); match *obligation { "A.1" => Ok(Some(vec![])), @@ -128,7 +140,7 @@ fn success_in_grandchildren() { assert!(err.is_empty()); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, _| { + forest.process_obligations::<(), _>(|obligation, tree, _| { assert_eq!(obligation.chars().next(), tree.chars().next()); match *obligation { "A.2.i" => Ok(Some(vec!["A.2.i.a"])), @@ -140,7 +152,7 @@ fn success_in_grandchildren() { assert!(err.is_empty()); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, _| { + forest.process_obligations::<(), _>(|obligation, tree, _| { assert_eq!(obligation.chars().next(), tree.chars().next()); match *obligation { "A.2.i.a" => Ok(Some(vec![])), @@ -150,8 +162,11 @@ fn success_in_grandchildren() { assert_eq!(ok, vec!["A.2.i.a", "A.2.i", "A.2", "A"]); assert!(err.is_empty()); - let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|_, _, _| unreachable!()); + let Outcome { completed: ok, errors: err, .. } = forest.process_obligations::<(), _>(|_, + _, + _| { + unreachable!() + }); assert!(ok.is_empty()); assert!(err.is_empty()); } @@ -163,7 +178,7 @@ fn to_errors_no_throw() { let mut forest = ObligationForest::new(); forest.push_tree("A", "A"); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, _| { + forest.process_obligations::<(), _>(|obligation, tree, _| { assert_eq!(obligation.chars().next(), tree.chars().next()); match *obligation { "A" => Ok(Some(vec!["A.1", "A.2", "A.3"])), @@ -183,7 +198,7 @@ fn backtrace() { let mut forest = ObligationForest::new(); forest.push_tree("A", "A"); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, mut backtrace| { + forest.process_obligations::<(), _>(|obligation, tree, mut backtrace| { assert_eq!(obligation.chars().next(), tree.chars().next()); assert!(backtrace.next().is_none()); match *obligation { @@ -194,7 +209,7 @@ fn backtrace() { assert!(ok.is_empty()); assert!(err.is_empty()); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, mut backtrace| { + forest.process_obligations::<(), _>(|obligation, tree, mut backtrace| { assert_eq!(obligation.chars().next(), tree.chars().next()); assert!(backtrace.next().unwrap() == &"A"); assert!(backtrace.next().is_none()); @@ -206,7 +221,7 @@ fn backtrace() { assert!(ok.is_empty()); assert!(err.is_empty()); let Outcome { completed: ok, errors: err, .. } = - forest.process_obligations::<(),_>(|obligation, tree, mut backtrace| { + forest.process_obligations::<(), _>(|obligation, tree, mut backtrace| { assert_eq!(obligation.chars().next(), tree.chars().next()); assert!(backtrace.next().unwrap() == &"A.1"); assert!(backtrace.next().unwrap() == &"A"); diff --git a/src/librustc_data_structures/obligation_forest/tree_index.rs b/src/librustc_data_structures/obligation_forest/tree_index.rs index a9f5483f45b..499448634ac 100644 --- a/src/librustc_data_structures/obligation_forest/tree_index.rs +++ b/src/librustc_data_structures/obligation_forest/tree_index.rs @@ -12,7 +12,7 @@ #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct TreeIndex { - index: u32 + index: u32, } impl TreeIndex { @@ -25,4 +25,3 @@ pub fn get(self) -> usize { self.index as usize } } - diff --git a/src/librustc_data_structures/snapshot_vec.rs b/src/librustc_data_structures/snapshot_vec.rs index 5f89856afdb..614e7aae74b 100644 --- a/src/librustc_data_structures/snapshot_vec.rs +++ b/src/librustc_data_structures/snapshot_vec.rs @@ -23,7 +23,7 @@ use std::mem; use std::ops; -pub enum UndoLog { +pub enum UndoLog { /// Indicates where a snapshot started. OpenSnapshot, @@ -37,10 +37,10 @@ pub enum UndoLog { SetElem(usize, D::Value), /// Extensible set of actions - Other(D::Undo) + Other(D::Undo), } -pub struct SnapshotVec { +pub struct SnapshotVec { values: Vec, undo_log: Vec>, } @@ -58,7 +58,7 @@ pub trait SnapshotVecDelegate { fn reverse(values: &mut Vec, action: Self::Undo); } -impl SnapshotVec { +impl SnapshotVec { pub fn new() -> SnapshotVec { SnapshotVec { values: Vec::new(), @@ -117,9 +117,7 @@ pub fn start_snapshot(&mut self) -> Snapshot { Snapshot { length: length } } - pub fn actions_since_snapshot(&self, - snapshot: &Snapshot) - -> &[UndoLog] { + pub fn actions_since_snapshot(&self, snapshot: &Snapshot) -> &[UndoLog] { &self.undo_log[snapshot.length..] } @@ -128,11 +126,10 @@ fn assert_open_snapshot(&self, snapshot: &Snapshot) { assert!(self.undo_log.len() > snapshot.length); // Invariant established by start_snapshot(): - assert!( - match self.undo_log[snapshot.length] { - OpenSnapshot => true, - _ => false - }); + assert!(match self.undo_log[snapshot.length] { + OpenSnapshot => true, + _ => false, + }); } pub fn rollback_to(&mut self, snapshot: Snapshot) { @@ -168,7 +165,10 @@ pub fn rollback_to(&mut self, snapshot: Snapshot) { } let v = self.undo_log.pop().unwrap(); - assert!(match v { OpenSnapshot => true, _ => false }); + assert!(match v { + OpenSnapshot => true, + _ => false, + }); assert!(self.undo_log.len() == snapshot.length); } @@ -188,20 +188,28 @@ pub fn commit(&mut self, snapshot: Snapshot) { } } -impl ops::Deref for SnapshotVec { +impl ops::Deref for SnapshotVec { type Target = [D::Value]; - fn deref(&self) -> &[D::Value] { &*self.values } + fn deref(&self) -> &[D::Value] { + &*self.values + } } -impl ops::DerefMut for SnapshotVec { - fn deref_mut(&mut self) -> &mut [D::Value] { &mut *self.values } +impl ops::DerefMut for SnapshotVec { + fn deref_mut(&mut self) -> &mut [D::Value] { + &mut *self.values + } } -impl ops::Index for SnapshotVec { +impl ops::Index for SnapshotVec { type Output = D::Value; - fn index(&self, index: usize) -> &D::Value { self.get(index) } + fn index(&self, index: usize) -> &D::Value { + self.get(index) + } } -impl ops::IndexMut for SnapshotVec { - fn index_mut(&mut self, index: usize) -> &mut D::Value { self.get_mut(index) } +impl ops::IndexMut for SnapshotVec { + fn index_mut(&mut self, index: usize) -> &mut D::Value { + self.get_mut(index) + } } diff --git a/src/librustc_data_structures/transitive_relation.rs b/src/librustc_data_structures/transitive_relation.rs index 7ea5cb8721d..c3a2f978e1a 100644 --- a/src/librustc_data_structures/transitive_relation.rs +++ b/src/librustc_data_structures/transitive_relation.rs @@ -14,7 +14,7 @@ use std::mem; #[derive(Clone)] -pub struct TransitiveRelation { +pub struct TransitiveRelation { // List of elements. This is used to map from a T to a usize. We // expect domain to be small so just use a linear list versus a // hashmap or something. @@ -33,7 +33,7 @@ pub struct TransitiveRelation { // are added with new elements. Perhaps better would be to ask the // user for a batch of edges to minimize this effect, but I // already wrote the code this way. :P -nmatsakis - closure: RefCell> + closure: RefCell>, } #[derive(Clone, PartialEq, PartialOrd)] @@ -45,11 +45,13 @@ struct Edge { target: Index, } -impl TransitiveRelation { +impl TransitiveRelation { pub fn new() -> TransitiveRelation { - TransitiveRelation { elements: vec![], - edges: vec![], - closure: RefCell::new(None) } + TransitiveRelation { + elements: vec![], + edges: vec![], + closure: RefCell::new(None), + } } fn index(&self, a: &T) -> Option { @@ -74,7 +76,10 @@ fn add_index(&mut self, a: T) -> Index { pub fn add(&mut self, a: T, b: T) { let a = self.add_index(a); let b = self.add_index(b); - let edge = Edge { source: a, target: b }; + let edge = Edge { + source: a, + target: b, + }; if !self.edges.contains(&edge) { self.edges.push(edge); @@ -86,10 +91,8 @@ pub fn add(&mut self, a: T, b: T) { /// Check whether `a < target` (transitively) pub fn contains(&self, a: &T, b: &T) -> bool { match (self.index(a), self.index(b)) { - (Some(a), Some(b)) => - self.with_closure(|closure| closure.contains(a.0, b.0)), - (None, _) | (_, None) => - false, + (Some(a), Some(b)) => self.with_closure(|closure| closure.contains(a.0, b.0)), + (None, _) | (_, None) => false, } } @@ -156,7 +159,9 @@ pub fn postdom_upper_bound(&self, a: &T, b: &T) -> Option<&T> { pub fn minimal_upper_bounds(&self, a: &T, b: &T) -> Vec<&T> { let (mut a, mut b) = match (self.index(a), self.index(b)) { (Some(a), Some(b)) => (a, b), - (None, _) | (_, None) => { return vec![]; } + (None, _) | (_, None) => { + return vec![]; + } }; // in some cases, there are some arbitrary choices to be made; @@ -233,7 +238,7 @@ pub fn minimal_upper_bounds(&self, a: &T, b: &T) -> Vec<&T> { .collect() } - fn with_closure(&self, op: OP) -> R + fn with_closure(&self, op: OP) -> R where OP: FnOnce(&BitMatrix) -> R { let mut closure_cell = self.closure.borrow_mut(); @@ -431,14 +436,15 @@ fn pdub_crisscross() { // b -> b1 ---+ let mut relation = TransitiveRelation::new(); - relation.add("a", "a1"); - relation.add("a", "b1"); - relation.add("b", "a1"); - relation.add("b", "b1"); + relation.add("a", "a1"); + relation.add("a", "b1"); + relation.add("b", "a1"); + relation.add("b", "b1"); relation.add("a1", "x"); relation.add("b1", "x"); - assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"a1", &"b1"]); + assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), + vec![&"a1", &"b1"]); assert_eq!(relation.postdom_upper_bound(&"a", &"b"), Some(&"x")); } @@ -451,23 +457,25 @@ fn pdub_crisscross_more() { // b -> b1 -> b2 ---------+ let mut relation = TransitiveRelation::new(); - relation.add("a", "a1"); - relation.add("a", "b1"); - relation.add("b", "a1"); - relation.add("b", "b1"); + relation.add("a", "a1"); + relation.add("a", "b1"); + relation.add("b", "a1"); + relation.add("b", "b1"); - relation.add("a1", "a2"); - relation.add("a1", "b2"); - relation.add("b1", "a2"); - relation.add("b1", "b2"); + relation.add("a1", "a2"); + relation.add("a1", "b2"); + relation.add("b1", "a2"); + relation.add("b1", "b2"); relation.add("a2", "a3"); relation.add("a3", "x"); relation.add("b2", "x"); - assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"a1", &"b1"]); - assert_eq!(relation.minimal_upper_bounds(&"a1", &"b1"), vec![&"a2", &"b2"]); + assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), + vec![&"a1", &"b1"]); + assert_eq!(relation.minimal_upper_bounds(&"a1", &"b1"), + vec![&"a2", &"b2"]); assert_eq!(relation.postdom_upper_bound(&"a", &"b"), Some(&"x")); } @@ -479,8 +487,8 @@ fn pdub_lub() { // b -> b1 ---+ let mut relation = TransitiveRelation::new(); - relation.add("a", "a1"); - relation.add("b", "b1"); + relation.add("a", "a1"); + relation.add("b", "b1"); relation.add("a1", "x"); relation.add("b1", "x"); @@ -497,9 +505,9 @@ fn mubs_intermediate_node_on_one_side_only() { // "digraph { a -> c -> d; b -> d; }", let mut relation = TransitiveRelation::new(); - relation.add("a", "c"); - relation.add("c", "d"); - relation.add("b", "d"); + relation.add("a", "c"); + relation.add("c", "d"); + relation.add("b", "d"); assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"d"]); } @@ -516,11 +524,11 @@ fn mubs_scc_1() { // "digraph { a -> c -> d; d -> c; a -> d; b -> d; }", let mut relation = TransitiveRelation::new(); - relation.add("a", "c"); - relation.add("c", "d"); - relation.add("d", "c"); - relation.add("a", "d"); - relation.add("b", "d"); + relation.add("a", "c"); + relation.add("c", "d"); + relation.add("d", "c"); + relation.add("a", "d"); + relation.add("b", "d"); assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]); } @@ -536,11 +544,11 @@ fn mubs_scc_2() { // "digraph { a -> c -> d; d -> c; b -> d; b -> c; }", let mut relation = TransitiveRelation::new(); - relation.add("a", "c"); - relation.add("c", "d"); - relation.add("d", "c"); - relation.add("b", "d"); - relation.add("b", "c"); + relation.add("a", "c"); + relation.add("c", "d"); + relation.add("d", "c"); + relation.add("b", "d"); + relation.add("b", "c"); assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]); } @@ -556,12 +564,12 @@ fn mubs_scc_3() { // "digraph { a -> c -> d -> e -> c; b -> d; b -> e; }", let mut relation = TransitiveRelation::new(); - relation.add("a", "c"); - relation.add("c", "d"); - relation.add("d", "e"); - relation.add("e", "c"); - relation.add("b", "d"); - relation.add("b", "e"); + relation.add("a", "c"); + relation.add("c", "d"); + relation.add("d", "e"); + relation.add("e", "c"); + relation.add("b", "d"); + relation.add("b", "e"); assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]); } @@ -578,12 +586,12 @@ fn mubs_scc_4() { // "digraph { a -> c -> d -> e -> c; a -> d; b -> e; }" let mut relation = TransitiveRelation::new(); - relation.add("a", "c"); - relation.add("c", "d"); - relation.add("d", "e"); - relation.add("e", "c"); - relation.add("a", "d"); - relation.add("b", "e"); + relation.add("a", "c"); + relation.add("c", "d"); + relation.add("d", "e"); + relation.add("e", "c"); + relation.add("a", "d"); + relation.add("b", "e"); assert_eq!(relation.minimal_upper_bounds(&"a", &"b"), vec![&"c"]); } diff --git a/src/librustc_data_structures/tuple_slice.rs b/src/librustc_data_structures/tuple_slice.rs index f157d82eda1..9a90ab8c09d 100644 --- a/src/librustc_data_structures/tuple_slice.rs +++ b/src/librustc_data_structures/tuple_slice.rs @@ -36,13 +36,13 @@ fn as_mut_slice(&mut self) -> &mut [T] { } } -impl_tuple_slice!((T,T), 2); -impl_tuple_slice!((T,T,T), 3); -impl_tuple_slice!((T,T,T,T), 4); -impl_tuple_slice!((T,T,T,T,T), 5); -impl_tuple_slice!((T,T,T,T,T,T), 6); -impl_tuple_slice!((T,T,T,T,T,T,T), 7); -impl_tuple_slice!((T,T,T,T,T,T,T,T), 8); +impl_tuple_slice!((T, T), 2); +impl_tuple_slice!((T, T, T), 3); +impl_tuple_slice!((T, T, T, T), 4); +impl_tuple_slice!((T, T, T, T, T), 5); +impl_tuple_slice!((T, T, T, T, T, T), 6); +impl_tuple_slice!((T, T, T, T, T, T, T), 7); +impl_tuple_slice!((T, T, T, T, T, T, T, T), 8); #[test] fn test_sliced_tuples() { diff --git a/src/librustc_data_structures/unify/mod.rs b/src/librustc_data_structures/unify/mod.rs index c6da70eef75..fe7fa06c962 100644 --- a/src/librustc_data_structures/unify/mod.rs +++ b/src/librustc_data_structures/unify/mod.rs @@ -56,21 +56,21 @@ fn combine(&self, _other: &()) {} /// time of the algorithm under control. For more information, see /// . #[derive(PartialEq,Clone,Debug)] -pub struct VarValue { - parent: K, // if equal to self, this is a root +pub struct VarValue { + parent: K, // if equal to self, this is a root value: K::Value, // value assigned (only relevant to root) - rank: u32, // max depth (only relevant to root) + rank: u32, // max depth (only relevant to root) } /// Table of unification keys and their values. -pub struct UnificationTable { +pub struct UnificationTable { /// Indicates the current value of each key. values: sv::SnapshotVec>, } /// At any time, users may snapshot a unification table. The changes /// made during the snapshot may either be *committed* or *rolled back*. -pub struct Snapshot { +pub struct Snapshot { // Link snapshot to the key type `K` of the table. marker: marker::PhantomData, snapshot: sv::Snapshot, @@ -79,15 +79,17 @@ pub struct Snapshot { #[derive(Copy, Clone)] struct Delegate(PhantomData); -impl VarValue { +impl VarValue { fn new_var(key: K, value: K::Value) -> VarValue { VarValue::new(key, value, 0) } fn new(parent: K, value: K::Value, rank: u32) -> VarValue { - VarValue { parent: parent, // this is a root - value: value, - rank: rank } + VarValue { + parent: parent, // this is a root + value: value, + rank: rank, + } } fn redirect(self, to: K) -> VarValue { @@ -95,7 +97,11 @@ fn redirect(self, to: K) -> VarValue { } fn root(self, rank: u32, value: K::Value) -> VarValue { - VarValue { rank: rank, value: value, ..self } + VarValue { + rank: rank, + value: value, + ..self + } } /// Returns the key of this node. Only valid if this is a root @@ -122,18 +128,18 @@ fn if_not_self(&self, key: K, self_key: K) -> Option { // other type parameter U, and we have no way to say // Option:LatticeValue. -impl UnificationTable { +impl UnificationTable { pub fn new() -> UnificationTable { - UnificationTable { - values: sv::SnapshotVec::new() - } + UnificationTable { values: sv::SnapshotVec::new() } } /// Starts a new snapshot. Each snapshot must be either /// rolled back or committed in a "LIFO" (stack) order. pub fn snapshot(&mut self) -> Snapshot { - Snapshot { marker: marker::PhantomData::, - snapshot: self.values.start_snapshot() } + Snapshot { + marker: marker::PhantomData::, + snapshot: self.values.start_snapshot(), + } } /// Reverses all changes since the last snapshot. Also @@ -154,9 +160,7 @@ pub fn new_key(&mut self, value: K::Value) -> K { let len = self.values.len(); let key: K = UnifyKey::from_index(len as u32); self.values.push(VarValue::new_var(key, value)); - debug!("{}: created new key: {:?}", - UnifyKey::tag(None::), - key); + debug!("{}: created new key: {:?}", UnifyKey::tag(None::), key); key } @@ -179,9 +183,7 @@ fn get(&mut self, vid: K) -> VarValue { } root } - None => { - value - } + None => value, } } @@ -195,8 +197,7 @@ fn is_root(&self, key: K) -> bool { fn set(&mut self, key: K, new_value: VarValue) { assert!(self.is_root(key)); - debug!("Updating variable {:?} to {:?}", - key, new_value); + debug!("Updating variable {:?} to {:?}", key, new_value); let index = key.index() as usize; self.values.set(index, new_value); @@ -243,7 +244,7 @@ fn redirect_root(&mut self, } } -impl sv::SnapshotVecDelegate for Delegate { +impl sv::SnapshotVecDelegate for Delegate { type Value = VarValue; type Undo = (); @@ -253,7 +254,7 @@ fn reverse(_: &mut Vec>, _: ()) {} /////////////////////////////////////////////////////////////////////////// // Base union-find algorithm, where we are just making sets -impl<'tcx,K:UnifyKey> UnificationTable +impl<'tcx, K: UnifyKey> UnificationTable where K::Value: Combine { pub fn union(&mut self, a_id: K, b_id: K) { @@ -285,30 +286,24 @@ pub fn unioned(&mut self, a_id: K, b_id: K) -> bool { // floats---anything that doesn't have a subtyping relationship we // need to worry about. -impl<'tcx,K,V> UnificationTable - where K: UnifyKey>, - V: Clone+PartialEq+Debug, +impl<'tcx, K, V> UnificationTable + where K: UnifyKey>, + V: Clone + PartialEq + Debug { - pub fn unify_var_var(&mut self, - a_id: K, - b_id: K) - -> Result<(),(V,V)> - { + pub fn unify_var_var(&mut self, a_id: K, b_id: K) -> Result<(), (V, V)> { let node_a = self.get(a_id); let node_b = self.get(b_id); let a_id = node_a.key(); let b_id = node_b.key(); - if a_id == b_id { return Ok(()); } + if a_id == b_id { + return Ok(()); + } let combined = { match (&node_a.value, &node_b.value) { - (&None, &None) => { - None - } - (&Some(ref v), &None) | (&None, &Some(ref v)) => { - Some(v.clone()) - } + (&None, &None) => None, + (&Some(ref v), &None) | (&None, &Some(ref v)) => Some(v.clone()), (&Some(ref v1), &Some(ref v2)) => { if *v1 != *v2 { return Err((v1.clone(), v2.clone())); @@ -323,11 +318,7 @@ pub fn unify_var_var(&mut self, /// Sets the value of the key `a_id` to `b`. Because simple keys do not have any subtyping /// relationships, if `a_id` already has a value, it must be the same as `b`. - pub fn unify_var_value(&mut self, - a_id: K, - b: V) - -> Result<(),(V,V)> - { + pub fn unify_var_value(&mut self, a_id: K, b: V) -> Result<(), (V, V)> { let mut node_a = self.get(a_id); match node_a.value { @@ -358,7 +349,13 @@ pub fn probe(&mut self, a_id: K) -> Option { pub fn unsolved_variables(&mut self) -> Vec { self.values .iter() - .filter_map(|vv| if vv.value.is_some() { None } else { Some(vv.key()) }) + .filter_map(|vv| { + if vv.value.is_some() { + None + } else { + Some(vv.key()) + } + }) .collect() } } diff --git a/src/librustc_data_structures/unify/tests.rs b/src/librustc_data_structures/unify/tests.rs index 089e629a569..f29a7132e83 100644 --- a/src/librustc_data_structures/unify/tests.rs +++ b/src/librustc_data_structures/unify/tests.rs @@ -19,9 +19,15 @@ impl UnifyKey for UnitKey { type Value = (); - fn index(&self) -> u32 { self.0 } - fn from_index(u: u32) -> UnitKey { UnitKey(u) } - fn tag(_: Option) -> &'static str { "UnitKey" } + fn index(&self) -> u32 { + self.0 + } + fn from_index(u: u32) -> UnitKey { + UnitKey(u) + } + fn tag(_: Option) -> &'static str { + "UnitKey" + } } #[test] @@ -45,7 +51,7 @@ fn big_array() { } for i in 1..MAX { - let l = keys[i-1]; + let l = keys[i - 1]; let r = keys[i]; ut.union(l, r); } @@ -68,7 +74,7 @@ fn big_array_bench(b: &mut Bencher) { b.iter(|| { for i in 1..MAX { - let l = keys[i-1]; + let l = keys[i - 1]; let r = keys[i]; ut.union(l, r); } @@ -90,16 +96,16 @@ fn even_odd() { keys.push(key); if i >= 2 { - ut.union(key, keys[i-2]); + ut.union(key, keys[i - 2]); } } for i in 1..MAX { - assert!(!ut.unioned(keys[i-1], keys[i])); + assert!(!ut.unioned(keys[i - 1], keys[i])); } for i in 2..MAX { - assert!(ut.unioned(keys[i-2], keys[i])); + assert!(ut.unioned(keys[i - 2], keys[i])); } } @@ -108,9 +114,15 @@ fn even_odd() { impl UnifyKey for IntKey { type Value = Option; - fn index(&self) -> u32 { self.0 } - fn from_index(u: u32) -> IntKey { IntKey(u) } - fn tag(_: Option) -> &'static str { "IntKey" } + fn index(&self) -> u32 { + self.0 + } + fn from_index(u: u32) -> IntKey { + IntKey(u) + } + fn tag(_: Option) -> &'static str { + "IntKey" + } } /// Test unifying a key whose value is `Some(_)` with a key whose value is `None`. @@ -191,4 +203,3 @@ fn unify_key_Some_x_val_x() { assert!(ut.unify_var_value(k1, 22).is_ok()); assert_eq!(ut.probe(k1), Some(22)); } - diff --git a/src/librustc_data_structures/veccell/mod.rs b/src/librustc_data_structures/veccell/mod.rs index 008642d9d65..054eee8829a 100644 --- a/src/librustc_data_structures/veccell/mod.rs +++ b/src/librustc_data_structures/veccell/mod.rs @@ -12,11 +12,11 @@ use std::mem; pub struct VecCell { - data: UnsafeCell> + data: UnsafeCell>, } impl VecCell { - pub fn with_capacity(capacity: usize) -> VecCell{ + pub fn with_capacity(capacity: usize) -> VecCell { VecCell { data: UnsafeCell::new(Vec::with_capacity(capacity)) } } -- 2.44.0