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

generic const items: support effects #117530

Closed

Conversation

fmease
Copy link
Member

@fmease fmease commented Nov 3, 2023

Treat ~const bounds on generic const items as “const bounds” (always const)1.

cc @oli-obk
r? @fee1-dead

Footnotes

  1. 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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 3, 2023
@fmease fmease added F-effects `#![feature(effects)]` F-generic_const_items `#![feature(generic_const_items)]` and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 3, 2023
@fmease fmease changed the title effects: support generic const items generic const items: support effects Nov 3, 2023
@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 3, 2023
@fmease fmease force-pushed the effects-support-generic-const-items branch from ae233cf to 2bb16fd Compare November 3, 2023 07:53
@fmease fmease force-pushed the effects-support-generic-const-items branch from 2bb16fd to f47ca54 Compare November 3, 2023 09:41
#[derive(Clone, Copy)]
enum EffectContext {
Parametrized { host_param_id: LocalDefId },
Invariant { value: bool },
Copy link
Member Author

@fmease fmease Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fee1-dead
Copy link
Member

I don't know if we should even allow ~const for always-const contexts. I think it should probably be better if we used T: const Trait for such bounds which would be more consistent with the rest of the features.

@fmease fmease closed this Nov 8, 2023
@fmease fmease deleted the effects-support-generic-const-items branch November 8, 2023 15:07
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-effects `#![feature(effects)]` F-generic_const_items `#![feature(generic_const_items)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants