Description
The message "expected type impl Trait
(opaque type), found type impl Trait
(opaque type)" sounds like a paradox: the compiler has found exactly what it expected, and yet it doesn't match.
The explanatory text of E0308 is the same as for all other banal type errors, so it's not addressing this problem specifically.
From the questions I'm seeing in the user forum I have an impression that users think impl Trait
is like dyn Trait
and try to use it as an abstraction for mixing different types.
fn f1() -> impl Copy {
"1"
}
fn f2() -> impl Copy {
2
}
fn main() {
let mut x = f1();
x = f2();
}
10 | x = f2();
| ^^^^ expected opaque type, found a different opaque type
|
= note: expected typeimpl std::marker::Copy
(opaque type)
found typeimpl std::marker::Copy
(opaque type)
Suggestions:
-
Make it a separate error, so that it'll get its own
rustc --explain
document. -
If possible, instead of "(opaque type)" print more internal type information, e.g. like for closures:
= note: expected type `impl [std::marker::Copy@src/main.rs:2:10]` found type `impl [std::marker::Copy@src/main.rs:5:10]` = note: each `impl Trait`, even for the same trait, is a different type. = help: consider using `Box<dyn Trait>` or separate generic parameters.