Skip to content

Commit 61771df

Browse files
lcnrcompiler-errors
authored andcommitted
fix line lengths
1 parent 0bcdb56 commit 61771df

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

src/solve/coinduction.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# Coinduction
22

3-
The trait solver may use coinduction when proving goals. Coinduction is fairly subtle so we're giving it its own chapter.
3+
The trait solver may use coinduction when proving goals.
4+
Coinduction is fairly subtle so we're giving it its own chapter.
45

56
## Coinduction and induction
67

7-
With induction, we recursively apply proofs until we end up with a finite proof tree. Consider the example of `Vec<Vec<Vec<u32>>>: Debug` which results in the following tree.
8+
With induction, we recursively apply proofs until we end up with a finite proof tree.
9+
Consider the example of `Vec<Vec<Vec<u32>>>: Debug` which results in the following tree.
810

911
- `Vec<Vec<Vec<u32>>>: Debug`
1012
- `Vec<Vec<u32>>: Debug`
1113
- `Vec<u32>: Debug`
1214
- `u32: Debug`
1315

14-
This tree is finite. But not all goals we would want to hold have finite proof trees, consider the following example:
16+
This tree is finite. But not all goals we would want to hold have finite proof trees,
17+
consider the following example:
1518

1619
```rust
1720
struct List<T> {
@@ -20,7 +23,8 @@ struct List<T> {
2023
}
2124
```
2225

23-
For `List<T>: Send` to hold all its fields have to recursively implement `Send` as well. This would result in the following proof tree:
26+
For `List<T>: Send` to hold all its fields have to recursively implement `Send` as well.
27+
This would result in the following proof tree:
2428

2529
- `List<T>: Send`
2630
- `T: Send`
@@ -57,9 +61,9 @@ With cycles we have to be careful with caching. Due to canonicalization of regio
5761
variables we also have to rerun queries until the provisional result returned when hitting the cycle
5862
is equal to the final result.
5963

60-
TODO: elaborate here. We use the same approach as chalk for coinductive cycles. Note that the treatment
61-
for inductive cycles currently differs by simply returning `Overflow`. See [the relevant chapters][chalk]
62-
in the chalk book.
64+
TODO: elaborate here. We use the same approach as chalk for coinductive cycles.
65+
Note that the treatment for inductive cycles currently differs by simply returning `Overflow`.
66+
See [the relevant chapters][chalk] in the chalk book.
6367

6468
[chalk]: https://rust-lang.github.io/chalk/book/recursive/inductive_cycles.html
6569

@@ -90,20 +94,29 @@ impl<T: Clone> Clone for List<T> {
9094
}
9195
```
9296

93-
We are using `tail.clone()` in this impl. For this we have to prove `Box<List<T>>: Clone` which requires `List<T>: Clone` but that relies on the currently impl which we are currently checking. By adding that requirement to the `where`-clauses of the impl, which is what we would do with [perfect derive], we move that cycle into the trait solver and [get an error][ex1].
97+
We are using `tail.clone()` in this impl. For this we have to prove `Box<List<T>>: Clone`
98+
which requires `List<T>: Clone` but that relies on the currently impl which we are currently
99+
checking. By adding that requirement to the `where`-clauses of the impl, which is what we would
100+
do with [perfect derive], we move that cycle into the trait solver and [get an error][ex1].
94101

95102
### Recursive data types
96103

97-
We also need coinduction to reason about recursive types containing projections, e.g. the following currently fails to compile even though it should be valid.
104+
We also need coinduction to reason about recursive types containing projections,
105+
e.g. the following currently fails to compile even though it should be valid.
98106
```rust
99107
use std::borrow::Cow;
100108
pub struct Foo<'a>(Cow<'a, [Foo<'a>]>);
101109
```
102-
This issue has been known since at least 2015, see [#23714](https://github.com/rust-lang/rust/issues/23714) if you want to know more.
110+
This issue has been known since at least 2015, see
111+
[#23714](https://github.com/rust-lang/rust/issues/23714) if you want to know more.
103112

104113
### Explicitly checked implied bounds
105114

106-
When checking an impl, we assume that the types in the impl headers are well-formed. This means that when using instantiating the impl we have to prove that's actually the case. [#100051](https://github.com/rust-lang/rust/issues/100051) shows that this is not the case. To fix this, we have to add `WF` predicates for the types in impl headers. Without coinduction for all traits, this even breaks `core`.
115+
When checking an impl, we assume that the types in the impl headers are well-formed.
116+
This means that when using instantiating the impl we have to prove that's actually the case.
117+
[#100051](https://github.com/rust-lang/rust/issues/100051) shows that this is not the case.
118+
To fix this, we have to add `WF` predicates for the types in impl headers.
119+
Without coinduction for all traits, this even breaks `core`.
107120

108121
```rust
109122
trait FromResidual<R> {}
@@ -123,7 +136,7 @@ When checking that the impl of `FromResidual` is well formed we get the followin
123136
The impl is well formed if `<Ready<T> as Try>::Residual` and `Ready<T>` are well formed.
124137
- `wf(<Ready<T> as Try>::Residual)` requires
125138
- `Ready<T>: Try`, which requires because of the super trait
126-
- `Ready<T>: FromResidual<Ready<T> as Try>::Residual>`, which has an impl which requires **because of implied bounds**
139+
- `Ready<T>: FromResidual<Ready<T> as Try>::Residual>`, **because of implied bounds on impl**
127140
- `wf(<Ready<T> as Try>::Residual)` :tada: **cycle**
128141

129142
### Issues when extending coinduction to more goals
@@ -133,9 +146,13 @@ The issues here are not relevant for the current solver.
133146

134147
#### Implied super trait bounds
135148

136-
Our trait system currectly treats super traits, e.g. `trait Trait: SuperTrait`, by 1) requiring that `SuperTrait` has to hold for all types which implement `Trait`, and 2) assuming `SuperTrait` holds if `Trait` holds.
149+
Our trait system currectly treats super traits, e.g. `trait Trait: SuperTrait`,
150+
by 1) requiring that `SuperTrait` has to hold for all types which implement `Trait`,
151+
and 2) assuming `SuperTrait` holds if `Trait` holds.
137152

