-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
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
:
rust/compiler/rustc_infer/src/infer/error_reporting/mod.rs
Lines 230 to 244 in 341ef15
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).