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

Incorrect explicit_outlives_requirements error #119228

Closed
Nadrieril opened this issue Dec 22, 2023 · 0 comments · Fixed by #128908
Closed

Incorrect explicit_outlives_requirements error #119228

Nadrieril opened this issue Dec 22, 2023 · 0 comments · Fixed by #128908
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@Nadrieril
Copy link
Member

Nadrieril commented Dec 22, 2023

Code

playground link

#![warn(explicit_outlives_requirements)]
pub trait TypeCx {
    type Ty;
}

pub struct Pat<Cx: TypeCx> {
    pub ty: Cx::Ty,
}

pub struct MyTypeContext<'thir, 'tcx: 'thir> {
    pub pat: Pat<MyTypeContext<'thir, 'tcx>>,
}

impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContext<'thir, 'tcx> {
    type Ty = ();
}

Current output

warning: outlives requirements can be inferred
 --> compiler/rustc_pattern_analysis/src/lib.rs:9:37
  |
9 | pub struct MyTypeContext<'thir, 'tcx: 'thir> {
  |                                     ^^^^^^^ help: remove this bound
  |
  = note: `-W explicit-outlives-requirements` implied by `-W rust-2018-idioms`
  = help: to override `-W rust-2018-idioms` add `#[allow(explicit_outlives_requirements)]`

Desired output

This warning should not be emitted.

Rationale and extra context

If I remove the outlives bound like the warning suggests, I then get:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'tcx` due to conflicting requirements
  --> compiler/rustc_pattern_analysis/src/lib.rs:10:14
   |
10 |     pub pat: Pat<MyTypeContext<'thir, 'tcx>>,
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the lifetime `'tcx` as defined here...
  --> compiler/rustc_pattern_analysis/src/lib.rs:9:33
   |
9  | pub struct MyTypeContext<'thir, 'tcx> {
   |                                 ^^^^
note: ...but the lifetime must also be valid for the lifetime `'thir` as defined here...
  --> compiler/rustc_pattern_analysis/src/lib.rs:9:26
   |
9  | pub struct MyTypeContext<'thir, 'tcx> {
   |                          ^^^^^
note: ...so that the types are compatible
  --> compiler/rustc_pattern_analysis/src/lib.rs:10:14
   |
10 |     pub pat: Pat<MyTypeContext<'thir, 'tcx>>,
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected `<MyTypeContext<'thir, 'tcx> as TypeCx>`
              found `<MyTypeContext<'_, '_> as TypeCx>`

For more information about this error, try `rustc --explain E0495`.

So the outlives bound not only couldn't be inferred, but was required.

Other cases

If I don't make MyTypeContext recursive, then the bound is not warned as inferrable. This compiles without warnings (playground):

#![warn(explicit_outlives_requirements)]
pub trait TypeCx {
    type Ty;
}

pub struct Pat<Cx: TypeCx> {
    pub ty: Cx::Ty,
}

pub struct MyTypeContext<'thir, 'tcx: 'thir> {
    pub tcx: &'tcx (),
    pub thir: &'thir (),
}

impl<'thir, 'tcx: 'thir> TypeCx for MyTypeContext<'thir, 'tcx> {
    type Ty = ();
}

pub struct OtherContext<'thir, 'tcx> {
    pub pat: Pat<MyTypeContext<'thir, 'tcx>>,
}

And weirdly OtherContext does not require 'tcx: 'thir to compile, whereas the recursive MyTypeContext did. I'm not sure what the correct behavior is but that seems inconsistent.

I just realized that this requires #![warn(explicit_outlives_requirements)] which is turned on when working on rustc but is actually allow-by-default for other rust users.

@Nadrieril Nadrieril 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 Dec 22, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 22, 2023
@saethlin saethlin added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 22, 2023
@bors bors closed this as completed in 0d0265c Aug 10, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 10, 2024
Rollup merge of rust-lang#128908 - notriddle:notriddle/self-inferred-lifetime-bounds, r=compiler-errors

diagnostics: do not warn when a lifetime bound infers itself

Fixes rust-lang#119228
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 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

Successfully merging a pull request may close this issue.

3 participants