rust-lang / rust-lang/rust

The expected type vs. found type in the error `one type is more general than the other`

Open
#127,800 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#![feature(unboxed_closures)]
#![feature(fn_traits)]

struct A;
impl<'l> FnOnce<(&'l i32,)> for A{
    type Output = &'l i32;
    extern "rust-call" fn call_once(self, x: (&'l i32,))->Self::Output{
        x.0
    }
}
fn show<'a,F,>(_:F)
where F: for<'b> FnOnce(&'b i32)->&'a i32
{
    
}

fn main(){
    show(A);
}

Current output
error[E0308]: mismatched types
  --> src/main.rs:18:5
   |
18 |     show(A);
   |     ^^^^^^^ one type is more general than the other
   |
   = note: expected reference `&'b _`
              found reference `&_`
note: the lifetime requirement is introduced here
  --> src/main.rs:12:35
   |
12 | where F: for<'b> FnOnce(&'b i32)->&'a i32
   |                                   ^^^^^^^
Desired output
expected reference `&'a _`
found reference `&'b _`
Rationale and extra context

The trait bound where F: for<'b> FnOnce(&'b i32)->&'a i32 means we expect that the parameter is &'b i32 for any lifetime 'b and the returned type is &a i32 for a specific lifetime 'a, the implementation of FnOnce for A means the parameter and returned are &'l i32 for any lifetime 'l.

That is, for passing A, it implements for<'b> Fn(&'b i32)->&'b i32, and the trait bound expresses that we expect for<'b> Fn(&'b i32)->&'a i32

Other cases

No response

Rust Version
1.81.0-nightly
Anything else?

No response

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

Reproduce the example from src/main.rs with Rust 1.81.0-nightly and inspect the diagnostic produced at show(A), especially the FnOnce trait bound in show. Compare the current and desired expected/found lifetime labels; done means the diagnostic reports the intended lifetimes without regressing the relevant compiler tests.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.