Open
Description
Minimal example, imagine that we have the following type:
enum MyCow<'a> {
Owned(String),
Borrowed(&'a str),
}
If the user returns Self
variants, this fails:
impl<'a> MyCow<'a> {
fn borrow(&self) -> MyCow<'_> {
match self {
Self::Owned(s) => Self::Borrowed(s),
Self::Borrowed(s) => Self::Borrowed(s),
}
}
fn own(&self) -> MyCow<'static> {
match self {
Self::Owned(s) => Self::Owned(s.clone()),
Self::Borrowed(s) => Self::Owned(s.to_string()),
}
}
}
But the same code works when you change Self
to MyCow
!
impl<'a> MyCow<'a> {
fn borrow(&self) -> MyCow<'_> {
match self {
Self::Owned(s) => MyCow::Borrowed(s),
Self::Borrowed(s) => MyCow::Borrowed(s),
}
}
fn own(&self) -> MyCow<'static> {
match self {
Self::Owned(s) => MyCow::Owned(s.clone()),
Self::Borrowed(s) => MyCow::Owned(s.to_string()),
}
}
}
The real error here is that Self
really represents MyCow<'a>
and not MyCow<'_>
or MyCow<'static>
-, but the errors don't really relay this. Potentially linked issue: #30904 (has been fixed).