Skip to content

Commit ebdb87c

Browse files
committed
Auto merge of #767 - eggyal:rename-traits, r=jackh726
Rename folding/visiting traits Align the names of chalk's folding/visiting traits with those in rustc (especially once rust-lang/rust#98206) is merged. r? `@jackh726`
2 parents 48b1f1c + 23d39c9 commit ebdb87c

40 files changed

+419
-409
lines changed

book/src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [Application types](./types/rust_types/application_ty.md)
1313
- [Rust lifetimes](./types/rust_lifetimes.md)
1414
- [Operations](./types/operations.md)
15-
- [Fold and the Folder trait](./types/operations/fold.md)
15+
- [TypeFoldable and the TypeFolder trait](./types/operations/fold.md)
1616
- [Lowering Rust IR to logic](./clauses.md)
1717
- [Goals and clauses](./clauses/goals_and_clauses.md)
1818
- [Type equality and unification](./clauses/type_equality.md)

book/src/types/operations/fold.md

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
# Fold and the Folder trait
1+
# TypeFoldable and the TypeFolder trait
22

3-
The [`Fold`] trait permits one to traverse a type or other term in the
3+
The [`TypeFoldable`] trait permits one to traverse a type or other term in the
44
chalk-ir and make a copy of it, possibly making small substitutions or
55
alterations along the way. Folding also allows copying a term from one
66
interner to another.
77

