Skip to content

Diagnostics: Nested higher-ranked lifetimes are incorrectly printed #111365

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
fmease opened this issue May 8, 2023 · 1 comment
Open

Diagnostics: Nested higher-ranked lifetimes are incorrectly printed #111365

fmease opened this issue May 8, 2023 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions A-pretty Area: Pretty printing (including `-Z unpretty`) D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented May 8, 2023

For

type One = fn(HelperOne);
type HelperOne = for<'a> fn(&'a (), &'a ());

type Two = for<'o> fn(HelperTwo<'o>);
type HelperTwo<'x> = for<'a> fn(&'a (), &'x ());

fn accept(x: One) -> Two { x }

the output is

error[E0308]: mismatched types
 --> src/lib.rs:7:28
  |
7 | fn accept(x: One) -> Two { x }
  |                      ---   ^ one type is more general than the other
  |                      |
  |                      expected `for<'o> fn(for<'a> fn(&'a (), &'o ()))` because of return type
  |
  = note: expected fn pointer `for<'o> fn(for<'o, 'a> fn(&'a (), &'o ()))`
             found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`

Here the type for<'o> fn(for<'o, 'a> fn(&'a (), &'o ())) shown after “expected fn pointer” contains
for<'o, 'a> which doesn't make sense. The binder contains the lifetime bound in the outer binder.
The inner binder should just be for<'a> just like in the type shown in the label.
Note that this doesn't happen when -Zverbose is passed 1.

Further, if we change the definition of Two to be type Two = for<'a> fn(HelperTwo<'a>); ('o => 'a),
then the pretty-printer does not try to avoid shadowing through renaming of one of the lifetimes or
through other means. Output:

error[E0308]: mismatched types
 --> src/lib.rs:7:28
  |
7 | fn accept(x: One) -> Two { x }
  |                      ---   ^ one type is more general than the other
  |                      |
  |                      expected `for<'a> fn(for<'a> fn(&'a (), &'a ()))` because of return type
  |
  = note: expected fn pointer `for<'a> fn(for<'a, 'a> fn(&'a (), &'a ()))`
             found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`

Ideally it should look something like

  error[E0308]: mismatched types
   --> src/lib.rs:7:28
    |
  7 | fn accept(x: One) -> Two { x }
    |                      ---   ^ one type is more general than the other
    |                      |
-   |                      expected `for<'a> fn(for<'a> fn(&'a (), &'a ()))` because of return type
+   |                      expected `for<'r> fn(for<'a> fn(&'a (), &'r ()))` because of return type
    |
-   = note: expected fn pointer `for<'a> fn(for<'a, 'a> fn(&'a (), &'a ()))`
+   = note: expected fn pointer `for<'r> fn(for<'a> fn(&'a (), &'r ()))`
               found fn pointer `fn(for<'a> fn(&'a (), &'a ()))`

Here is a playground for a smaller reproducer of the last issue. In this case, the compiler could either
perform renaming or it could omit the inner binder entirely since the lifetime it binds is not referenced
in the inner type.

I've looked through all relevant A-diagnostics A-lifetimes issues and through some others and couldn't find anything that matches 100% what I was looking for (out of those maybe kinda #29061, #56423, #73457, #92281 but not really).

@rustbot label T-compiler A-diagnostics A-lifetimes D-incorrect

Footnotes

  1. It's for<Region(BrNamed(DefId(0:7 ~ repro[8603]::Two::'o), 'o))> fn(for<Region(BrNamed(DefId(0:10 ~ repro[8603]::HelperTwo::'a), 'a))> fn(..., ...)).

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 8, 2023
@compiler-errors
Copy link
Member

Yeah, this has to do with some irresponsible binder-skipping in TypeErrCtxt::cmp I'm pretty sure.

@fmease fmease self-assigned this Oct 4, 2023
@fmease fmease added A-pretty Area: Pretty printing (including `-Z unpretty`) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) labels Sep 24, 2024
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 A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions A-pretty Area: Pretty printing (including `-Z unpretty`) D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

3 participants