diff --git a/Cargo.lock b/Cargo.lock index 3798d4f636..72e898e96b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3474,7 +3474,6 @@ dependencies = [ "roc_types", "target-lexicon", "tempfile", - "ven_graph", ] [[package]] diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml index b852187bf2..acde3f948e 100644 --- a/bindgen/Cargo.toml +++ b/bindgen/Cargo.toml @@ -26,7 +26,6 @@ roc_collections = { path = "../compiler/collections" } roc_target = { path = "../compiler/roc_target" } roc_error_macros = { path = "../error_macros" } bumpalo = { version = "3.8.0", features = ["collections"] } -ven_graph = { path = "../vendor/pathfinding" } target-lexicon = "0.12.3" clap = { version = "3.1.15", default-features = false, features = ["std", "color", "suggestions", "derive"] } diff --git a/bindgen/src/types.rs b/bindgen/src/types.rs index c814c18072..c3b9a00e7c 100644 --- a/bindgen/src/types.rs +++ b/bindgen/src/types.rs @@ -5,7 +5,6 @@ use roc_mono::layout::UnionLayout; use roc_std::RocDec; use roc_target::TargetInfo; use std::convert::TryInto; -use ven_graph::topological_sort; #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct TypeId(usize); @@ -52,16 +51,28 @@ impl Types { } pub fn sorted_ids(&self) -> Vec { - // TODO: instead use the bitvec matrix type we use in the Roc compiler - - // it's more efficient and also would bring us one step closer to dropping - // the dependency on this topological_sort implementation! - topological_sort(self.ids(), |id| match self.deps.get(id) { - Some(dep_ids) => dep_ids.to_vec(), - None => Vec::new(), - }) - .unwrap_or_else(|err| { - unreachable!("Cyclic type definitions: {:?}", err); - }) + use roc_collections::{ReferenceMatrix, TopologicalSort}; + + let mut matrix = ReferenceMatrix::new(self.by_id.len()); + + for type_id in self.ids() { + for dep in self.deps.get(&type_id).iter().flat_map(|x| x.iter()) { + matrix.set_row_col(type_id.0, dep.0, true); + } + } + + match matrix.topological_sort_into_groups() { + TopologicalSort::Groups { groups } => groups + .into_iter() + .flatten() + .rev() + .map(|n| TypeId(n as usize)) + .collect(), + TopologicalSort::HasCycles { + groups: _, + nodes_in_cycle, + } => unreachable!("Cyclic type definitions: {:?}", nodes_in_cycle), + } } pub fn iter(&self) -> impl ExactSizeIterator { diff --git a/compiler/collections/src/lib.rs b/compiler/collections/src/lib.rs index b41a0ad0a0..9bce46c2f2 100644 --- a/compiler/collections/src/lib.rs +++ b/compiler/collections/src/lib.rs @@ -10,7 +10,7 @@ mod vec_map; mod vec_set; pub use all::{default_hasher, BumpMap, ImEntry, ImMap, ImSet, MutMap, MutSet, SendMap}; -pub use reference_matrix::{ReferenceMatrix, Sccs}; +pub use reference_matrix::{ReferenceMatrix, Sccs, TopologicalSort}; pub use small_string_interner::SmallStringInterner; pub use vec_map::VecMap; pub use vec_set::VecSet; diff --git a/compiler/collections/src/reference_matrix.rs b/compiler/collections/src/reference_matrix.rs index 8ef1981df5..b74e361bc9 100644 --- a/compiler/collections/src/reference_matrix.rs +++ b/compiler/collections/src/reference_matrix.rs @@ -154,7 +154,7 @@ impl ReferenceMatrix { } } -#[allow(dead_code)] +#[derive(Debug)] pub enum TopologicalSort { /// There were no cycles, all nodes have been partitioned into groups Groups { groups: Vec> },