Skip to content

Commit 9edf8ec

Browse files
committed
impl const blocks are not just "similar" to const fn, they behave exactly the same
1 parent a5e07d8 commit 9edf8ec

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

const-generic-const-fn-bounds.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,24 @@ for it.
8181
These rules for associated types exist to make this RFC forward compatible with adding const default bodies
8282
for trait methods. These are further discussed in the "future work" section.
8383

84-
## Generic `impl` blocks
84+
## Generic bounds
8585

86-
Similar to generic parameters on `const` functions, one can have generic parameters on `impl` blocks.
87-
These follow the same rules as bounds on `const` functions:
86+
The above section skimmed over a few topics for brevity. First of all, `impl const` items can also
87+
have generic parameters and thus bounds on these parameters, and these bounds follow the same rules
88+
as bounds on generic parameters on `const` functions: all bounds can only be substituted with types
89+
that have `impl const` items for all the bounds. Thus the `T` in the following `impl` requires that
90+
when `MyType<T>` is used in a const context, `T` needs to have an `impl const Add for Foo`.
8891

89-
* all bounds are required to have `impl const` for substituted types if the impl is used in a const context
90-
* except in the presence of `?const` (see below)
91-
* if the impl is used at runtime, there are no restrictions what kind of bounds are required
92+
```rust
93+
impl<T: Add> const Add for MyType<T> {
94+
/* some code here */
95+
}
96+
const FOO: MyType<u32> = ...;
97+
const BAR: MyType<u32> = FOO + FOO; // only legal because `u32: const Add`
98+
```
99+
100+
Furthermore, if `MyType` is used outside a const context, there are no constness requirements on the
101+
bounds for types substituted for `T`.
92102

93103
## Drop
94104

0 commit comments

Comments
 (0)