rust-lang / rust-lang/rust

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

Open
#126,975 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-terse F-const_trait_impl PG-const-traits T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 (6b0f4b5ec 2024-06-24).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided file.rs reproducer with the nightly toolchain version noted in the issue and compare the diagnostic with the expected const behavior. Done means the compiler either accepts the const call using the trait bound or reports the missing ~const Foo requirement with a clear diagnostic consistent with the intended behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.