Skip to content
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

const fn in Trait implementation. #71971

Closed
ldm0 opened this issue May 7, 2020 · 2 comments
Closed

const fn in Trait implementation. #71971

ldm0 opened this issue May 7, 2020 · 2 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) F-const_trait_impl `#![feature(const_trait_impl)]`

Comments

@ldm0
Copy link
Contributor

ldm0 commented May 7, 2020

For code like this:

#[derive(Debug, Clone, Copy)]
pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl std::ops::Add<Vec3> for Vec3 {
    type Output = Vec3;
    const fn add(self, b: Vec3) -> Self::Output {
        Vec3 {
            x: self.x + b.x,
            y: self.y + b.y,
            z: self.z + b.z,
        }
    }
}

This currently emits:

error[E0379]: functions in traits cannot be declared const                                                                                                                                                  
  --> src\main.rs:10:5
   |
10 |     const fn add(self, b: Vec3) -> Self::Output {
   |     ^^^^^ functions in traits cannot be const

error: aborting due to previous error                                                                                                                                                                       

For more information about this error, try `rustc --explain E0379`.

It would be extremely useful if we could have const fn in trait implementation (especially for operator overloading). And I saw this is also mentioned in the RFC 0911.

@csmoe csmoe added A-const-fn F-const_trait_impl `#![feature(const_trait_impl)]` labels May 7, 2020
@ldm0
Copy link
Contributor Author

ldm0 commented May 7, 2020

Interestingly, I found the #68847 and it has been merged. So now we can have code below compiles:

#![feature(const_fn)]
#![feature(const_trait_impl)]
#[derive(Debug, Clone, Copy)]
pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl const std::ops::Add<Vec3> for Vec3 {
    type Output = Vec3;
    fn add(self, b: Vec3) -> Self::Output {
        Vec3 {
            x: self.x + b.x,
            y: self.y + b.y,
            z: self.z + b.z,
        }
    }
}

But that have not directly solves the problem in this issue. Since some trait may contains more than one functions and only some of them are able to be const function.

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented May 7, 2020

@ldm0, rust-lang/rfcs#2632 is still actively being designed. Perhaps you'd like to participate in that discussion?

I'll close this since I don't think it's actionable. If the initial implementation is not working as the RFC specifies, feel free to reopen.

facebook-github-bot pushed a commit to facebook/sapling that referenced this issue Sep 2, 2022
Summary:
This allows constructing Bytes or Text statically. Unfortunately traits cannot
have const_fn for now: rust-lang/rust#71971
so we manually write out `from_static` for both types.

Differential Revision: D39041222

fbshipit-source-id: 9b2312cfd415c1276485a4c2b4a4216db9c02a64
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) F-const_trait_impl `#![feature(const_trait_impl)]`
Projects
None yet
Development

No branches or pull requests

4 participants