Can't infer both sides of a Result when using ?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproduction in src/lib.rs and run cargo check on stable and nightly, as in the report. Compare the inference and diagnostic behavior around Result, impl Trait, and the ? operator; done means the reported example is handled consistently with the stated inference expectation or the limitation is clearly characterized.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100