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

Experiment: Generic const items #214

Closed
fmease opened this issue Jul 12, 2023 · 2 comments
Closed

Experiment: Generic const items #214

fmease opened this issue Jul 12, 2023 · 2 comments

Comments

@fmease
Copy link
Member

fmease commented Jul 12, 2023

In the current version of Rust it is impossible to place generic parameters and where-clauses on free and associated constant items. This limits developers in their expressive power and renders certain desired functionality unattainable.

I hereby propose an experiment carried out by me in which the compiler gets extended to support generic const items addressing the aforementioned shortcoming of the language in an attempt to identify benefits and concerns in preparation for an RFC.

Motivation and use-cases

In no particular order:

  • It makes constant items more consistent with other similar items, those being type aliases, associated types, free & associated functions, ADTs, traits and implementations. This is especially true for associated constants whose two siblings both already support generic parameters and where-clauses.
  • It allows for increased conciseness. While generic parameters on free & associated constant items can (always?) be emulated by moving them up to the parent of the constant (i.e. the corresponding trait or type which may require defining such a parent beforehand and converting a free to an associated constant), this approach is cumbersome and unintuitive for both the author and the consumer. As for a concrete example, one does not have to look any further than the compiler itself: TyCtxtConsts::<'tcx>::DEREF_PROJECTION (example kindly provided by @compiler-errors)
  • It enables users to write const-evaluatable bounds (currently part of the feature generic_const_exprs) on constant items unblocking many useful patterns. See also no place to add evaluatable bounds to assoc const items rust#104400.
  • It could form the basis for a potential MVP of generic_const_exprs. See also https://lcnr.de/blog/generic-const-expressions/
  • It allows users to make their traits that contain associated constants object-safe by adding where Self: Sized to each associated constant just like they would do with associated functions and types. See also Don't require associated types with Self: Sized bounds in dyn Trait objects rust#112319.

Links

Initial people involved

@scottmcm
Copy link
Member

scottmcm commented Jul 12, 2023

As before, I'm happy to second/liason an experiment for this. We already have None::<i32> that sure looks like a generic const, so experimenting with a "real" version of that makes good sense to me.

cc @rust-lang/lang in case anyone wants to raise an objection.

@scottmcm
Copy link
Member

@fmease We briefly raised this in the lang team meeting today, and there were still no objections, so the experiment is approved to move forward!

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 28, 2023
Implement generic const items

This implements generic parameters and where-clauses on free and associated const items under the experimental feature gate `generic_const_items`. See rust-lang/lang-team#214.

Tracking issue: rust-lang#113521.
Fixes rust-lang#104400.

This PR doesn't include rustfmt support as per [nightly style procedure](https://github.com/rust-lang/style-team/blob/master/nightly-style-procedure.md) and it doesn't add rustdoc JSON support (see [related Zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/Rustdoc.20JSON.3A.20Experimental.20rustc.20features)).

CC `@scottmcm` `@compiler-errors` `@WalterSmuts`
r? `@oli-obk`

<details><summary>Resolved Questions</summary>

* Q: Should `const ADD<const N: usize, const M: usize>: usize = N + M; ADD::<0, 1>` trigger the error *generic parameters may not be used in const operations* (which can be unlocked with `#![feature(generic_const_exprs)]`). Currently it doesn't. Or does this fall under [this paragraph](https://github.com/rust-lang/rust/blob/71f71a5397c42fec01f5c1045c638d961fa9f7ca/compiler/rustc_resolve/src/late.rs#L191)?
  * A: No, rust-lang#113522 (comment)
* Q: Should `const UNUSED: () = () where String: Copy;` (with `#![feature(trivial_bounds)]` and with `UNUSED` unused) succeed compilation? Currently it doesn't: *evaluation of constant value failed // entering unreachable code*
  * A: Yes, but postponed until stabilization (latest), rust-lang#113522 (comment)

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants