Open
Description
I tried this code:
#![feature(type_alias_impl_trait)]
trait Foo {}
impl<T: Foo> Foo for &T {}
trait Bar {
// I need this associated type to be implemented separately.
// This is why I didn't put it inside `Baz` definition.
type Bar<'a>: Foo;
}
trait Baz: Bar {
fn baz<'a>(&'a self) -> Self::Bar<'a>;
}
// Problem:
struct Qux<T: Foo> {
field: T,
}
impl<T: Foo> Bar for Qux<T> {
type Bar<'a> = impl Foo + 'a; // Error: unconstrained opaque type.
// `Bar` must be used in combination with a concrete type within
// the same module.
}
impl<T: Foo> Baz for Qux<T> {
fn baz<'a>(&'a self) -> Self::Bar<'a> {
&self.field // Error: expected opaque type `<Qux<T> as Bar>::Bar<'a>`.
// found reference `&T`
}
}
fn main() {}
I got the exact errors visible in the comments.
I expected this to compile, but it didn't.
I found a following workaround:
trait Bar {
type Bar<'a>: Foo where Self: 'a; // <--
}
// ...
struct Qux<T: Foo> {
field: T,
}
type Temp<'a, T: Foo + 'a> = impl Foo + 'a; // <--
impl<T: Foo> Bar for Qux<T> {
type Bar<'a> = Temp<'a, T> where Self : 'a; // <--
}
impl<T: Foo> Baz for Qux<T> {
fn baz<'a>(&'a self) -> Self::Bar<'a> {
&self.field
}
}
Meta
rustc --version --verbose
:
rustc 1.65.0-nightly (cf9ed0dd5 2022-09-15)
binary: rustc
commit-hash: cf9ed0dd5836201843d28bbad50abfbe1913af2a
commit-date: 2022-09-15
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 15.0.0
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Can do after stabilization