138-
Relying on 2) while proving 1) is unsound. This can only be observed in case of coinductive cycles. Without a cycles, whenever we rely on 2) we must have also proven 1) without relying on 2) for the used impl of `Trait`.
153+
Relying on 2) while proving 1) is unsound. This can only be observed in case of
154+
coinductive cycles. Without a cycles, whenever we rely on 2) we must have also
155+
proven 1) without relying on 2) for the used impl of `Trait`.
139156

140157
```rust
141158
trait Trait: SuperTrait {}
@@ -148,21 +165,25 @@ fn sup<T: SuperTrait>() {}
148165
fn requires_trait<T: Trait>() { sup::<T>() }
149166
fn generic<T>() { requires_trait::<T>() }
150167
```
151-
This is not really fundamental to coinduction but rather an existing property which is made unsound because of it.
168+
This is not really fundamental to coinduction but rather an existing property
169+
which is made unsound because of it.
152170

153171
##### Possible solutions
154172

155173
The easiest way to solve this would be to completely remove 2) and always elaborate
156174
`T: Trait` to `T: Trait` and `T: SuperTrait` outside of the trait solver.
157-
This would allow us to also remove 1), but as we still have to prove ordinary `where`-bounds on traits,
158-
that's just additional work.
175+
This would allow us to also remove 1), but as we still have to prove ordinary
176+
`where`-bounds on traits, that's just additional work.
159177

160-
While one could imagine ways to disable cyclic uses of 2) when checking 1), at least the ideas of myself - @lcnr -
161-
are all far to complex to be reasonable.
178+
While one could imagine ways to disable cyclic uses of 2) when checking 1),
179+
at least the ideas of myself - @lcnr - are all far to complex to be reasonable.
162180

163181
#### `normalizes_to` goals and progress
164182

165-
A `normalizes_to` goal represents the requirement that `<T as Trait>::Assoc` normalizes to some `U`. This is achieved by defacto first normalizing `<T as Trait>::Assoc` and then equating the resulting type with `U`. It should be a mapping as each projection should normalize to exactly one type. By simply allowing infinite proof trees, we would get the following behavior:
183+
A `normalizes_to` goal represents the requirement that `<T as Trait>::Assoc` normalizes
184+
to some `U`. This is achieved by defacto first normalizing `<T as Trait>::Assoc` and then
185+
equating the resulting type with `U`. It should be a mapping as each projection should normalize
186+
to exactly one type. By simply allowing infinite proof trees, we would get the following behavior:
166187

167188
```rust
168189
trait Trait {
@@ -174,19 +195,30 @@ impl Trait for () {
174195
}
175196
```
176197

177-
If we now compute `normalizes_to(<() as Trait>::Assoc, Vec<u32>)`, we would resolve the impl and get the associated type `<() as Trait>::Assoc`. We then equate that with the expected type, causing us to check `normalizes_to(<() as Trait>::Assoc, Vec<u32>)` again. This just goes on forever, resulting in an infinite proof tree.
198+
If we now compute `normalizes_to(<() as Trait>::Assoc, Vec<u32>)`, we would resolve the impl
199+
and get the associated type `<() as Trait>::Assoc`. We then equate that with the expected type,
200+
causing us to check `normalizes_to(<() as Trait>::Assoc, Vec<u32>)` again.
201+
This just goes on forever, resulting in an infinite proof tree.
178202

