-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Make trait methods callable in const contexts #3762
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
545099b
290d7fe
0c59930
d50c8ef
ff7fabe
64c0773
f7cc5e6
fe97e3f
83f31f7
8d9f649
328655a
fb621b6
73e06e4
dc1cb64
7da95b8
73db002
58343dd
0a82e9e
c1981e4
cb7f02d
bc5cc29
e18f475
fdd11fb
53319c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
~const Trait
bounds can be used
- Loading branch information
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: A lot of the discussion around the language design is happening on Zulip, under the t-lang/effects stream. The latest major proposal discussion can be found at https://rust-lang.zulipchat.com/#narrow/channel/328082-t-lang.2Feffects/topic/All.20that's.20old.20is.20new.20again. It would be nice if before commenting on this RFC one could check if this topic has been discussed on the Zulip stream before :) When there appears to be a consensus forming from community members + the lang team, the proposal should then be updated. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see
#[derive]
'ing being discussed in this RFC, it's notably absent from it and shouldn't be an afterthought in my opinion.Namely, there should be a canonical/conventional mechanism for deriving a "const trait impl" from/for an ADT.
Whether such an impl would be (unconditionally) const, conditionally const, or either depending on a "flag" would be a point for discussion.
It should be quite obvious why you'd want to const-derive a trait and why
#[derive]
itself shouldn't derive "conditionally const trait impls" by default.As for the concrete design, there should be a canonical/conventional way to signal to the (built-in or user-defined) derive macro that a "const" trait impl is requested.
As @petrochenkov noted in rust-lang/rust#118580 (comment), rust-lang/rust#118580 (comment) and rust-lang/rust#118580 (comment) there are three extensible candidates:
Leveraging helper attributes. At call sites, that might look like
I guess. At def sites, it would just look like
That design wouldn't require any additional changes to the language.
Giving derive macros a second input stream. At call sites, that might look like
#[derive(One(const), Two(const))] …
At def sites:
Note that the one-argument form would still exist for backward compatibility. This arity-based dispatch is very much possible for proc macros. See e.g., PR Provide a way for custom derives to know if they were invoked via
#[derive_const]
rust#118580. Of course, we could choose to do something different here but in any case I'd prefer if we didn't force a new edition for this.Passing another input stream via "global data" (which is what e.g.,
Span::call_site()
uses under the hood). At call sites, that might look like#[derive(One(const), Two(const))] …
At def sites:
For context, the compiler currently features the placeholder syntax
#[derive_const]
under the experimental featurederive_const
. Obviously, that syntax is quite awkward and non-extensible (to other potential effects or data in general we might want to pass to proc macros in the future).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the derive syntax should reflect the bound syntax:
#[derive(const Trait)]
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you could elaborate on this. I can think of some reasons, but I want to be sure I've thought of all the reasons.
For a related syntax discussion, see:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsafe
is a property of the derivation operation there, whileconst
is a property of the resulting derivedimpl
here. The syntaxes for each should be different, to reflect that distinction.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, of course. For the sake of argument, let's assume that this RFC establishes the convention for derive macros to generate const trait impls either always or just by default.
const_trait_impl
would naturally continue to generate non-const trait impls until migrated (which takes time) contrary to the ones created after it. This could be perceived as confusing by newcomers and frustrating by seasoned Rust devs.core
/std
) were applied as suddenly said crates would promise more (conditionally) const impls which they can't go back on without a major version update.#[derive(nonconst Trait)]
or similar).Feel free to correct me on / contest any of these points, some of these might be a bit weak or even irrelevant. I threw this together quickly, so yeah.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're sticking with the already implemented
#[derive_const(Trait)]
syntax and leaving it to future RFCs afterunsafe
derives are finalized to come up with something betterUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
derive_const
only works for built-in derive macros though, are you saying that we won't support user-defined const-derives in the foreseeable future (rust-lang/rust#118304 (blocked by design concerns or rather total lack of design))?