unlock unlimited code runs for $79, limited time →
NOSSLETTER.techLIVE
back to archive

type system const items via direct rhs

Language: Rust+307 -197feature
concepts demonstrated
Associated constantsConst genericsMacros (macro_rules! and procedural macros)Type system transparency
The Issue & Context

Rust's type system currently supports 'type const' for associated constants, but this syntax is being phased out in favor of direct constant items using a 'gca!' macro or direct RHS expressions to represent type system transparent constants.

Root Cause Analysis

The existing 'type const' syntax and semantics are incompatible with the planned Generic Const Arguments (GCA) feature, causing confusion and complexity in the compiler's handling of associated constants and const generics.

Solution Strategy & Architectural Pattern

This PR introduces support for const items with a direct RHS using the 'direct_const_arg!' macro, allowing constants to be type system transparent similarly to 'type const'. Both syntaxes are supported concurrently to enable incremental migration, with updates to compiler queries and internal flags to distinguish between 'type const' and direct const items.

Key Takeaway for Contributors

Incrementally supporting both 'type const' and direct const item syntaxes eases the transition toward GCA, though it creates temporary complexity. The change improves API ergonomics by returning Option types where appropriate and sets the stage for fully replacing 'type const' with direct const items in future Rust versions.