179203
This means that `<() as Trait>::Assoc` would be equal to any other type which is unsound.
180204

181205
##### How to solve this
182206

183207
**WARNING: THIS IS SUBTLE AND MIGHT BE WRONG**
184208

185-
Unlike trait goals, `normalizes_to` has to be *productive*[^1]. A `normalizes_to` goal is productive once the projection normalizes to a rigid type constructor, so `<() as Trait>::Assoc` normalizing to `Vec<<() as Trait>::Assoc>` would be productive.
209+
Unlike trait goals, `normalizes_to` has to be *productive*[^1]. A `normalizes_to` goal
210+
is productive once the projection normalizes to a rigid type constructor,
211+
so `<() as Trait>::Assoc` normalizing to `Vec<<() as Trait>::Assoc>` would be productive.
186212

187-
A `normalizes_to` goal has two kinds of nested goals. Nested requirements needed to actually normalize the projection, and the equality between the normalized projection and the expected type. Only the equality has to be productive. A branch in the proof tree is productive if it is either finite, or contains at least one `normalizes_to` where the alias is resolved to a rigid type constructor.
213+
A `normalizes_to` goal has two kinds of nested goals. Nested requirements needed to actually
214+
normalize the projection, and the equality between the normalized projection and the
215+
expected type. Only the equality has to be productive. A branch in the proof tree is productive
216+
if it is either finite, or contains at least one `normalizes_to` where the alias is resolved
217+
to a rigid type constructor.
188218

189-
Alternatively, we could simply always treat the equate branch of `normalizes_to` as inductive. Any cycles should result in infinite types, which aren't supported anyways and would only result in overflow when deeply normalizing for codegen.
219+
Alternatively, we could simply always treat the equate branch of `normalizes_to` as inductive.
220+
Any cycles should result in infinite types, which aren't supported anyways and would only
221+
result in overflow when deeply normalizing for codegen.
190222

191223
experimentation and examples: https://hackmd.io/-8p0AHnzSq2VAE6HE_wX-w?view
192224

src/solve/the-solver.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The basic structure of the solver is a pure function
1111
While the actual solver is not fully pure to deal with overflow and cycles, we are
1212
going to defer that for now.
1313

14-
To deal with inference variables and to improve caching, we use [canonicalization](/canonicalization.html).
14+
To deal with inference variables and to improve caching, we use
15+
[canonicalization](/canonicalization.html).
1516

1617
TODO: write the remaining code for this as well.

src/solve/trait-solving.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ recursively proving its nested goals. For a list of possible candidates with exa
3131
[`CandidateSource`]. The most important candidates are `Impl` candidates, i.e. trait implementations
3232
written by the user, and `ParamEnv` candidates, i.e. assumptions in our current environment.
3333

34-
Looking at the above example, to prove `Vec<T>: Clone` we first use `impl<T: Clone> Clone for Vec<T>`.
35-
To use this impl we have to prove the nested goal that `T: Clone` holds. This can use the
36-
assumption `T: Clone` from the `ParamEnv` which does not have any nested goals.
37-
Therefore `Vec<T>: Clone` holds.
34+
Looking at the above example, to prove `Vec<T>: Clone` we first use
35+
`impl<T: Clone> Clone for Vec<T>`. To use this impl we have to prove the nested
36+
goal that `T: Clone` holds. This can use the assumption `T: Clone` from the `ParamEnv`
37+
which does not have any nested goals. Therefore `Vec<T>: Clone` holds.
3838

3939
The trait solver can either return success, ambiguity or an error as a [`CanonicalResponse`].
4040
For success and ambiguity it also returns constraints inference and region constraints.
@@ -55,8 +55,9 @@ simply be unsound by assuming a trait is implemented even though it is not.
5555

5656
### 2. If type checker solves generic goal concrete instantiations of that goal have the same result
5757

58-
Pretty much: If we successfully typecheck a generic function concrete instantiations of that function
59-
should also typeck. We should not get errors post-monomorphization. We can however get overflow.
58+
Pretty much: If we successfully typecheck a generic function concrete instantiations
59+
of that function should also typeck. We should not get errors post-monomorphization.
60+
We can however get overflow.
6061

6162
### 3. Trait goals in empty environments are proven by a unique impl.
6263

@@ -84,7 +85,8 @@ is special behavior for `'static`.
8485

8586
### 7. Removing ambiguity makes strictly more things compile
8687

87-
We *should* not rely on ambiguity for things to compile. Not doing that will cause future improvements to be breaking changes.
88+
We *should* not rely on ambiguity for things to compile.
89+
Not doing that will cause future improvements to be breaking changes.
8890

8991
### 8. semantic equality implies structural equality
9092

0 commit comments

Comments
 (0)