Closed
Description
Given the following code (Playground):
trait Trait {
type Associated;
}
fn returns_opaque() -> impl Trait + 'static {
struct Impl;
impl Trait for Impl {
type Associated = ();
}
Impl
}
fn accepts_trait<T>(_: T)
where
T: Trait<Associated = ()>,
{
}
fn main() {
accepts_trait(returns_opaque());
}
The current output is:
Compiling playground v0.0.1 (/playground)
error[E0271]: type mismatch resolving `<impl Trait as Trait>::Associated == ()`
--> src/main.rs:22:5
|
5 | fn returns_opaque() -> impl Trait + 'static {
| -------------------- the found opaque type
...
15 | fn accepts_trait<T>(_: T)
| ------------- required by a bound in this
16 | where
17 | T: Trait<Associated = ()>,
| --------------- required by this bound in `accepts_trait`
...
22 | accepts_trait(returns_opaque());
| ^^^^^^^^^^^^^ expected `()`, found associated type
|
= note: expected unit type `()`
found associated type `<impl Trait as Trait>::Associated`
help: consider constraining the associated type `<impl Trait as Trait>::Associated` to `()`
|
5 | fn returns_opaque() -> impl Trait + 'static<Associated = ()> {
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
The syntax impl Trait + 'static<Associated = ()>
does not actually work.
Ideally the suggested fix should look like:
help: consider constraining the associated type `<impl Trait as Trait>::Associated` to `()`
|
5 | fn returns_opaque() -> impl Trait<Associated = ()> + 'static {
| ^^^^^^^^^^^^^^^^^
This bug appears to be present in stable, beta, and nightly, on both Rust 2015 and 2018.