Skip to content

Commit 936df35

Browse files
committed
refactor shared visitors
1 parent 556d668 commit 936df35

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

chalk-ir/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::cast::{Cast, CastTo};
44
use crate::fold::shift::Shift;
55
use crate::fold::{Fold, Folder, Subst, SuperFold};
6-
use crate::visit::{visitors::FindBound, SuperVisit, Visit, VisitResult, Visitor};
6+
use crate::visit::{SuperVisit, Visit, VisitExt, VisitResult, Visitor};
77
use chalk_derive::{Fold, HasInterner, Visit};
88
use chalk_engine::fallible::*;
99
use std::iter;
@@ -237,8 +237,7 @@ impl<I: Interner> Ty<I> {
237237
/// needs to be shifted across binders. This is a very inefficient
238238
/// check, intended only for debug assertions, because I am lazy.
239239
pub fn needs_shift(&self, interner: &I) -> bool {
240-
self.visit_with(&mut FindBound { interner }, DebruijnIndex::INNERMOST)
241-
.found
240+
self.has_free_vars(interner)
242241
}
243242
}
244243

chalk-ir/src/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ mod binder_impls;
1010
mod boring_impls;
1111
pub mod visitors;
1212

13+
pub use visitors::VisitExt;
14+
1315
pub trait VisitResult: Sized {
1416
fn new() -> Self;
1517

chalk-ir/src/visit/visitors.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
use crate::{BoundVar, DebruijnIndex, Interner, VisitResult, Visitor};
1+
use crate::{BoundVar, DebruijnIndex, Interner, Visit, VisitResult, Visitor};
2+
3+
pub trait VisitExt<I: Interner>: Visit<I> {
4+
fn has_free_vars(&self, interner: &I) -> bool {
5+
self.visit_with(
6+
&mut FindFreeVarsVisitor { interner },
7+
DebruijnIndex::INNERMOST,
8+
)
9+
.to_bool()
10+
}
11+
}
12+
13+
impl<T, I: Interner> VisitExt<I> for T where T: Visit<I> {}
214

315
#[derive(Clone, Copy, Debug)]
416
pub struct FindAny {
517
pub found: bool,
618
}
719

20+
impl FindAny {
21+
pub fn to_bool(&self) -> bool {
22+
self.found
23+
}
24+
}
25+
826
impl VisitResult for FindAny {
927
fn new() -> Self {
1028
FindAny { found: false }
@@ -18,11 +36,11 @@ impl VisitResult for FindAny {
1836
}
1937
}
2038

21-
pub struct FindBound<'i, I: Interner> {
39+
pub struct FindFreeVarsVisitor<'i, I: Interner> {
2240
pub interner: &'i I,
2341
}
2442

25-
impl<'i, I: Interner> Visitor<'i, I> for FindBound<'i, I> {
43+
impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
2644
type Result = FindAny;
2745

2846
fn as_dyn(&mut self) -> &mut dyn Visitor<'i, I, Result = Self::Result> {

0 commit comments

Comments
 (0)