Skip to content

== expression with unsafe block on left-hand side fails to parse #74854

Open
@jmagnuson

Description

@jmagnuson

I tried this code (playground):

unsafe fn foo() -> usize {
    0
}

fn foo_wrapper() -> bool {
    // Fails to compile
    unsafe { foo() } == 0
    
    // OK
    // unsafe { foo() }.eq(&0)
}

fn main() {
    let maybe = foo_wrapper();
    
    println!("{}", maybe);
}

I expected to see this happen: The result of foo() would return from the unsafe block and be compared to 0.

Instead, this happened: Compilation failed because the parser wanted type () instead of the expected usize for comparison to 0.

error: expected expression, found `==`
 --> src/main.rs:7:22
  |
7 |     unsafe { foo() } == 0
  |                      ^^ expected expression

error[E0308]: mismatched types
 --> src/main.rs:7:14
  |
7 |     unsafe { foo() } == 0
  |              ^^^^^- help: try adding a semicolon: `;`
  |              |
  |              expected `()`, found `usize`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

Incidentally, compilation is successful if the unsafe block is instead on the right-hand side:

0 == unsafe { foo() }

Meta

rustc --version --verbose:

rustc 1.45.0 (5c1f21c3b 2020-07-13)
binary: rustc
commit-hash: 5c1f21c3b82297671ad3ae1e8c942d2ca92e84f2
commit-date: 2020-07-13
host: x86_64-unknown-linux-gnu
release: 1.45.0
LLVM version: 10.0

(the behavior is identical with 1.46.0-beta.2 and 1.47.0-nightly compilers)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions