-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Experiment: mark derived Clone impls as const #93255
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
9de2ef0
to
5c46ec4
Compare
This comment has been minimized.
This comment has been minimized.
5c46ec4
to
eaef54d
Compare
Even though it currently builds and the test passes, I'm going to leave this marked as a draft since I think it still could use some feedback in terms of how it's done (this is my first time changing any compiler code) and also just, the way it works in general. |
r? @oli-obk |
#[default_method_body_is_const] | ||
fn clone_from(&mut self, source: &Self) | ||
where | ||
Self: ~const Clone + ~const Drop, |
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.
does this show up in rustdoc?
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.
The main problem with this approach is that it is a breaking change for
struct Foo;
impl Clone for Foo {
fn clone(&self) -> Self { Foo }
}
#[derive(Clone)]
struct Bar(Foo);
Thus the only way we can ever really make derives work, is by opting in, meaning something like #[derive(const Clone)]
. This has been discussed in https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/syntax.2Fgrammar.3A.20deriving.20const.20traits
The discussion there isn't yet clear on the syntax, so I'm not sure how to proceed here.
That's very fair; I think that the As you probably noticed in the code itself, what is probably a bigger concern is how to annotate the derived |
eaef54d
to
9fd85da
Compare
This comment has been minimized.
This comment has been minimized.
(Will properly rebase later; am glad the error messages improved.) |
9fd85da
to
3068dcc
Compare
☔ The latest upstream changes (presumably #94787) made this pull request unmergeable. Please resolve the merge conflicts. |
Gonna close since this needs a redesign anyway. |
This is an attempt to lay the foundation for marking
#[derive]
d trait impls as const. Right now, only trivialClone
impls (*self
) are marked as const, but in the future, more complicated impls likePartialEq
andPartialOrd
could be marked asconst
as well.This mostly just modifies the
TraitDef
struct inrustc_builtin_macros
to allow marking traits as const. For now, only theClone
implementations are marked as const since they're the simplest, to prove that the feature works.This also augments the existing
Clone
derive test to check that the generated impls can be const.