Skip to content

Confusing lifetime diagnostics for trait impl #115903

@eduardosm

Description

@eduardosm
Contributor

Code

trait Tr {
    fn func(x: &u8, y: &u16) -> !;
}

impl Tr for () {
    fn func(_x: &u8, _y: &'static u16) -> ! {
        unimplemented!();
    }
}

Current output

error[E0308]: method not compatible with trait
 --> lib.rs:6:5
  |
6 |     fn func(_x: &u8, _y: &'static u16) -> ! {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
  |
  = note: expected signature `fn(&u8, &u16) -> _`
             found signature `fn(&u8, &'static u16) -> _`
note: the anonymous lifetime as defined here...
 --> lib.rs:6:17
  |
6 |     fn func(_x: &u8, _y: &'static u16) -> ! {
  |                 ^
  = note: ...does not necessarily outlive the static lifetime

Desired output

No response

Rationale and extra context

The lifetime mismatch occurs because for y, the lifetime of the trait (anonymous) will not outlive the lifetime of the impl ('static). However, the diagnosis is making reference to (the lifetime of?) x, which does not have anything to do with the mismatch.

If I define an explicit lifetime for x in the impl fn:

note: the anonymous lifetime as defined here...
 --> lib.rs:6:5
  |
6 |     fn func<'a>(_x: &'a u8, _y: &'static u16) -> ! {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...does not necessarily outlive the static lifetime

It refers to some anonymouse lifetime using the span of the whole impl function signature.

Something similar happens if I remove the x argument (from both trait and impl):

note: the anonymous lifetime as defined here...
 --> lib.rs:6:5
  |
6 |     fn func(_y: &'static u16) -> ! {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...does not necessarily outlive the static lifetime

Other cases

No response

Anything else?

I think the issue might be in the msg_span_from_named_region function from compiler/rustc_infer/src/infer/error_reporting/mod.rs:

ty::BoundRegionKind::BrNamed(_, name) => {
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
{
param.span
} else {
tcx.def_span(scope)
};
let text = if name == kw::UnderscoreLifetime {
"the anonymous lifetime as defined here".to_string()
} else {
format!("the lifetime `{name}` as defined here")
};
(text, Some(span))
}

In this case, name is kw::UnderscoreLifetime and generics.get_named(name) will find the span of the first anonymous lifetime. If there is none, it will fallback to the span of scope (the whole function signature).

Activity

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 Sep 16, 2023
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Sep 16, 2023
added
D-confusingDiagnostics: Confusing error or lint that should be reworked.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Sep 17, 2023
eopb

eopb commented on Sep 19, 2023

@eopb
Contributor

@rustbot claim

eopb

eopb commented on Sep 19, 2023

@eopb
Contributor

@rustbot release-assignment

veera-sivarajan

veera-sivarajan commented on Jul 17, 2024

@veera-sivarajan
Contributor

Output on rustc 1.79.0:

error[E0308]: method not compatible with trait
 --> <source>:6:5
  |
6 |     fn func(_x: &u8, _y: &'static u16) -> ! {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
  |
  = note: expected signature `fn(&_, &_) -> _`
             found signature `fn(&_, &'static _) -> _`
note: the anonymous lifetime defined here...
 --> <source>:2:24
  |
2 |     fn func(x: &u8, y: &u16) -> !;
  |                        ^^^^
  = note: ...does not necessarily outlive the static lifetime

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.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

        @eduardosm@eopb@saethlin@veera-sivarajan@rustbot

        Issue actions

          Confusing lifetime diagnostics for trait impl · Issue #115903 · rust-lang/rust