8-
[`Fold`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.Fold.html
8+
[`TypeFoldable`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.TypeFoldable.html
99

10-
To use the [`Fold`] trait, one invokes the [`Fold::fold_with`] method, supplying some
10+
To use the [`TypeFoldable`] trait, one invokes the [`TypeFoldable::fold_with`] method, supplying some
1111
"folder" as well as the number of "in scope binders" for that term (typically `0`
1212
to start):
1313

1414
```rust,ignore
1515
let output_ty = input_ty.fold_with(&mut folder, 0);
1616
```
1717

18-
[`Fold::fold_with`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.Fold.html#tymethod.fold_with
18+
[`TypeFoldable::fold_with`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.TypeFoldable.html#tymethod.fold_with
1919

20-
The folder is some instance of the [`Folder`] trait. This trait
20+
The folder is some instance of the [`TypeFolder`] trait. This trait
2121
defines a few key callbacks that allow you to substitute different
2222
values as the fold proceeds. For example, when a type is folded, the
2323
folder can substitute a new type in its place.
2424

25-
[`Folder`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.Folder.html
25+
[`TypeFolder`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.TypeFolder.html
2626

2727
## Uses for folders
2828

29-
A common use for `Fold` is to permit a substitution -- that is,
29+
A common use for `TypeFoldable` is to permit a substitution -- that is,
3030
replacing generic type parameters with their values.
3131

32-
## From Fold to Folder to SuperFold and back again
32+
## From TypeFoldable to TypeFolder to TypeSuperFoldable and back again
3333

3434
The overall flow of folding is like this.
3535

36-
1. [`Fold::fold_with`] is invoked on the outermost term. It recursively
36+
1. [`TypeFoldable::fold_with`] is invoked on the outermost term. It recursively
3737
walks the term.
3838
2. For those sorts of terms (types, lifetimes, goals, program clauses) that have
39-
callbacks in the [`Folder`] trait, invoking [`Fold::fold_with`] will in turn
40-
invoke the corresponding method on the [`Folder`] trait, such as `Folder::fold_ty`.
41-
3. The default implementation of `Folder::fold_ty`, in turn, invokes
42-
`SuperFold::super_fold_with`. This will recursively fold the
39+
callbacks in the [`TypeFolder`] trait, invoking [`TypeFoldable::fold_with`] will in turn
40+
invoke the corresponding method on the [`TypeFolder`] trait, such as `TypeFolder::fold_ty`.
41+
3. The default implementation of `TypeFolder::fold_ty`, in turn, invokes
42+
`TypeSuperFoldable::super_fold_with`. This will recursively fold the
4343
contents of the type. In some cases, the `super_fold_with`
44-
implementation invokes more specialized methods on [`Folder`], such
45-
as [`Folder::fold_free_var_ty`], which makes it easier to write
44+
implementation invokes more specialized methods on [`TypeFolder`], such
45+
as [`TypeFolder::fold_free_var_ty`], which makes it easier to write
4646
folders that just intercept *certain* types.
4747

48-
[`Folder::fold_free_var_ty`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.Folder.html#method.fold_free_var_ty
48+
[`TypeFolder::fold_free_var_ty`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.TypeFolder.html#method.fold_free_var_ty
4949

5050
Thus, as a user, you can customize folding by:
5151

52-
* Defining your own `Folder` type
52+
* Defining your own `TypeFolder` type
5353
* Implementing the appropriate methods to "intercept" types/lifetimes/etc at the right level of
5454
detail
5555
* In those methods, if you find a case where you would prefer not to
56-
substitute a new value, then invoke `SuperFold::super_fold_with` to
56+
substitute a new value, then invoke `TypeSuperFoldable::super_fold_with` to
5757
return to the default behavior.
5858

5959
## The `binders` argument
6060

61-
Each callback in the [`Folder`] trait takes a `binders` argument. This indicates
61+
Each callback in the [`TypeFolder`] trait takes a `binders` argument. This indicates
6262
the number of binders that we have traversed during folding, which is relevant for De Bruijn indices.
6363
So e.g. a bound variable with depth 1, if invoked with a `binders` value of 1, indicates something that was bound to something external to the fold.
6464

@@ -70,32 +70,32 @@ Foo<'a>: for<'b> Bar<'b>
7070

7171
In this case, `Foo<'a>` gets visited with depth 0 and `Bar<'b>` gets visited with depth 1.
7272

73-
## The `Fold::Result` associated type
73+
## The `TypeFoldable::Result` associated type
7474

75-
The `Fold` trait defines a [`Result`] associated type, indicating the
75+
The `TypeFoldable` trait defines a [`Result`] associated type, indicating the
7676
type that will result from folding.
7777

78-
[`Result`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.Fold.html#associatedtype.Result
78+
[`Result`]: https://rust-lang.github.io/chalk/chalk_ir/fold/trait.TypeFoldable.html#associatedtype.Result
7979

80-
## When to implement the Fold and SuperFold traits
80+
## When to implement the TypeFoldable and TypeSuperFoldable traits
8181

8282
Any piece of IR that represents a kind of "term" (e.g., a type, part
83-
of a type, or a goal, etc) in the logic should implement `Fold`. We
84-
also implement `Fold` for common collection types like `Vec` as well
83+
of a type, or a goal, etc) in the logic should implement `TypeFoldable`. We
84+
also implement `TypeFoldable` for common collection types like `Vec` as well
8585
as tuples, references, etc.
8686

87-
The `SuperFold` trait should only be implemented for those types that
88-
have a callback defined on the `Folder` trait (e.g., types and
87+
The `TypeSuperFoldable` trait should only be implemented for those types that
88+
have a callback defined on the `TypeFolder` trait (e.g., types and
8989
lifetimes).
9090

9191
## Derives
9292

93-
Using the `chalk-derive` crate, you can auto-derive the `Fold` trait.
94-
There isn't presently a derive for `SuperFold` since it is very rare
95-
to require it. The derive for `Fold` is a bit cludgy and requires:
93+
Using the `chalk-derive` crate, you can auto-derive the `TypeFoldable` trait.
94+
There isn't presently a derive for `TypeSuperFoldable` since it is very rare
95+
to require it. The derive for `TypeFoldable` is a bit cludgy and requires:
9696

97-
* You must import `Fold` into scope.
98-
* The type you are deriving `Fold` on must have either:
97+
* You must import `TypeFoldable` into scope.
98+
* The type you are deriving `TypeFoldable` on must have either:
9999
* A type parameter that has a `Interner` bound, like `I: Interner`
100100
* A type parameter that has a `HasInterner` bound, like `I: HasInterner`
101101
* The `has_interner(XXX)` attribute.

book/src/what_is_chalk/crates.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The following crate is an implementation detail, used internally by `chalk-solve
1919

2020
* The `chalk-engine` crate, which defines the actual engine that solves logical predicate. This
2121
engine is quite general and not really specific to Rust.
22-
* The `chalk-derive` crate defines custom derives for the `chalk_ir::fold::Fold` trait and other
22+
* The `chalk-derive` crate defines custom derives for the `chalk_ir::fold::TypeFoldable` trait and other
2323
such things.
2424

2525
## Crates for standalone REPL and testing
@@ -37,11 +37,11 @@ define a kind of "minimal embedding" of chalk.
3737

3838
## The chalk-solve crate
3939

40-
| The `chalk-solve` crate | |
41-
|---|--- |
42-
| Purpose: | to solve a given goal |
43-
| Depends on IR: | chalk-ir and rust-ir |
44-
| Context required: | `RustIrDatabase` |
40+
| The `chalk-solve` crate | |
41+
| ----------------------- | --------------------- |
42+
| Purpose: | to solve a given goal |
43+
| Depends on IR: | chalk-ir and rust-ir |
44+
| Context required: | `RustIrDatabase` |
4545

4646
The `chalk-solve` crate exposes a key type called `Solver`. This is a
4747
solver that, given a goal (expressed in chalk-ir) will solve the goal
@@ -60,11 +60,11 @@ provide needed context for the solver -- notably, the solver can ask:
6060

6161
## The chalk-engine crate
6262

63-
| The `chalk-engine` crate | |
64-
|---|--- |
65-
| Purpose: | define the base solving strategy |
66-
| IR: | none |
67-
| Context required: | `Context` trait |
63+
| The `chalk-engine` crate | |
64+
| ------------------------ | -------------------------------- |
65+
| Purpose: | define the base solving strategy |
66+
| IR: | none |
67+
| Context required: | `Context` trait |
6868

6969
For the purposes of chalk, the `chalk-engine` crate is effectively
7070
encapsulated by `chalk-solve`. It defines the base SLG engine. It is

chalk-derive/src/lib.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ enum DeriveKind {
118118
}
119119

120120
decl_derive!([HasInterner, attributes(has_interner)] => derive_has_interner);
121-
decl_derive!([Visit, attributes(has_interner)] => derive_visit);
122-
decl_derive!([SuperVisit, attributes(has_interner)] => derive_super_visit);
123-
decl_derive!([Fold, attributes(has_interner)] => derive_fold);
121+
decl_derive!([TypeVisitable, attributes(has_interner)] => derive_type_visitable);
122+
decl_derive!([TypeSuperVisitable, attributes(has_interner)] => derive_type_super_visitable);
123+
decl_derive!([TypeFoldable, attributes(has_interner)] => derive_type_foldable);
124124
decl_derive!([Zip, attributes(has_interner)] => derive_zip);
125125

126126
fn derive_has_interner(mut s: synstructure::Structure) -> TokenStream {
@@ -136,24 +136,28 @@ fn derive_has_interner(mut s: synstructure::Structure) -> TokenStream {
136136
)
137137
}
138138

139-
/// Derives Visit for structs and enums for which one of the following is true:
139+
/// Derives TypeVisitable for structs and enums for which one of the following is true:
140140
/// - It has a `#[has_interner(TheInterner)]` attribute
141141
/// - There is a single parameter `T: HasInterner` (does not have to be named `T`)
142142
/// - There is a single parameter `I: Interner` (does not have to be named `I`)
143-
fn derive_visit(s: synstructure::Structure) -> TokenStream {
144-
derive_any_visit(s, parse_quote! { Visit }, parse_quote! { visit_with })
143+
fn derive_type_visitable(s: synstructure::Structure) -> TokenStream {
144+
derive_any_type_visitable(
145+
s,
146+
parse_quote! { TypeVisitable },
147+
parse_quote! { visit_with },
148+
)
145149
}
146150

147-
/// Same as Visit, but derives SuperVisit instead
148-
fn derive_super_visit(s: synstructure::Structure) -> TokenStream {
149-
derive_any_visit(
151+
/// Same as TypeVisitable, but derives TypeSuperVisitable instead
152+
fn derive_type_super_visitable(s: synstructure::Structure) -> TokenStream {
153+
derive_any_type_visitable(
150154
s,
151-
parse_quote! { SuperVisit },
155+
parse_quote! { TypeSuperVisitable },
152156
parse_quote! { super_visit_with },
153157
)
154158
}
155159

156-
fn derive_any_visit(
160+
fn derive_any_type_visitable(
157161
mut s: synstructure::Structure,
158162
trait_name: Ident,
159163
method_name: Ident,
@@ -164,13 +168,13 @@ fn derive_any_visit(
164168

165169
let body = s.each(|bi| {
166170
quote! {
167-
::chalk_ir::try_break!(::chalk_ir::visit::Visit::visit_with(#bi, visitor, outer_binder));
171+
::chalk_ir::try_break!(::chalk_ir::visit::TypeVisitable::visit_with(#bi, visitor, outer_binder));
168172
}
169173
});
170174

171175
if kind == DeriveKind::FromHasInterner {
172176
let param = get_generic_param_name(input).unwrap();
173-
s.add_where_predicate(parse_quote! { #param: ::chalk_ir::visit::Visit<#interner> });
177+
s.add_where_predicate(parse_quote! { #param: ::chalk_ir::visit::TypeVisitable<#interner> });
174178
}
175179

176180
s.add_bounds(synstructure::AddBounds::None);
@@ -179,7 +183,7 @@ fn derive_any_visit(
179183
quote! {
180184
fn #method_name <B>(
181185
&self,
182-
visitor: &mut dyn ::chalk_ir::visit::Visitor < #interner, BreakTy = B >,
186+
visitor: &mut dyn ::chalk_ir::visit::TypeVisitor < #interner, BreakTy = B >,
183187
outer_binder: ::chalk_ir::DebruijnIndex,
184188
) -> std::ops::ControlFlow<B> {
185189
match *self {
@@ -250,11 +254,11 @@ fn derive_zip(mut s: synstructure::Structure) -> TokenStream {
250254
)
251255
}
252256

253-
/// Derives Fold for structs and enums for which one of the following is true:
257+
/// Derives TypeFoldable for structs and enums for which one of the following is true:
254258
/// - It has a `#[has_interner(TheInterner)]` attribute
255259
/// - There is a single parameter `T: HasInterner` (does not have to be named `T`)
256260
/// - There is a single parameter `I: Interner` (does not have to be named `I`)
257-
fn derive_fold(mut s: synstructure::Structure) -> TokenStream {
261+
fn derive_type_foldable(mut s: synstructure::Structure) -> TokenStream {
258262
s.underscore_const(true);
259263
s.bind_with(|_| synstructure::BindStyle::Move);
260264

@@ -265,7 +269,7 @@ fn derive_fold(mut s: synstructure::Structure) -> TokenStream {
265269
vi.construct(|_, index| {
266270
let bind = &bindings[index];
267271
quote! {
268-
::chalk_ir::fold::Fold::fold_with(#bind, folder, outer_binder)?
272+
::chalk_ir::fold::TypeFoldable::fold_with(#bind, folder, outer_binder)?
269273
}
270274
})
271275
});
@@ -277,7 +281,7 @@ fn derive_fold(mut s: synstructure::Structure) -> TokenStream {
277281
let param = get_generic_param_name(input).unwrap();
278282
s.add_impl_generic(parse_quote! { _U })
279283
.add_where_predicate(
280-
parse_quote! { #param: ::chalk_ir::fold::Fold<#interner, Result = _U> },
284+
parse_quote! { #param: ::chalk_ir::fold::TypeFoldable<#interner, Result = _U> },
281285
)
282286
.add_where_predicate(
283287
parse_quote! { _U: ::chalk_ir::interner::HasInterner<Interner = #interner> },
@@ -289,13 +293,13 @@ fn derive_fold(mut s: synstructure::Structure) -> TokenStream {
289293

290294
s.add_bounds(synstructure::AddBounds::None);
291295
s.bound_impl(
292-
quote!(::chalk_ir::fold::Fold<#interner>),
296+
quote!(::chalk_ir::fold::TypeFoldable<#interner>),
293297
quote! {
294298
type Result = #result;
295299

296300
fn fold_with<E>(
297301
self,
298-
folder: &mut dyn ::chalk_ir::fold::Folder < #interner, Error = E >,
302+
folder: &mut dyn ::chalk_ir::fold::TypeFolder < #interner, Error = E >,
299303
outer_binder: ::chalk_ir::DebruijnIndex,
300304
) -> ::std::result::Result<Self::Result, E> {
301305
Ok(match self { #body })

chalk-engine/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
use std::cmp::min;
5757
use std::usize;
5858

59-
use chalk_derive::{Fold, HasInterner, Visit};
59+
use chalk_derive::{HasInterner, TypeFoldable, TypeVisitable};
6060
use chalk_ir::interner::Interner;
6161
use chalk_ir::{
6262
AnswerSubst, Canonical, ConstrainedSubst, Constraint, DebruijnIndex, Goal, InEnvironment,
@@ -78,13 +78,13 @@ mod table;
7878
mod tables;
7979

8080
index_struct! {
81-
pub struct TableIndex { // FIXME: pub b/c Fold
81+
pub struct TableIndex { // FIXME: pub b/c TypeFoldable
8282
value: usize,
8383
}
8484
}
8585

8686
/// The paper describes these as `A :- D | G`.
87-
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, Visit, HasInterner)]
87+
#[derive(Clone, Debug, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable, HasInterner)]
8888
pub struct ExClause<I: Interner> {
8989
/// The substitution which, applied to the goal of our table,
9090
/// would yield A.
@@ -168,7 +168,7 @@ impl TimeStamp {
168168
///
169169
/// trying to solve `?T: Foo` would immediately require solving `?T:
170170
/// Sized`, and hence would flounder.
171-
#[derive(Clone, Debug, PartialEq, Eq, Hash, Fold, Visit)]
171+
#[derive(Clone, Debug, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)]
172172
pub struct FlounderedSubgoal<I: Interner> {
173173
/// Literal that floundered.
174174
pub floundered_literal: Literal<I>,
@@ -209,7 +209,7 @@ pub struct CompleteAnswer<I: Interner> {
209209
}
210210

211211
/// Either `A` or `~A`, where `A` is a `Env |- Goal`.
212-
#[derive(Clone, Debug, Fold, Visit)]
212+
#[derive(Clone, Debug, TypeFoldable, TypeVisitable)]
213213
pub enum Literal<I: Interner> {
214214
// FIXME: pub b/c fold
215215
Positive(InEnvironment<Goal<I>>),

chalk-engine/src/normalize_deep.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chalk_ir::fold::shift::Shift;
2-
use chalk_ir::fold::{Fold, Folder};
2+
use chalk_ir::fold::{TypeFoldable, TypeFolder};
33
use chalk_ir::interner::Interner;
44
use chalk_ir::*;
55
use chalk_solve::infer::InferenceTable;
@@ -21,7 +21,7 @@ impl<I: Interner> DeepNormalizer<'_, I> {
2121
/// See also `InferenceTable::canonicalize`, which -- during real
2222
/// processing -- is often used to capture the "current state" of
2323
/// variables.
24-
pub fn normalize_deep<T: Fold<I>>(
24+
pub fn normalize_deep<T: TypeFoldable<I>>(
2525
table: &mut InferenceTable<I>,
2626
interner: I,
2727
value: T,
@@ -35,10 +35,10 @@ impl<I: Interner> DeepNormalizer<'_, I> {
3535
}
3636
}
3737

38-
impl<I: Interner> Folder<I> for DeepNormalizer<'_, I> {
38+
impl<I: Interner> TypeFolder<I> for DeepNormalizer<'_, I> {
3939
type Error = NoSolution;
4040

41-
fn as_dyn(&mut self) -> &mut dyn Folder<I, Error = Self::Error> {
41+
fn as_dyn(&mut self) -> &mut dyn TypeFolder<I, Error = Self::Error> {
4242
self
4343
}
4444

chalk-engine/src/slg/resolvent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::slg::ResolventOps;
33
use crate::{ExClause, Literal, TimeStamp};
44
use chalk_ir::cast::Caster;
55
use chalk_ir::fold::shift::Shift;
6-
use chalk_ir::fold::Fold;
6+
use chalk_ir::fold::TypeFoldable;
77
use chalk_ir::interner::{HasInterner, Interner};
88
use chalk_ir::zip::{Zip, Zipper};
99
use chalk_ir::*;
@@ -708,7 +708,7 @@ impl<'i, I: Interner> Zipper<I> for AnswerSubstitutor<'i, I> {
708708
pending: &Binders<T>,
709709
) -> Fallible<()>
710710
where
711-
T: HasInterner<Interner = I> + Zip<I> + Fold<I, Result = T>,
711+
T: HasInterner<Interner = I> + Zip<I> + TypeFoldable<I, Result = T>,
712712
{
713713
self.outer_binder.shift_in();
714714
Zip::zip_with(

0 commit comments

Comments
 (0)