Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move text about checking of bounds on generics #1060

Merged
merged 3 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions src/items/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,13 @@ parameters.
The `for` keyword can be used to introduce [higher-ranked lifetimes]. It only
allows [_LifetimeParam_] parameters.

Bounds that don't use the item's parameters or [higher-ranked lifetimes] are
checked when the item is defined. It is an error for such a bound to be false.

[`Copy`], [`Clone`], and [`Sized`] bounds are also checked for certain generic
types when defining the item. It is an error to have `Copy` or `Clone` as a
bound on a mutable reference, [trait object] or [slice][arrays] or `Sized` as a
bound on a trait object or slice.

```rust,compile_fail
```rust
struct A<T>
where
T: Iterator, // Could use A<T: Iterator> instead
T::Item: Copy,
String: PartialEq<T>,
T::Item: Copy, // Bound on an associated type
String: PartialEq<T>, // Bound on `String`, using the type parameter
i32: Default, // Allowed, but not useful
i32: Iterator, // Error: the trait bound is not satisfied
[T]: Copy, // Error: the trait bound is not satisfied
{
f: T,
}
Expand Down Expand Up @@ -303,9 +293,6 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
[path expression]: ../expressions/path-expr.md
[raw pointers]: ../types/pointer.md#raw-pointers-const-and-mut
[references]: ../types/pointer.md#shared-references-
[`Clone`]: ../special-types-and-traits.md#clone
[`Copy`]: ../special-types-and-traits.md#copy
[`Sized`]: ../special-types-and-traits.md#sized
[structs]: structs.md
[tuples]: ../types/tuple.md
[trait object]: ../types/trait-object.md
Expand Down
25 changes: 25 additions & 0 deletions src/trait-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ fn name_figure<U: Shape>(
}
```

Bounds that don't use the item's parameters or [higher-ranked lifetimes] are
checked when the item is defined. It is an error for such a bound to be false.

[`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.
It is an error to have `Copy` or `Clone` as a bound on a mutable reference, [trait object], or [slice][arrays].
It is an error to have `Sized` as a bound on a trait object or slice.

```rust,compile_fail
struct A<'a, T>
where
i32: Default, // Allowed, but not useful
i32: Iterator, // Error: `i32` is not an iterator
&'a mut T: Copy, // (at use) Error: the trait bound is not satisfied
[T]: Sized, // (at use) Error: size cannot be known at compilation
{
f: &'a T,
}
struct UsesA<'a, T>(A<'a, T>);
```

Trait and lifetime bounds are also used to name [trait objects].

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

[arrays]: types/array.md
[associated types]: items/associated-items.md#associated-types
[supertraits]: items/traits.md#supertraits
[generic]: items/generics.md
[higher-ranked lifetimes]: #higher-ranked-trait-bounds
[Trait]: items/traits.md#trait-bounds
[trait object]: types/trait-object.md
[trait objects]: types/trait-object.md
[where clause]: items/generics.md#where-clauses