Skip to content

Commit 88a6bbb

Browse files
committed
more nits
1 parent f35edae commit 88a6bbb

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

chalk-ir/src/visit.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,32 @@ pub mod visitors;
1212

1313
pub use visitors::VisitExt;
1414

15+
/// A "result type" that can be returned from a visitor. Visitors pick
16+
/// an appropriate result type depending on what sort of operation they
17+
/// are doing. A common choice is `FindAny`, which indicates that the visitor
18+
/// is searching for something and that the visitor should stop once it is found.
1519
pub trait VisitResult: Sized {
1620
fn new() -> Self;
1721

22+
/// Returns true if this result is "complete" and we can stop visiting any
23+
/// further parts of the term. This is used by `FindAny`, for example, to
24+
/// stop the search after a match has been found.
1825
fn return_early(&self) -> bool;
1926
fn combine(self, other: Self) -> Self;
2027

28+
/// Convenience helper for use in writing `Visitor` impls. Returns `self`
29+
/// if `return_early()` is true, but otherwise combines `self` with the
30+
/// result of invoking `op`.
31+
///
32+
/// If you have a struct with two fields, `foo` and `bar`, you can
33+
/// thus write code like
34+
///
35+
/// ```rust,ignore
36+
/// self.foo.visit_with(visitor, outer_binder)
37+
/// .and_then(|| self.bar.visit_with(visitor, outer_binder))
38+
/// ```
39+
///
40+
/// and `bar` will only be visited if necessary.
2141
fn and_then(self, op: impl FnOnce() -> Self) -> Self {
2242
if self.return_early() {
2343
self
@@ -27,6 +47,8 @@ pub trait VisitResult: Sized {
2747
}
2848
}
2949

50+
/// Unit type for a visitor indicates a "side-effecting" visitor that
51+
/// should visit an entire term.
3052
impl VisitResult for () {
3153
fn new() -> () {}
3254

chalk-ir/src/visit/boring_impls.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
77
use crate::{
88
AssocTypeId, DebruijnIndex, Goals, ImplId, Interner, Parameter, ParameterKind,
9-
PlaceholderIndex, ProgramClause, ProgramClauseData, ProgramClauses, QuantifierKind, StructId,
10-
Substitution, SuperVisit, TraitId, UniverseIndex, Visit, VisitResult, Visitor,
9+
PlaceholderIndex, ProgramClause, ProgramClauseData, ProgramClauses, QuantifiedWhereClauses,
10+
QuantifierKind, StructId, Substitution, SuperVisit, TraitId, UniverseIndex, Visit, VisitResult,
11+
Visitor,
1112
};
1213
use chalk_engine::{context::Context, ExClause, FlounderedSubgoal, Literal};
1314
use std::{marker::PhantomData, sync::Arc};
1415

15-
pub fn visit_iter<'i, T, I, IT, R>(
16-
it: IT,
16+
/// Convenience function to visit all the items in the iterator it.
17+
pub fn visit_iter<'i, T, I, R>(
18+
it: impl Iterator<Item = T>,
1719
visitor: &mut dyn Visitor<'i, I, Result = R>,
1820
outer_binder: DebruijnIndex,
1921
) -> R
2022
where
2123
T: Visit<I>,
2224
I: 'i + Interner,
23-
IT: Iterator<Item = T>,
2425
R: VisitResult,
2526
{
2627
let mut result = R::new();
@@ -262,6 +263,21 @@ impl<I: Interner> Visit<I> for ProgramClauses<I> {
262263
}
263264
}
264265

266+
impl<I: Interner> Visit<I> for QuantifiedWhereClauses<I> {
267+
fn visit_with<'i, R: VisitResult>(
268+
&self,
269+
visitor: &mut dyn Visitor<'i, I, Result = R>,
270+
outer_binder: DebruijnIndex,
271+
) -> R
272+
where
273+
I: 'i,
274+
{
275+
let interner = visitor.interner();
276+
277+
visit_iter(self.iter(interner), visitor, outer_binder)
278+
}
279+
}
280+
265281
impl<I: Interner> Visit<I> for PhantomData<I> {
266282
fn visit_with<'i, R: VisitResult>(
267283
&self,

chalk-ir/src/visit/visitors.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ pub trait VisitExt<I: Interner>: Visit<I> {
1212

1313
impl<T, I: Interner> VisitExt<I> for T where T: Visit<I> {}
1414

15-
#[derive(Clone, Copy, Debug)]
15+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1616
pub struct FindAny {
1717
pub found: bool,
1818
}
1919

2020
impl FindAny {
21+
pub const FOUND: FindAny = FindAny { found: true };
22+
2123
pub fn to_bool(&self) -> bool {
2224
self.found
2325
}
@@ -38,8 +40,8 @@ impl VisitResult for FindAny {
3840
}
3941
}
4042

41-
pub struct FindFreeVarsVisitor<'i, I: Interner> {
42-
pub interner: &'i I,
43+
struct FindFreeVarsVisitor<'i, I: Interner> {
44+
interner: &'i I,
4345
}
4446

4547
impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
@@ -58,14 +60,14 @@ impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
5860
_bound_var: BoundVar,
5961
_outer_binder: DebruijnIndex,
6062
) -> Self::Result {
61-
FindAny { found: true }
63+
FindAny::FOUND
6264
}
6365

6466
fn visit_free_var_lifetime(
6567
&mut self,
6668
_bound_var: BoundVar,
6769
_outer_binder: DebruijnIndex,
6870
) -> Self::Result {
69-
FindAny { found: true }
71+
FindAny::FOUND
7072
}
7173
}

chalk-solve/src/wf.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ where
177177
let types =
178178
InputTypeCollector::types_in(gb.interner(), (&fields, &where_clauses));
179179

180-
gb.all(types.into_iter().map(|ty| ty.well_formed()).chain(sized_constraint_goal.into_iter()))
180+
gb.all(
181+
types
182+
.into_iter()
183+
.map(|ty| ty.well_formed().cast(interner))
184+
.chain(sized_constraint_goal.into_iter()),
185+
)
181186
},
182187
)
183188
});

0 commit comments

Comments
 (0)