From ee4bf21942fbc8a8df9c03d492f3be3c0a128d9f Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Thu, 19 Dec 2019 22:44:37 -0500 Subject: [PATCH] Add get_without_compacting for debug_assert --- src/ena/unify/mod.rs | 38 +++++++++++++++++++++++++++----------- src/solve.rs | 2 +- src/subs.rs | 4 ++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/ena/unify/mod.rs b/src/ena/unify/mod.rs index a45f1984ff..25468e60d2 100644 --- a/src/ena/unify/mod.rs +++ b/src/ena/unify/mod.rs @@ -271,20 +271,25 @@ impl UnificationTable { /// NB. This is a building-block operation and you would probably /// prefer to call `probe` below. pub fn get_root_key(&mut self, vid: S::Key) -> S::Key { - let redirect = { - match self.value(vid).parent(vid) { - None => return vid, - Some(redirect) => redirect, + match self.value(vid).parent(vid) { + None => vid, + Some(redirect) => { + let root_key: S::Key = self.get_root_key(redirect); + if root_key != redirect { + // Path compression + self.update_value(vid, |value| value.parent = root_key); + } + + root_key } - }; - - let root_key: S::Key = self.get_root_key(redirect); - if root_key != redirect { - // Path compression - self.update_value(vid, |value| value.parent = root_key); } + } - root_key + pub fn get_root_key_without_compacting(&self, vid: S::Key) -> S::Key { + match self.value(vid).parent(vid) { + None => vid, + Some(redirect) => self.get_root_key_without_compacting(redirect), + } } pub fn is_redirect(&mut self, vid: S::Key) -> bool { @@ -408,4 +413,15 @@ where let id = self.get_root_key(id); self.value(id).value.clone() } + + /// This is for a debug_assert! in solve() only. Do not use it elsewhere! + pub fn probe_value_without_compacting(&self, id: K1) -> V + where + K1: Into, + { + let id = id.into(); + let id = self.get_root_key_without_compacting(id); + + self.value(id).value.clone() + } } diff --git a/src/solve.rs b/src/solve.rs index 7a1d2642c0..9a0f2bf572 100644 --- a/src/solve.rs +++ b/src/solve.rs @@ -229,7 +229,7 @@ fn solve( // check that things went well debug_assert!(rigid_vars .iter() - .all(|&var| subs.get(var).rank == Rank::none())); + .all(|&var| subs.get_without_compacting(var).rank == Rank::none())); let mut new_vars_by_symbol = vars_by_symbol.clone(); diff --git a/src/subs.rs b/src/subs.rs index b9f98e8eec..afdad19f74 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -159,6 +159,10 @@ impl Subs { self.utable.probe_value(key) } + pub fn get_without_compacting(&self, key: Variable) -> Descriptor { + self.utable.probe_value_without_compacting(key) + } + pub fn get_root_key(&mut self, key: Variable) -> Variable { self.utable.get_root_key(key) }