rust-lang / rust-lang/rust

error[E0275]: overflow evaluating the requirement `<RefTFamily<T> as LifetimizedOutputFamily>::Output<'a> == _`

Open
#130,728 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-GATs C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I was trying to implement a generic SelfBorrowingClosure based on https://users.rust-lang.org/t/why-closure-cannot-return-a-reference-to-data-moved-into-closure/72655/6 but got a problem compiling the following piece of code:

pub trait LifetimizedOutputFamily {
    type Output<'a>
    where 
        Self::Output<'a>: 'a;
}

pub struct RefTFamily<T>(core::marker::PhantomData<*const T>);

impl<T> LifetimizedOutputFamily for RefTFamily<T>
{
    type Output<'a> = &'a T
    where
        T: 'a;
}

pub struct SelfBorrowingClosure<State, F, OutputFam>
where
    OutputFam: LifetimizedOutputFamily,
    F: Fn(&'_ State) -> <OutputFam as LifetimizedOutputFamily>::Output<'_>,
{
    pub state: State,
    pub call: F,
    pub output_family: core::marker::PhantomData<*const OutputFam>,
}

impl<State, F, OutputFam> SelfBorrowingClosure<State, F, OutputFam>
where
    OutputFam: LifetimizedOutputFamily,
    F: Fn(&'_ State) -> <OutputFam as LifetimizedOutputFamily>::Output<'_>,
{
    pub fn new(state: State, call: F) -> Self {
        Self {
            state,
            call,
            output_family: core::marker::PhantomData,
        }
    }

    pub fn call(&self) -> <OutputFam as LifetimizedOutputFamily>::Output<'_> {
        (self.call)(&self.state)
    }
}

Minimal Reproducible Example (MRE):

pub trait LifetimizedOutputFamily {
    type Output<'a>
    where 
        Self::Output<'a>: 'a;
}

pub struct RefTFamily<T>(core::marker::PhantomData<*const T>);

impl<T> LifetimizedOutputFamily for RefTFamily<T>
{
    type Output<'a> = &'a T
    where
        T: 'a;
}

Error:

error[E0275]: overflow evaluating the requirement `<RefTFamily<T> as LifetimizedOutputFamily>::Output<'a> == _`
  --> src\lib.rs:11:5
   |
11 |     type Output<'a> = &'a T
   |     ^^^^^^^^^^^^^^^

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 reproducing the Minimal Reproducible Example in src/lib.rs and inspect the compiler output at the reported type alias line. Trace how the LifetimizedOutputFamily implementation evaluates Output<'a>; done means establishing and implementing the appropriate compiler behavior for this overflow, with regression coverage if the relevant test location is identified.

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.