-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Copy link
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.F-const_trait_impl`#![feature(const_trait_impl)]``#![feature(const_trait_impl)]`PG-const-traitsProject group: Const traitsProject group: Const traitsS-blockedStatus: Blocked on something else such as an RFC or other implementation work.Status: Blocked on something else such as an RFC or other implementation work.T-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-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API 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
compiler-errors, fee1-dead, mati865, KisaragiEffective and VladasZ
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.F-const_trait_impl`#![feature(const_trait_impl)]``#![feature(const_trait_impl)]`PG-const-traitsProject group: Const traitsProject group: Const traitsS-blockedStatus: Blocked on something else such as an RFC or other implementation work.Status: Blocked on something else such as an RFC or other implementation work.T-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-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.WG-macrosWorking group: MacrosWorking group: Macros
Activity
impl const Trait for Ty
and[const]
(conditionally const) syntax (const_trait_impl
) #67792#[derive_const]
#118580fmease 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:
const
implconst
implconst
, great, if not, I'll take non-const
Do 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
const
derive, 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
const
way.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 aconst
impl when called with#[derive]
?