Closed
Description
I tried this code:
#![feature(generic_associated_types)]
pub trait Trait {
type Assoc<'a> where Self: 'a;
fn f(&self) -> Self::Assoc<'_>;
}
pub struct Struct {
item: f32
}
pub struct GenericStruct<'a> {
ref_item: &'a f32
}
impl Trait for Struct {
type Assoc<'a> = GenericStruct<'a>;
fn f(&self) -> Self::Assoc<'_> {
// This doesn't work
Self::Assoc {
ref_item: &self.item
}
// This doesn't work
// Self::Assoc<'_> {
// ref_item: &self.item
// }
// This works, but concrete name is not always readily available
// GenericStruct {
// ref_item: &self.item
// }
// This works
// type Fix<'a> = <Struct as Trait>::Assoc<'a>;
// Fix {
// ref_item: &self.item
// }
}
}
I expected to see this happen: the lifetime is correctly inferred when using
Self::Assoc {
ref_item: &self.item
}
Instead, this happened: I have to use one of the work arounds described in the linked code.
Swap from GAT lifetime to the trait parameterized by a lifetime it works:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=551e676af610e612a7627661d6c5ca05
Meta
rustc --version --verbose
:
rustc 1.60.0-nightly (1409c015b 2022-01-11)
binary: rustc
commit-hash: 1409c015b44a4d4d38bef2250b2a37c17b8b7463
commit-date: 2022-01-11
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0