Prepare const traits for stabilization (original) (raw)

Summary

Prepare const Trait bounds for stabilization.

Motivation

const fn on stable are unable to invoke trait methods, limiting their usefulness. After years of experimentation, the compiler now has a promising implementation of const traits and key parts of the stdlib have been updated to use it. However, the feature is still firmly in experimental territory: there has never been an RFC describing its syntax.

The goal for the next 6 months is to author an RFC proposing an end-user syntax for const bounds and a specific semantics. Our believe is that the core implementation work is done and that it should be possible to merge the RFC, implement the syntactic choices, issue a public call for experimentation, and otherwise pave the ground for stabilization (stabilization itself is a stretch goal and seems likely to occur in 2025H2).

The status quo

People write a lot of code that will be run in compile time. They include procedural macros, build scripts (42.8k hits on GitHub for build.rs), and const functions/consts (108k hits on GitHub for const fn). Not being able to write const functions with generic behavior is often cited as a pain point of Rust's compile time capabilities. Because of the limited expressiveness of const fn, people may decide to move some compile time logic to a build script, which could increase build times, or simply choose not to do it in compile time (even though it would have helped runtime performance).

There are also language features that require the use of traits, such as iterating with for and handling errors with ?. Because the Iterator and Try traits currently cannot be used in constant contexts, people are unable to use ? to handle results, nor use iterators e.g. for x in 0..5.

The next 6 months

The primary goal is to do "Everything but" stabilization of this feature over the next 6 months

As a secondary goal, we will use this as an experiment to drive forward a-mir-formality, with Niko Matsakis and Oliver Scherer mentoring byt to extend a-mir-formality with support for const traits.

The "shiny future" we are working towards

We're working towards enabling developers to do more things in general within a const context. Const traits is a blocker for many future possibilities (see also the const eval feature skill tree) including heap operations in const contexts.

Design axioms

None.

Ownership and team asks

Steps towards the primary goal of doing everything towards stabilization apart from approving stabilization itself:

Formalize const-traits in a-mir-formality

Steps towards formalization of the approach in a-mir-formality:

Task Owner(s) or team(s) Notes
Discussion and moral support Team types During types team office hours, we'll share information about our progress.
Implementation byt
Mentoring and kibbitzing Niko Matsakis, Oliver Scherer

Definitions

Definitions for terms used above:

Frequently asked questions

Will we be stabilizing the syntax found on libstd today?

Most likely not. The current syntax includes some controversial notation, such as T: ~Trait. The point of the RFC is to determine what syntax will be used. What we hope will not change is the semantics.