You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: explainer/inference.md
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@ fn main() {
62
62
63
63
```rust
64
64
modfoo {
65
-
typeFoo<T> =implClone;
65
+
typeFoo<T:Clone> =implClone;
66
66
67
67
fnmake_foo<A>(x:u32) ->Foo<A> {
68
68
x
@@ -217,6 +217,20 @@ To determine the hidden type for some opaque type `O`, we examine the set `C` of
217
217
218
218
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).
219
219
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
+
typeFoo<X> =implClone;
228
+
229
+
fnmake_foo<T:Clone>(t:T) {
230
+
t
231
+
}
232
+
```
233
+
220
234
### Limitations on exemplars
221
235
222
236
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
0 commit comments