Open
Description
I tried this code:
trait Trait {}
impl Trait for ((), ()) {}
fn returns_impl_whatever(condition: bool) -> Result<impl Trait, impl Trait> {
if condition {
Err(((), ()))?;
}
Ok(((), ()))
}
I expected to see this happen: I believe this should compile, both T and E should be inferrable by the compiler.
Instead, this happened:
Compiling impl-whatever v0.1.0 (/private/tmp/impl-whatever)
error[E0282]: type annotations needed
--> src/lib.rs:9:5
|
9 | Ok(((), ()))
| ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
|
help: consider specifying the generic arguments
|
9 | Ok::<((), ()), E>(((), ()))
| +++++++++++++++
For more information about this error, try `rustc --explain E0282`.
error: could not compile `impl-whatever` (lib) due to 1 previous error
I also tried a couple of other things. An explicit return Err(...)
in the branch typechecks fine. If I try something like:
if let Err(e) = Err(((), ())) {
return Err(e);
}
I get an error telling me I need to specify the type of T
. So it appears that it's able to infer either type, but not both at the same time.
Meta
Tried on stable and nightly
rustc --version --verbose
:
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7
rustc 1.87.0-nightly (4d30011f6 2025-03-15)
binary: rustc
commit-hash: 4d30011f6c616be074ba655a75e5d55441232bbb
commit-date: 2025-03-15
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0