Skip to content

suggested fix for undeclared hrtb lifetimes in where clauses in produces error "[E0316] nested quantification of lifetimes" when specified just before the trait #122714

Closed

Description

@polina4096

Code

trait Trait<T>
  where T: for<'a> Trait<T> + 'b { }

Current output

error[E0261]: use of undeclared lifetime name `'b`
 --> src/lib.rs:2:31
  |
2 |   where T: for<'a> Trait<T> + 'b { }
  |                               ^^ undeclared lifetime
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'b` lifetime
  |
2 |   where for<'b> T: for<'a> Trait<T> + 'b { }
  |         +++++++
help: consider introducing lifetime `'b` here
  |
1 | trait Trait<'b, T>
  |             +++

Desired output

error[E0261]: use of undeclared lifetime name `'b`
 --> src/lib.rs:2:31
  |
2 |   where T: for<'a> Trait<T> + 'b { }
  |                               ^^ undeclared lifetime
  |
  = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'b` lifetime
  |
2 |   where T: for<'a, 'b> Trait<T> + 'b { }
  |                  ++++
help: consider introducing lifetime `'b` here
  |
1 | trait Trait<'b, T>
  |             +++

Rationale and extra context

HRTB lifetimes can be put not only before the constrained type (for<...> T: ...), but also just before the constraining trait (T: for<...> Trait ...). When in this position, compiler suggests code which produces error "[E0316] nested quantification of lifetimes" as it ignores the existing HRTB just before the trait and only looks at constraint HRTB.

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: aarch64-apple-darwin
release: 1.76.0
LLVM version: 17.0.6

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 18, 2024
  2. changed the title [-]suggested fix for undeclared hrtb lifetimes in where clause in produces error "[E0316] nested quantification of lifetimes" when specified just before the trait[/-] [+]suggested fix for undeclared hrtb lifetimes in where clauses in produces error "[E0316] nested quantification of lifetimes" when specified just before the trait[/+] on Mar 18, 2024
  3. surechen commented on Mar 19, 2024

    @surechen
    Contributor

    I think this should suggest :

    trait Trait<'b, T>
      where T: for<'a> Trait<'b, T> + 'b { }

    Because 'b is bound for T

  4. surechen commented on Mar 19, 2024

    @surechen
    Contributor

    @rustbot claim

  5. polina4096 commented on Mar 19, 2024

    @polina4096
    Author

    I think this should suggest :

    trait Trait<'b, T>
      where T: for<'a> Trait<'b, T> + 'b { }

    Because 'b is bound for T

    It already does suggest that, I think. There are two suggestions.

    help: consider making the bound lifetime-generic with a new `'b` lifetime
      |
    2 |   where T: for<'a, 'b> Trait<T> + 'b { }
      |                  ++++
    help: consider introducing lifetime `'b` here
      |
    1 | trait Trait<'b, T>
      |             +++
    
  6. fmease commented on Mar 19, 2024

    @fmease
    Member

    Desired output

    2 |   where T: for<'a, 'b> Trait<T> + 'b { }
      |                  ++++
    

    where T: for<'a, 'b> Trait<T> + 'b isn't valid either tho b/c for<> after : only universally quantifies (i.e., introduces universal parameters for) the trait ref immediately following the binder meaning 'b in + 'b is still undefined / unbound.

    We should keep the preexisting trait Trait<'b, T> suggestion (which can lead to follow-up errors unfortunately) and maybe suggest:

    - where T: for<'a> Trait<T> + 'b
    + where for<'a, 'b> T: Trait<T> + 'b

    removing the inner binder (which might be a bit harder to do). The outer binder to the left of the : quantifies the entire bound.

  7. added 4 commits that reference this issue on Mar 27, 2024
    b186d4c
    c864852
    2941db4
    db9b023
  8. added 3 commits that reference this issue on Apr 9, 2024
    e704d5f
    cfe5a1e
    52b1ed0
  9. added 5 commits that reference this issue on Apr 28, 2024
    8aad60d
    98565be
    341b9c6
    8e42b2e
    82f9833
  10. added a commit that references this issue on May 20, 2024
    4ebbb5f
  11. added a commit that references this issue on May 21, 2024
    48f0e23
  12. added a commit that references this issue on May 21, 2024
    43bf82a
  13. added a commit that references this issue on May 21, 2024
    6009cb7
  14. added a commit that references this issue on May 21, 2024
    22a1275
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 lintsT-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

    Issue actions