Skip to content

Commit 8d9f649

Browse files
committed
Elaborate where ~const Trait bounds can be used
1 parent 83f31f7 commit 8d9f649

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

text/0000-const-trait-impls.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Examples:
155155
* `dyn const Trait`, requiring any type that is unsized to this dyn trait to have a const trait impl for `Trait`.
156156
* These are not part of this RFC because they require `const` function pointers. See [the Future Possibilities section](#future-possibilities).
157157
* `impl const Trait` (in all positions).
158-
* These are not part of this RFC because they require `const` function pointers. See [the Future Possibilities section](#future-possibilities).
158+
* These are not part of this RFC.
159159
* `trait Foo: const Bar {}`, requiring every type that has an impl for `Foo` (even a non-const one), to also have a const trait impl for `Bar`.
160160
* `trait Foo { type Bar: const Trait; }`, requiring all the impls to provide a type for `Bar` that has a const trait impl for `Trait`
161161

@@ -394,6 +394,17 @@ While this could be modelled with generic parameters in the type system, that:
394394

395395
Thus that approach was abandoned after proponents and opponents cooperated in trying to make the generic parameter approach work, resulting in all proponents becoming opponents, too.
396396

397+
### Sites where `const Trait` bounds can be used
398+
399+
Everywhere where non-const trait bounds can be written, but only for traits that are declared `const Trait`.
400+
401+
### Sites where `~const Trait` bounds can be used
402+
403+
* `const fn`
404+
* `impl const Trait for Type`
405+
* NOT in inherent impls, the individual `const fn` need to be annotated instead
406+
* associated types bounds of `const trait Trait` declarations
407+
397408
### `const` desugaring
398409

399410
```rust
@@ -729,7 +740,7 @@ const fn foo<T: Trait<bikeshed#effect = ~const> + OtherTrait<bikeshed#effect = c
729740
We could default to making all `T: Trait` bounds be const if the function is called from a const context, and require a `T: ?const Trait` opt out
730741
for when a trait bound is only used for its associated types and consts.
731742

732-
This requires a new `~const fn` syntax (sigils or syntax bikesheddable), as the existing `const fn` already has trait bounds that
743+
This requires new syntax (demonstrated here with `#[next_const_fn]`), as the existing `const fn` already has trait bounds that
733744
do not require const trait impls even if used in const contexts.
734745

735746
An example from libstd today is [the impl block of Vec::new](https://github.com/rust-lang/rust/blob/1ab85fbd7474e8ce84d5283548f21472860de3e2/library/alloc/src/vec/mod.rs#L406) which has an implicit `A: Allocator` bound from [the type definition](https://github.com/rust-lang/rust/blob/1ab85fbd7474e8ce84d5283548f21472860de3e2/library/alloc/src/vec/mod.rs#L397).
@@ -741,6 +752,7 @@ const trait Foo: Bar + ?const Baz {}
741752

742753
impl const Foo for () {}
743754

755+
#[next_const_fn]
744756
const fn foo<T: Foo>() -> T {
745757
// cannot call `Baz` methods
746758
<T as Bar>::bar()
@@ -922,6 +934,10 @@ This in turn implies that a `const fn()` function pointer, a `~const fn()` funct
922934
different `TypeId`s, which is something that requires more design and consideration to clarify whether supporting downcasting with `Any`
923935
or just supporting `TypeId` equality checks detecting constness is desirable.
924936

937+
Furthermore `const fn()` pointers introduce a new situation: you can transmute arbitrary values (e.g. null pointers, or just integers) to
938+
`const fn()` pointers, and the type system will not protect you. Instead the const evaluator will reject that when it actually
939+
evaluateds the code around the function pointer or even as late as when the function call happens.
940+
925941
## `const` closures
926942

927943
Closures need explicit opt-in to be callable in const contexts.

0 commit comments

Comments
 (0)