Uh oh!
There was an error while loading. Please reload this page.
Allow using generic trait methods in const fn - #79287
Conversation
rust-highfive
commented
Nov 22, 2020
r? @oli-obk (rust_highfive has picked a reviewer for you, use r? to override) |
oli-obk
commented
Nov 22, 2020
This is absolutely great! A few points for clarity:
|
Uh oh!
There was an error while loading. Please reload this page.
jonas-schievink
commented
Nov 22, 2020
I've added more tests, but I'm not sure what you mean by this:
|
oli-obk
commented
Nov 22, 2020
I added some text about the issue of using types that do not @bors r+ |
bors
commented
Nov 22, 2020
📌 Commit cb40684 has been approved by |
RalfJung
commented
Nov 22, 2020
Do we have tests to ensure that this only works when a feature flag is set? |
oli-obk
commented
Nov 22, 2020
We have https://github.com/rust-lang/rust/blob/6af388b25050bca26710be7e4030e17bf6d8d2f7/src/test/ui/rfc-2632-const-trait-impl/feature-gate.rs, but that only checks for impls, not actually using them. |
jonas-schievink
commented
Nov 22, 2020
There are no const impls in libcore/std, so using one currently also requires defining one. I'll add a note to the tracking issue. |
…=oli-obk Allow using generic trait methods in `const fn` Next step for rust-lang#67792, this now also allows code like the following: ```rust struct S; impl const PartialEq for S { fn eq(&self, _: &S) -> bool { true } } const fn equals_self<T: PartialEq>(t: &T) -> bool { *t == *t } pub const EQ: bool = equals_self(&S); ``` This works by threading const-ness of trait predicates through trait selection, in particular through `ParamCandidate`, and exposing it in the resulting `ImplSource`. Since this change makes two bounds `T: Trait` and `T: ?const Trait` that only differ in their const-ness be treated like different bounds, candidate winnowing has been changed to drop the `?const` candidate in favor of the const candidate, to avoid ambiguities when both a const and a non-const bound is present.
…as-schievink Rollup of 10 pull requests Successful merges: - rust-lang#76829 (stabilize const_int_pow) - rust-lang#79080 (MIR visitor: Don't treat debuginfo field access as a use of the struct) - rust-lang#79236 (const_generics: assert resolve hack causes an error) - rust-lang#79287 (Allow using generic trait methods in `const fn`) - rust-lang#79324 (Use Option::and_then instead of open-coding it) - rust-lang#79325 (Reduce boilerplate with the `?` operator) - rust-lang#79330 (Fix typo in comment) - rust-lang#79333 (doc typo) - rust-lang#79337 (Use Option::map instead of open coding it) - rust-lang#79343 (Add my (`@flip1995)` work mail to the mailmap) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
leonardo-m
commented
Nov 24, 2020
I think if you write the keywords in the wrong order "const impl PartialEq for S {" the compiler could give a bit better error message. |
leonardo-m
commented
Nov 24, 2020
See also: #79378 |
…trochenkov Recover on `const impl<> X for Y` `@leonardo-m` mentioned that `const impl Foo for Bar` could be recovered from in rust-lang#79287. I'm not sure about the error strings as they are, I think it should probably be something like the error that `expected_one_of_not_found` makes + the suggestion to flip the keywords, but I'm not sure how exactly to do that. Also, I decided not to try to handle `const unsafe impl` or `unsafe const impl` cause I figured that `unsafe impl const` would be pretty rare anyway (if it's even valid?), and it wouldn't be worth making the code more messy.
usbalbin
commented
Dec 26, 2020
Is something like this supposed to work with this PR? Or is that something which might get implemented in a later stage? :) impl<T,U>constInto<U>forTwhereU:From<T>,{fninto(self) -> U{U::from(self)}}I currently get this error |
oli-obk
commented
Dec 26, 2020
This is unimplemented as of now, but being looked into since around a week |
usbalbin
commented
Dec 26, 2020
Oh, sorry. Thanks :) |
Next step for #67792, this now also allows code like the following:
This works by threading const-ness of trait predicates through trait selection, in particular through
ParamCandidate, and exposing it in the resultingImplSource.Since this change makes two bounds
T: TraitandT: ?const Traitthat only differ in their const-ness be treated like different bounds, candidate winnowing has been changed to drop the?constcandidate in favor of the const candidate, to avoid ambiguities when both a const and a non-const bound is present.