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
@polina4096

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

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