rust-lang / rust-lang/rust

TAIT bug report from tracking issue

Open
#150,685 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

F-impl_trait_in_assoc_type needs-triage T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I found what I believe is a bug, that seems to only trigger when using a closure as the associated type of a trait.
The following does not compile:

pub trait Bind {
    type Arg;
    type OutFn;

    fn bind<'a>(self, arg: Self::Arg) -> Self::OutFn
    where
        Self: 'a,
        Self::Arg: 'a,
        Self::OutFn: 'a,
        Self::Arg: Clone;
}

impl<Arg, Out> Bind for fn(Arg) -> Out {
    type Arg = Arg;
    type OutFn = impl Fn() -> Out;

    fn bind<'a>(self, arg: Self::Arg) -> Self::OutFn
    where
        Self: 'a,
        Self::OutFn: 'a,
        Self::Arg: 'a,
        Self::Arg: Clone,
    {
        move || self(arg.clone())
    }
}

The compiler produces the output:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Arg: Clone` is not satisfied
  --> src/main.rs:28:9
   |
28 |         move || self(arg.clone())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `Arg`
   |
note: required by a bound in `<fn(Arg) -> Out as Bind>::bind`
  --> src/main.rs:26:20
   |
21 |     fn bind<'a>(self, arg: Self::Arg) -> Self::OutFn
   |        ---- required by a bound in this associated function
...
26 |         Self::Arg: Clone,
   |                    ^^^^^ required by this bound in `<fn(Arg) -> Out as Bind>::bind`
help: consider restricting type parameter `Arg` with trait `Clone`
   |
17 | impl<Arg: std::clone::Clone, Out> Bind for fn(Arg) -> Out {
   |         +++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=3e6d260f4ed442040fabce0bdebc8e9f

Originally posted by @Houtamelo in #63063

cc @software-artificer

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 running the linked nightly Playground example and reproduce the E0277 diagnostic shown at src/main.rs. Trace how the compiler handles the closure associated type and the Arg: Clone bound; done means the minimized example behaves as intended and the regression is covered by an appropriate compiler test.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.