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
letshape_copy=sh; // doesn't move sh because T: Copy
72
-
draw_twice(surface, sh); // Can use generic function because T: Shape
73
-
}
65
+
[bound.satisfaction.impl.builtin]
74
66
75
-
structFigure<S:Shape>(S, S);
67
+
There exists impls which are automatically generated by the compiler.
76
68
77
-
fnname_figure<U:Shape>(
78
-
figure:Figure<U>, // Type Figure<U> is well-formed because U: Shape
79
-
) {
80
-
println!(
81
-
"Figure of two {}",
82
-
U::name(), // Can use associated function
83
-
);
84
-
}
85
-
```
69
+
-`Sized`,`Copy`, `Clone`,...
86
70
87
-
r[bound.trivial]
88
-
Bounds that don't use the item's parameters or [higher-ranked lifetimes] are checked when the item is defined.
89
-
It is an error for such a bound to be false.
90
-
91
-
r[bound.special]
92
-
[`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic types when using the item, even if the use does not provide a concrete type.
93
-
It is an error to have `Copy` or `Clone` as a bound on a mutable reference, [trait object], or [slice].
94
-
It is an error to have `Sized` as a bound on a trait object or slice.
95
-
96
-
```rust,compile_fail
97
-
struct A<'a, T>
98
-
where
99
-
i32: Default, // Allowed, but not useful
100
-
i32: Iterator, // Error: `i32` is not an iterator
101
-
&'a mut T: Copy, // (at use) Error: the trait bound is not satisfied
102
-
[T]: Sized, // (at use) Error: size cannot be known at compilation
103
-
{
104
-
f: &'a T,
105
-
}
106
-
struct UsesA<'a, T>(A<'a, T>);
107
-
```
71
+
72
+
- alternative: mention this in item-kind impl
73
+
74
+
[bound.satisfaction.impl.builtin.trait-object]
75
+
76
+
Trait objects implement their trait if TODO: lookup conditions, something something project bounds make sense
77
+
78
+
[bound.satisfaction.bounds]
79
+
80
+
While inside of a generic item, trait bounds can be satisfied by using the where-bounds of the current item as the item is able to assume that its bounds are satisfied. For this, higher-ranked where-bounds can be instantiated with inference variables. The where-bound is then equated with the trait bound that needs to be satisfied.
81
+
82
+
[bound.satisfaction.alias-bounds]
83
+
84
+
- if an alias cannot be normalized in the current environment, trait bounds using this alias as a self type can be proven by using its item bounds
85
+
- TODO: alias bounds from nested self-types github.com/rust-lang/rust/pull/120752
86
+
87
+
88
+
[bound.satisfaction.candidate-preference]
89
+
90
+
> This is purely descriptive. Candidate preference behavior may change in future releases and must not be relied upon for correctness or soundness.
91
+
92
+
If there are multiple ways to satisfy a trait bound, some groups of candidate are preferred over others. In case a single group has multiple different candidates, the bound remains ambiguous. Candidate preference has the following order
93
+
- builtin implementations of `Sized`
94
+
- if there are any non-global where-bounds, all where-bounds
95
+
- alias-bounds
96
+
- impls
97
+
- In case the goal trait bound does not contain any inference variables, we prefer builtin trait object impls over user-written impls. TODO: that's unsound jank
98
+
- global where-bounds (only relevant if it does not hold)
99
+
100
+
> note: this candidate preference can result in incorrect errors and type mismatches, e.g. ...
101
+
102
+
[bound.satisfaction.cycles]
103
+
104
+
In case satisfying a bound requires recursively satisfying the same bound, we've encountered a *cycle*. TODO: terminology is iffy here .... can just drop this again
108
105
109
106
r[bound.trait-object]
110
107
Trait and lifetime bounds are also used to name [trait objects].
0 commit comments