-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Allow trait methods to be called on concrete types in a const context #68847
Conversation
This comment has been minimized.
This comment has been minimized.
862cb71
to
a24aac4
Compare
fn plus(self, rhs: Self) -> Self; | ||
} | ||
|
||
impl const Plus for i32 { |
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.
Would this test work the same if the Add
trait were used instead of Plus
?
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. I'll add a type with a const impl of Add
to this test. Obviously I can't do this directly on i32
outside the standard library without conflicting impls. Note that, with the current implementation, it's possible to have const unstable trait impls even though it's not possible to have unstable ones. This means we could have const unstable implementations of arithmetic traits for the primitive types. I will try to find an approach to trait solving that preserves this property.
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.
Note that, with the current implementation, it's possible to have const unstable trait impls even though it's not possible to have unstable ones. This means we could have const unstable implementations of arithmetic traits for the primitive types. I will try to find an approach to trait solving that preserves this property.
Oh wow, that would be a neat feature
82a9302
to
475bd1c
Compare
This comment has been minimized.
This comment has been minimized.
475bd1c
to
a28e2f1
Compare
@oli-obk, I'm conflicted about this PR. On the one hand, it allows a lot of useful operations within The next step is to teach the trait solver and method resolution about the constness of impls. This will require a lot of small changes to performance-sensitive code, so it will take a while. |
Yea that's ok. You can mark the feature as partially implemented to make sure users get a warning.
Can you elaborate on the
part? Do you mean |
I thought that |
r=me with a stability test for the above worry |
☔ The latest upstream changes (presumably #69023) made this pull request unmergeable. Please resolve the merge conflicts. |
This seems straight-forward and the use of |
// See if this is a trait method for a concrete type whose impl of that trait is | ||
// `const`. | ||
if self.tcx.features().const_trait_impl { | ||
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs); |
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.
As long as self.param_env
is tcx.param_env(some_def_id)
without calling .with_reveal_all()
or w/e, it should be fine.
f0cc77e
to
cede204
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
cede204
to
a06734d
Compare
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
r=me with tests blessed |
a06734d
to
19801b1
Compare
@bors r=oli-obk |
📌 Commit 19801b1 has been approved by |
☀️ Test successful - checks-azure |
This partially implements RFC 2632 by const-checking methods inside an
impl const
block and allowing those methods to be called on concrete types. Calling trait methods on type parameters in a const context is not yet allowed. Implementing this will require much more work. Since we are only concerned with methods on concrete types, we are able to take advantage of the machinery inInstance::resolve
, which is doing most of the work.This also propagates
#[rustc_const_unstable]
from parent items to child items, making that attribute behave like#[stable]
and#[unstable]
do. This allows trait methods to be marked as unstably const.cc #67792 #57563
cc @rust-lang/wg-const-eval
r? @oli-obk