Skip to content

Early/late bound lifetime coercion produces a confusing diagnostic #140896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
parrottq opened this issue May 10, 2025 · 0 comments
Open

Early/late bound lifetime coercion produces a confusing diagnostic #140896

parrottq opened this issue May 10, 2025 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@parrottq
Copy link

parrottq commented May 10, 2025

Code

// https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=965a19cf1d3e124734e44af61178208f

struct A<'a>(&'a ());

impl<'a> A<'a> {
    fn associated(a: &'a (), b: &()) {}
}

fn main() {
    let works_not: for<'a> fn(&'a (), &()) = A::associated; // Error
    let works_not: for<'a, 'b> fn(&'a (), &'b ()) = A::<'_>::associated; // Error
    let works: for<'a> fn(&'a (), &()) = |a, b| A::associated(a, b); // Ok
    let works: for<'a, 'b> fn(&'a (), &'b ()) = |a, b| A::associated(a, b); // Ok
}

Current output

error[E0308]: mismatched types
 --> src\main.rs:8:46
  |
8 |     let works_not: for<'a> fn(&'a (), &()) = A::associated; // Error
  |                    -----------------------   ^^^^^^^^^^^^^ one type is more general than the other
  |                    |
  |                    expected due to this
  |
  = note: expected fn pointer `for<'a, 'b> fn(&'a (), &'b ())`
                found fn item `for<'a> fn(&(), &'a ()) {A::<'_>::associated}`

error[E0308]: mismatched types
 --> src\main.rs:9:53
  |
9 |     let works_not: for<'a, 'b> fn(&'a (), &'b ()) = A::<'_>::associated; // Error
  |                    ------------------------------   ^^^^^^^^^^^^^^^^^^^ one type is more general than the other
  |                    |
  |                    expected due to this
  |
  = note: expected fn pointer `for<'a, 'b> fn(&'a (), &'b ())`
                found fn item `for<'a> fn(&(), &'a ()) {A::<'_>::associated}`

Desired output

error[E0308]: mismatched types
  --> src/main.rs:8:46
   |
10 |     let no_works: for<'a> fn(&'a (), &()) = A::associated; // Error
   |                   -----------------------   ^^^^^^^^^^^^^ one type is more general than the other
   |                   |
   |                   expected due to this
   |
   = note: expected fn pointer `for<'a, 'b> fn(&'a (), &'b ())`
                  found fn item `for<'b> fn(&'1 (), &'b ()) {A::<'1>::associated}`
help: you can convert a early bound lifetime to a late bound using a closure
  --> src/main.rs:8:46
   |
10 |     let no_works: for<'a> fn(&'a (), &()) = |a, b| A::associated(a, b); // Error
   |                                             ++++++              ++++++

Rationale and extra context

Proposed changes (ranked by importance):

  1. Introduce the concept of early/late bounds so that the problem becomes searchable.
  2. Explicitly name the early bound lifetimes ('1) since I didn't really parse the {A::<'_>::associated} part of the fn type as linked/important. I thought that I might just need to add/remove lifetimes.
  3. Suggest using a closure to convert from early bounds to late bounds. (I'm not sure if this always works?)
  4. Don't reuse 'a and 'b in the diagnostic. Using the existing lifetime names instead of "shadowing" the definitions would be nice. This introduced more confusion since I commonly used 'a and 'b when writing lifetimes and it wasn't clear to me that these were different.

In my mind this is is an ideal output but some of these might not be feasible for one reason or another.

Other cases

Rust Version

$ cargo +stable -V -v
cargo 1.86.0 (adf9b6ad1 2025-02-28)
release: 1.86.0
commit-hash: adf9b6ad14cfa10ff680d5806741a144f7163698
commit-date: 2025-02-28
host: x86_64-pc-windows-msvc
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.0-DEV (sys:0.4.79+curl-8.12.0 vendored ssl:Schannel)
os: Windows 10.0.26100 (Windows 11 Professional) [64-bit]

Anything else?

You can see my initial confusion here: #140663.

I would be interested in trying to implement some of these changes.

@parrottq parrottq added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant