Skip to content

Commit 5d8e19f

Browse files
committed
reduce clutter when reading source
1 parent a82a329 commit 5d8e19f

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/doc/rustc-dev-guide/src/ty-fold.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ Binders can wrap an arbitrary Rust type `T`, not just a `Ty`.
77
So, how do we implement the `instantiate` methods on the `Early/Binder` types?
88

99
The answer is a couple of traits:
10-
[`TypeFoldable`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFoldable.html)
10+
[`TypeFoldable`]
1111
and
12-
[`TypeFolder`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html).
12+
[`TypeFolder`].
1313

1414
- `TypeFoldable` is implemented by types that embed type information. It allows you to recursively
1515
process the contents of the `TypeFoldable` and do stuff to them.
1616
- `TypeFolder` defines what you want to do with the types you encounter while processing the
1717
`TypeFoldable`.
1818

1919
For example, the `TypeFolder` trait has a method
20-
[`fold_ty`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html#method.fold_ty)
20+
[`fold_ty`]
2121
that takes a type as input and returns a new type as a result. `TypeFoldable` invokes the
2222
`TypeFolder` `fold_foo` methods on itself, giving the `TypeFolder` access to its contents (the
2323
types, regions, etc that are contained within).
@@ -36,7 +36,7 @@ So to reiterate:
3636
- `TypeFoldable` is a trait that is implemented by things that embed types.
3737

3838
In the case of `subst`, we can see that it is implemented as a `TypeFolder`:
39-
[`ArgFolder`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_type_ir/binder/struct.ArgFolder.html).
39+
[`ArgFolder`].
4040
Looking at its implementation, we see where the actual substitutions are happening.
4141

4242
However, you might also notice that the implementation calls this `super_fold_with` method. What is
@@ -91,17 +91,25 @@ things. We only want to do something when we reach a type. That means there may
9191
implementations. Such implementations of `TypeFoldable` tend to be pretty tedious to write by hand.
9292
For this reason, there is a `derive` macro that allows you to `#![derive(TypeFoldable)]`. It is
9393
defined
94-
[here](https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/type_foldable.rs).
94+
[here].
9595

9696
**`subst`** In the case of substitutions the [actual
97-
folder](https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L440-L451)
97+
folder]
9898
is going to be doing the indexing we’ve already mentioned. There we define a `Folder` and call
9999
`fold_with` on the `TypeFoldable` to process yourself. Then
100-
[fold_ty](https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L512-L536)
100+
[fold_ty]
101101
the method that process each type it looks for a `ty::Param` and for those it replaces it for
102102
something from the list of substitutions, otherwise recursively process the type. To replace it,
103103
calls
104-
[ty_for_param](https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L552-L587)
104+
[ty_for_param]
105105
and all that does is index into the list of substitutions with the index of the `Param`.
106106

107107
[a previous chapter]: ty_module/instantiating_binders.md
108+
[`TypeFoldable`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFoldable.html
109+
[`TypeFolder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html
110+
[`fold_ty`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html#method.fold_ty
111+
[`ArgFolder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_type_ir/binder/struct.ArgFolder.html
112+
[here]: https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/type_foldable.rs
113+
[actual folder]: https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L440-L451
114+
[fold_ty]: https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L512-L536
115+
[ty_for_param]: https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L552-L587

0 commit comments

Comments
 (0)