Skip to content

Commit 87428cd

Browse files
committed
add the example that oli-obk pointed out
1 parent 8d69f22 commit 87428cd

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

explainer/inference.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn main() {
6262

6363
```rust
6464
mod foo {
65-
type Foo<T> = impl Clone;
65+
type Foo<T: Clone> = impl Clone;
6666

6767
fn make_foo<A>(x: u32) -> Foo<A> {
6868
x
@@ -217,6 +217,20 @@ To determine the hidden type for some opaque type `O`, we examine the set `C` of
217217

218218
Computing the hidden type `H` for `O<P1..Pn>` that is required by the item `I` must be done independently from any other items within the defining scope of `O`. It can be done by creating an inference variable `?V` for `O<P1..Pn>` and unifying `?O` with all types that must be equal to `O<P1...Pn>`. This inference variable `?V` is called the *exemplar*, as it is an "example" of what the hidden type is when `P1..Pn` are substituted for the generic arguments of `O`. Note that a given function may produce multiple exemplars for a single opaque type if it contains multiple references to `O` with distinct generic arguments. Computing the actual hidden type is done by [higher-order pattern unification](#higher-order-pattern-unification).
219219

220+
### Checking the hidden type proposed by an item `I`
221+
222+
Once the hidden type for an item `I` is determined, we also have to check that the hidden type is well-formed in the context of the type alias and that its bounds are satisfied. Since the where clauses that were in scope when the exemplar was computed can be different from those declared on the opaque type, this is not a given.
223+
224+
**Example.** The following program **does not compile** because of this check. Here, the exemplar is `T` and the hidden type is `X`. However, the type alias does not declare that `X: Clone`, so the `impl Clone` bounds are not known to be satisfied.
225+
226+
```rust
227+
type Foo<X> = impl Clone;
228+
229+
fn make_foo<T: Clone>(t: T) {
230+
t
231+
}
232+
```
233+
220234
### Limitations on exemplars
221235

222236
Whenever an item `I` proposes a hidden type, the following conditions must be met:
@@ -281,7 +295,7 @@ When an item `I` finishes its type check, it will have computed an exemplar type
281295
Consider an opaque type `Foo`:
282296

283297
```rust
284-
type Foo<T, U> = impl Clone;
298+
type Foo<T: Clone, U: Clone> = impl Clone;
285299
```
286300

287301
and an item `make_foo` that contains `Foo`:

0 commit comments

Comments
 (0)