Repository navigation
Open
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-needs-designThis issue needs exploration and design to see how and if we can fix/implement itThis issue needs exploration and design to see how and if we can fix/implement itF-const_trait_impl`#![feature(const_trait_impl)]``#![feature(const_trait_impl)]`PG-const-traitsProject group: Const traitsProject group: Const traitsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.WG-macrosWorking group: MacrosWorking group: Macros
Description
As part of #67792, #[derive_const] is the current way to indicate that a derive should be impl const Trait rather than impl Trait. However, this only works for built-in macros. It should be possible for end-user proc macros to know if they were invoked with #[derive_const], permitting the derive to emit the appropriate code.
In my opinion, this could be an opaque type provided as an additional parameter to the derive implementation. That type would have methods to obtain constness and any other future extension (i.e. effects).
cc @fee1-dead per this Zulip comment.
@rustbot label +A-proc-macros +C-enhancement +F-const-trait-impl +T-libs-api
Reacted by Michael Goulet, beef, Mateusz Mikuła, Kisaragi and Vladas Zakrevskis
Metadata
Metadata
Assignees
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-trait-systemArea: Trait systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-needs-designThis issue needs exploration and design to see how and if we can fix/implement itThis issue needs exploration and design to see how and if we can fix/implement itF-const_trait_impl`#![feature(const_trait_impl)]``#![feature(const_trait_impl)]`PG-const-traitsProject group: Const traitsProject group: Const traitsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.WG-macrosWorking group: MacrosWorking group: Macros
Activity
fmease commentedon Jul 1, 2024
PR #118580 extends support for
#[derive_const]to user-defined derive proc-macros as outlined in the issue description. However,#[derive_const]itself is very ad-hoc and non-extensible as petrochenkov criticizes, justifiably so:Originally posted by @petrochenkov in #118580 (comment)
Originally posted by @petrochenkov in #118580 (comment)
Originally posted by @petrochenkov in #118580 (comment)
fmease commentedon Jul 1, 2024
I will therefore close the aforementioned PR and block this issue on further design work to be done by PG-const-traitsProject group: Const traits
and likely also
WG-macros
Working group: Macros
.
illicitonion commentedon Jul 17, 2025
It feels like there are three intentions that may be expressible:
constimplconstimplconst, great, if not, I'll take non-constDo we intend to make each of these intentions signal-able? Particularly, will
#[derive]imply 2 or 3? And will there be a way to express the other?oli-obk commentedon Jul 17, 2025
I don't think option 3 is doable without resorting to https://crates.io/crates/whichever-compiles (this is a joke, Option 3 is not possible, there is nothing that allows a proc macro to make a decision like that).
You can manually try a
constderive, if that doesn't work either change sth to make it work or switch to a normal non-const derive.illicitonion commentedon Jul 17, 2025
I think it's more a question of transition from const traits not existing to them existing - let's imagine I have a proc macro which derives an trait that it can always derive in a
constway.Will callers need to explicitly switch from
#[derive]to#[const_derive]in order to get the const version? Should it be an error to#[derive]aconst Trait? Is it ok for a proc macro to give aconstimpl when called with#[derive]?sunipkm commentedon May 11, 2026