Skip to content
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

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 opened this issue Mar 18, 2024 · 4 comments · Fixed by #123122
Assignees
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

@polina4096
Copy link

polina4096 commented Mar 18, 2024

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

@polina4096 polina4096 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 Mar 18, 2024
@polina4096 polina4096 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 Mar 18, 2024
@surechen
Copy link
Contributor

I think this should suggest :

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

Because 'b is bound for T

@surechen
Copy link
Contributor

@rustbot claim

@polina4096
Copy link
Author

polina4096 commented Mar 19, 2024

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>
  |             +++

@fmease
Copy link
Member

fmease commented Mar 19, 2024

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.

surechen added a commit to surechen/rust that referenced this issue Apr 9, 2024
surechen added a commit to surechen/rust that referenced this issue Apr 9, 2024
surechen added a commit to surechen/rust that referenced this issue Apr 9, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 21, 2024
Fix incorrect suggestion for undeclared hrtb lifetimes in where clauses.

For poly-trait-ref like `for<'a> Trait<T>`   in  `T: for<'a> Trait<T> + 'b { }`.
We should merge the hrtb lifetimes: existed `for<'a>` and suggestion `for<'b>` or will get err: [E0316] nested quantification of lifetimes

fixes rust-lang#122714
fmease added a commit to fmease/rust that referenced this issue May 21, 2024
Fix incorrect suggestion for undeclared hrtb lifetimes in where clauses.

For poly-trait-ref like `for<'a> Trait<T>`   in  `T: for<'a> Trait<T> + 'b { }`.
We should merge the hrtb lifetimes: existed `for<'a>` and suggestion `for<'b>` or will get err: [E0316] nested quantification of lifetimes

fixes rust-lang#122714
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 21, 2024
Fix incorrect suggestion for undeclared hrtb lifetimes in where clauses.

For poly-trait-ref like `for<'a> Trait<T>`   in  `T: for<'a> Trait<T> + 'b { }`.
We should merge the hrtb lifetimes: existed `for<'a>` and suggestion `for<'b>` or will get err: [E0316] nested quantification of lifetimes

fixes rust-lang#122714
@bors bors closed this as completed in 4ebbb5f May 21, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 21, 2024
Rollup merge of rust-lang#123122 - surechen:fix_122714, r=fmease

Fix incorrect suggestion for undeclared hrtb lifetimes in where clauses.

For poly-trait-ref like `for<'a> Trait<T>`   in  `T: for<'a> Trait<T> + 'b { }`.
We should merge the hrtb lifetimes: existed `for<'a>` and suggestion `for<'b>` or will get err: [E0316] nested quantification of lifetimes

fixes rust-lang#122714
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

Successfully merging a pull request may close this issue.

3 participants