rust-lang / rust-lang/rust

Unary operator applied to block fails to compile with type mismatch `<T as UnaryOp>::Output != T`

Open
#151,202 2 comments 0 reactions 1 assignee View on GitHub

@dianne is already working on this.

Since Jan 17, 2026.

A-inference C-bug needs-triage T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Providing the unary operator (! or -) for a type T with the Output of different type results in inability to apply
this operator to the block returning this T (the compiler expects the value of type <T as UnaryOp>::Output be the return type of the block instead).

I tried this code:

struct Foo;

// the same problem persists for core::ops::Not
impl core::ops::Neg for Foo {
    type Output = u8;
    
    fn neg(self) -> Self::Output {
        42
    }
}

fn main() {
    let x: u8 = core::ops::Neg::neg({
        println!("Entered block in explicit function call");
        Foo
    });
    println!("{x}");
    
    let x: u8 = -{
        println!("Entered block in sugared operator call");
        Foo
    };
}

The first usage with operator in its explicit function form has no problem, while the second one fails with the error[E0308]: mismatched types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.