Closed
Description
I tried this code:
struct S<'a> {
x: &'a str
}
impl S<'static> {
fn f(self) {}
}
impl<'a> S<'a> {
fn f1() -> S<'static> {
Self{x: ""} // error[E0308]: mismatched types
}
fn f2() -> S<'static> {
S{x: ""}
}
fn g1() {
S::f(Self{x: ""}) // error[E0308]: mismatched types
}
fn g2() {
S::f(S{x: ""})
}
}
fn main() {}
I expect this code to compile without error because I would assume that Self
is equivalent to S
in this case. I expect this equivalence because Self
is used as an alias of S
in the body of a function and lifetimes cannot be annotated in function bodies (edit: this turns out to be incorrect, see below).
Instead, this happened:
error[E0308]: mismatched types
--> test.rs:11:9
|
11 | Self{x: ""} // error[E0308]: mismatched types
| ^^^^^^^^^^^ lifetime mismatch
|
= note: expected struct `S<'static>`
found struct `S<'a>`
note: the lifetime `'a` as defined on the impl at 9:6...
--> test.rs:9:6
|
9 | impl<'a> S<'a> {
| ^^
= note: ...does not necessarily outlive the static lifetime
error[E0308]: mismatched types
--> test.rs:18:14
|
18 | S::f(Self{x: ""}) // error[E0308]: mismatched types
| ^^^^^^^^^^^ lifetime mismatch
|
= note: expected struct `S<'static>`
found struct `S<'a>`
note: the lifetime `'a` as defined on the impl at 9:6...
--> test.rs:9:6
|
9 | impl<'a> S<'a> {
| ^^
= note: ...does not necessarily outlive the static lifetime
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.
Meta
rustc --version --verbose
:
rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0
Same behaviour with the nightly version of Rust:
rustc 1.44.0-nightly (1057dc97a 2020-03-20)
binary: rustc
commit-hash: 1057dc97afce39ff6a224966ece3ed438af4c1f5
commit-date: 2020-03-20
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0
Probably related to: #69224