Skip to content

Commit 1420ae0

Browse files
authored
Merge pull request #1060 from tlyu/generic-bounds-checking
2 parents 1e094b9 + a162596 commit 1420ae0

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/items/generics.md

+3-16
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,13 @@ parameters.
230230
The `for` keyword can be used to introduce [higher-ranked lifetimes]. It only
231231
allows [_LifetimeParam_] parameters.
232232

233-
Bounds that don't use the item's parameters or [higher-ranked lifetimes] are
234-
checked when the item is defined. It is an error for such a bound to be false.
235-
236-
[`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic
237-
types when defining the item. It is an error to have `Copy` or `Clone` as a
238-
bound on a mutable reference, [trait object] or [slice][arrays] or `Sized` as a
239-
bound on a trait object or slice.
240-
241-
```rust,compile_fail
233+
```rust
242234
struct A<T>
243235
where
244236
T: Iterator, // Could use A<T: Iterator> instead
245-
T::Item: Copy,
246-
String: PartialEq<T>,
237+
T::Item: Copy, // Bound on an associated type
238+
String: PartialEq<T>, // Bound on `String`, using the type parameter
247239
i32: Default, // Allowed, but not useful
248-
i32: Iterator, // Error: the trait bound is not satisfied
249-
[T]: Copy, // Error: the trait bound is not satisfied
250240
{
251241
f: T,
252242
}
@@ -303,9 +293,6 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
303293
[path expression]: ../expressions/path-expr.md
304294
[raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
305295
[references]: ../types/pointer.md#shared-references-
306-
[`Clone`]: ../special-types-and-traits.md#clone
307-
[`Copy`]: ../special-types-and-traits.md#copy
308-
[`Sized`]: ../special-types-and-traits.md#sized
309296
[structs]: structs.md
310297
[tuples]: ../types/tuple.md
311298
[trait object]: ../types/trait-object.md

src/trait-bounds.md

+26
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ fn name_figure<U: Shape>(
7373
}
7474
```
7575

76+
Bounds that don't use the item's parameters or [higher-ranked lifetimes] are checked when the item is defined.
77+
It is an error for such a bound to be false.
78+
79+
[`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.
80+
It is an error to have `Copy` or `Clone` as a bound on a mutable reference, [trait object], or [slice].
81+
It is an error to have `Sized` as a bound on a trait object or slice.
82+
83+
```rust,compile_fail
84+
struct A<'a, T>
85+
where
86+
i32: Default, // Allowed, but not useful
87+
i32: Iterator, // Error: `i32` is not an iterator
88+
&'a mut T: Copy, // (at use) Error: the trait bound is not satisfied
89+
[T]: Sized, // (at use) Error: size cannot be known at compilation
90+
{
91+
f: &'a T,
92+
}
93+
struct UsesA<'a, T>(A<'a, T>);
94+
```
95+
7696
Trait and lifetime bounds are also used to name [trait objects].
7797

7898
## `?Sized`
@@ -142,11 +162,17 @@ fn call_on_ref_zero<F>(f: F) where F: for<'a> Fn(&'a i32) {
142162
[LIFETIME_OR_LABEL]: tokens.md#lifetimes-and-loop-labels
143163
[_GenericParams_]: items/generics.md
144164
[_TypePath_]: paths.md#paths-in-types
165+
[`Clone`]: special-types-and-traits.md#clone
166+
[`Copy`]: special-types-and-traits.md#copy
145167
[`Sized`]: special-types-and-traits.md#sized
146168

169+
[arrays]: types/array.md
147170
[associated types]: items/associated-items.md#associated-types
148171
[supertraits]: items/traits.md#supertraits
149172
[generic]: items/generics.md
173+
[higher-ranked lifetimes]: #higher-ranked-trait-bounds
174+
[slice]: types/slice.md
150175
[Trait]: items/traits.md#trait-bounds
176+
[trait object]: types/trait-object.md
151177
[trait objects]: types/trait-object.md
152178
[where clause]: items/generics.md#where-clauses

0 commit comments

Comments
 (0)