False-negative type-checking Error and Invalid Suggestion
Open
Nobody has claimed this yet.
A-inference
C-bug
T-types
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
use std::ops::Neg;
struct Foo;
fn foo() -> Result<Foo, Box<dyn std::error::Error>> {
Ok(Foo)
}
impl Neg for Foo {
type Output = Result<Foo, Box<dyn std::error::Error>>;
fn neg(self) -> Self::Output {
Ok(self)
}
}
fn bar() -> Result<Foo, Box<dyn std::error::Error>> {
-foo()?
}
fn main() {
bar().expect("bar should not fail");
}
I expected to see this happen: I receive Result<Foo, E> from foo() and ? converts it to Foo, then i calling neg() from Neg trait and returning Result<Foo, E> from bar()
Instead, this happened: Type-check fails with E0308 error and absolutely invalid suggestion in help section.
Meta
rustc --version --verbose:
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1
LLVM version: 22.1.8
Backtrace
error[E0308]: `?` operator has incompatible types
--> src/main.rs:18:6
|
17 | fn bar() -> Result<Foo, Box<dyn std::error::Error>> {
| --------------------------------------- expected `Result<Foo, Box<(dyn std::error::Error + 'static)>>` because of return type
18 | -foo()?
| ^^^^^^ expected `Result<Foo, Box<dyn Error>>`, found `Foo`
|
= note: `?` operator cannot convert from `Foo` to `Result<Foo, Box<(dyn std::error::Error + 'static)>>`
= note: expected enum `Result<Foo, Box<(dyn std::error::Error + 'static)>>`
found struct `Foo`
help: try removing this `?`
|
18 - -foo()?
18 + -foo()
|
help: try wrapping the expression in `Ok`
|
18 | -Ok(foo()?)
| +++ +
For more information about this error, try `rustc --explain E0308`.
error: could not compile `bug` (bin "bug") due to 1 previous error
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
Run the reproducer in src/main.rs with rustc and inspect the E0308 diagnostic for the -foo()? expression. Trace the compiler's type-checking and diagnostic handling for this case; done means the error remains accurate and the help suggestions no longer propose invalid edits.
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
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100