Open
Description
Seems like lifetime elision doesn't work quite right with nested self receivers. The error messages are also misleading. Of course this can be worked around by providing the annotation in the code
use std::rc::Rc;
struct S {
val: String,
}
impl S {
// error[E0623]: lifetime mismatch
// --> src/lib.rs:9:9
// |
// 8 | fn rc1(self: &Rc<Self>, _s: &str) -> &str {
// | --------- ----
// | |
// | this parameter and the return type are declared with different lifetimes...
// 9 | self.val.as_ref()
// | ^^^^^^^^^^^^^^^^^ ...but data from `self` is returned here
fn rc1(self: &Rc<Self>, _s: &str) -> &str {
self.val.as_ref()
}
// error[E0106]: missing lifetime specifier
// --> src/lib.rs:11:32
// |
// 11 | fn rc2(self: &Rc<Self>) -> &str {
// | ^ help: consider giving it a 'static lifetime: `&'static`
// |
// = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
fn rc2(self: &Rc<Self>) -> &str {
self.val.as_ref()
}
// Compiles fine
fn ref1(&self, _s: &str) -> &str {
self.val.as_ref()
}
// Compiles fine
fn ref2(&self) -> &str {
self.val.as_ref()
}
}