Skip to content

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

Open
@fmease

Description

@fmease
Member

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(..., ...)).

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-lifetimesArea: Lifetimes / regions
D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on May 8, 2023
compiler-errors

compiler-errors commented on May 8, 2023

@compiler-errors
Member

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

self-assigned this
on Oct 4, 2023
added
A-prettyArea: Pretty printing (including `-Z unpretty`)
A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)
on Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-lifetimesArea: Lifetimes / regionsA-prettyArea: Pretty printing (including `-Z unpretty`)D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @compiler-errors@fmease@rustbot

      Issue actions

        Diagnostics: Nested higher-ranked lifetimes are incorrectly printed · Issue #111365 · rust-lang/rust