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

Terse diagnostic on lack of ~const bound in inherent associated const functions #126975

Open
alestane opened this issue Jun 26, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-const_trait_impl `#![feature(const_trait_impl)]` PG-const-traits Project group: Const traits T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alestane
Copy link

alestane commented Jun 26, 2024

When a trait is used as a bounds to a generic inherent implementation, and that inherent implementation defines a const function, methods from that bounding trait are not being handled as const inside that definition.

I tried this code:

#![feature(const_trait_impl)]

#[const_trait]
trait Foo {
  fn bar(&self) -> u16;
}

struct Baz<F: Foo>(F, u16);

impl<F: Foo> Baz<F> {
  const fn bat(f: F) -> u16 { f.bar() }
}

I expected to see this happen: The code would compile and use the const bar() method implemented on F.

Instead, this happened: The compiler yields the error:

error[E0277]: the trait bound `F: ~const Foo` is not satisfied
  --> file.rs:11:30
   |
11 |   const fn bat(f: F) -> u16 { f.bar() }
   |                               ^^^^^^^

Meta

This was tested on rustc 1.81.0-nightly (6b0f4b5 2024-06-24).

@alestane alestane added the C-bug Category: This is a bug. label Jun 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 26, 2024
@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints F-effects `#![feature(effects)]` PG-const-traits Project group: Const traits T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 26, 2024
@fmease
Copy link
Member

fmease commented Jun 26, 2024

The bound F: Foo in impl<F: Foo> Baz<F> { is non-const. You need to add a const-if-const bound F: ~const Foo ... to the method itself, not to the impl block (#119059, rust-lang/project-const-traits#11).

Once things are semi-working again, the code should look like the following (which won't necessarily be the final design):

#![feature(freeze, const_trait_impl)]

use std::marker::{Destruct, Freeze};

#[const_trait]
trait Foo {
    fn bar(&self) -> u16;
}

struct Baz<F: Foo>(F, u16);

impl<F: Foo> Baz<F> {
    const fn bat(f: F) -> u16
    where
        F: ~const Foo + ~const Destruct + Freeze,
    {
        f.bar()
    }
}

@fmease
Copy link
Member

fmease commented Jun 26, 2024

In the meantime, you could make it work by replacing ~const Destruct (from my snippet above) with Copy. That's more restrictive of course but at the time of writing Destruct is no longer a const trait while we're reworking things. This compiles and should work as expected:

#![feature(freeze, const_trait_impl)]

impl<F: Foo> Baz<F> {
    const fn bat(f: F) -> u16
    where
        F: ~const Foo + Copy + Freeze,
    {
        f.bar()
    }
}

@fmease fmease added F-const_trait_impl `#![feature(const_trait_impl)]` D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed F-effects `#![feature(effects)]` labels Nov 15, 2024
@fmease fmease changed the title const Traits not being handled inside const associated functions/methods Terse diagnostic on lack of ~const bound in inherent associated const functions Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. F-const_trait_impl `#![feature(const_trait_impl)]` PG-const-traits Project group: Const traits T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants