Skip to content

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

Open
@alestane

Description

@alestane

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: 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-traitsProject group: Const traitsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions