generic const items: support effects#117530
Closed
fmease wants to merge 1 commit intorust-lang:masterfrom
Closed
Conversation
fmease
commented
Nov 3, 2023
ae233cf to
2bb16fd
Compare
2bb16fd to
f47ca54
Compare
fmease
commented
Nov 3, 2023
| #[derive(Clone, Copy)] | ||
| enum EffectContext { | ||
| Parametrized { host_param_id: LocalDefId }, | ||
| Invariant { value: bool }, |
Member
Author
There was a problem hiding this comment.
I struggle a bit with the modelling of this variant. I know that Invariant { value: true } is super useless and that I should probably make it into a unit variant. However, I don't know what I should name it. I don't know if EffectContext should mention const-specific stuff, I opted against it. Otherwise, AlwaysConst would be the most obvious choice.
Member
|
I don't know if we should even allow |
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Dec 27, 2023
…fee1-dead
Introduce `const Trait` (always-const trait bounds)
Feature `const_trait_impl` currently lacks a way to express “always const” trait bounds. This makes it impossible to define generic items like fns or structs which contain types that depend on const method calls (\*). While the final design and esp. the syntax of effects / keyword generics isn't set in stone, some version of “always const” trait bounds will very likely form a part of it. Further, their implementation is trivial thanks to the `effects` backbone.
Not sure if this needs t-lang sign-off though.
(\*):
```rs
#![feature(const_trait_impl, effects, generic_const_exprs)]
fn compute<T: const Trait>() -> Type<{ T::generate() }> { /*…*/ }
struct Store<T: const Trait>
where
Type<{ T::generate() }>:,
{
field: Type<{ T::generate() }>,
}
```
Lastly, “always const” trait bounds are a perfect fit for `generic_const_items`.
```rs
#![feature(const_trait_impl, effects, generic_const_items)]
const DEFAULT<T: const Default>: T = T::default();
```
Previously, we (oli, fee1-dead and I) wanted to reinterpret `~const Trait` as `const Trait` in generic const items which would've been quite surprising and not very generalizable.
Supersedes rust-lang#117530.
---
cc `@oli-obk`
As discussed
r? fee1-dead (or compiler)
bors
added a commit
to rust-lang-ci/rust
that referenced
this pull request
Dec 27, 2023
…fee1-dead
Introduce `const Trait` (always-const trait bounds)
Feature `const_trait_impl` currently lacks a way to express “always const” trait bounds. This makes it impossible to define generic items like fns or structs which contain types that depend on const method calls (\*). While the final design and esp. the syntax of effects / keyword generics isn't set in stone, some version of “always const” trait bounds will very likely form a part of it. Further, their implementation is trivial thanks to the `effects` backbone.
Not sure if this needs t-lang sign-off though.
(\*):
```rs
#![feature(const_trait_impl, effects, generic_const_exprs)]
fn compute<T: const Trait>() -> Type<{ T::generate() }> { /*…*/ }
struct Store<T: const Trait>
where
Type<{ T::generate() }>:,
{
field: Type<{ T::generate() }>,
}
```
Lastly, “always const” trait bounds are a perfect fit for `generic_const_items`.
```rs
#![feature(const_trait_impl, effects, generic_const_items)]
const DEFAULT<T: const Default>: T = T::default();
```
Previously, we (oli, fee1-dead and I) wanted to reinterpret `~const Trait` as `const Trait` in generic const items which would've been quite surprising and not very generalizable.
Supersedes rust-lang#117530.
---
cc `@oli-obk`
As discussed
r? fee1-dead (or compiler)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Treat
~constbounds on generic const items as “constbounds” (always const)1.cc @oli-obk
r? @fee1-dead
Footnotes
Obviously, it would be of advantage to at some point introduce const trait bounds into the surface language to enable users to abstract over generic const items with const fns. ↩