Skip to content

Commit

Permalink
Clarify when const parameters need to be used.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jan 10, 2021
1 parent 85a363c commit 7d86a9e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/items/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ fn foo<const N: usize>() -> Foo<N> { todo!() } // ERROR
fn bar<const N: usize>() -> Foo<{ N }> { todo!() } // ok
```

Unlike type and lifetime parameters, const parameters of types can be used without
being mentioned inside of a parameterized type:
Unlike type and lifetime parameters, const parameters can be declared without
being used inside of a parameterized item, with the exception of
implementations as described in [generic implementations]:

```rust,compile_fail
// ok
Expand All @@ -192,6 +193,8 @@ enum Bar<const M: usize> { A, B }
// ERROR: unused parameter
struct Baz<T>;
struct Biz<'a>;
struct Unconstrained;
impl<const N: usize> Unconstrained {}
```

When resolving a trait bound obligation, the exhaustiveness of all
Expand Down Expand Up @@ -298,6 +301,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
[enumerations]: enumerations.md
[functions]: functions.md
[function pointers]: ../types/function-pointer.md
[generic implementations]: implementations.md#generic-implementations
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
[implementations]: implementations.md
[item declarations]: ../statements.md#item-declarations
Expand Down
12 changes: 7 additions & 5 deletions src/items/implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ is considered local.

## Generic Implementations

An implementation can take type and lifetime parameters, which can be used in
the rest of the implementation. Type parameters declared for an implementation
must be used at least once in either the trait or the implementing type of an
implementation. Implementation parameters are written directly after the `impl`
keyword.
An implementation can take [generic parameters] which are written directly
after the `impl` keyword. The parameters can be used in the rest of the
implementation. Type and const parameters must be used at least once in either
the trait or the implementing type of an implementation. Lifetime parameters
do not need to be used unless they appear in an [associated type].

```rust
# trait Seq<T> { fn dummy(&self, _: T) { } }
Expand Down Expand Up @@ -219,6 +219,7 @@ attributes].
[trait]: traits.md
[associated functions]: associated-items.md#associated-functions-and-methods
[associated constants]: associated-items.md#associated-constants
[associated type]: associated-items.md#associated-types
[attributes]: ../attributes.md
[`cfg`]: ../conditional-compilation.md
[`deprecated`]: ../attributes/diagnostics.md#the-deprecated-attribute
Expand All @@ -230,3 +231,4 @@ attributes].
[local type]: ../glossary.md#local-type
[fundamental types]: ../glossary.md#fundamental-type-constructors
[uncovered type]: ../glossary.md#uncovered-type
[generic parameters]: generics.md

0 comments on commit 7d86a9e

Please sign in to comment.