Skip to content

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

@tsionyx

Description

@tsionyx

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.

Metadata

Metadata

Assignees

Labels

A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions