Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe blocks fail to parse as expressions in comparisons #95410

Closed
clarfonthey opened this issue Mar 28, 2022 · 2 comments
Closed

Unsafe blocks fail to parse as expressions in comparisons #95410

clarfonthey opened this issue Mar 28, 2022 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented Mar 28, 2022

Example gist: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=01dfdf28913f8fe69d419dfd7472dc49

In short, take these three functions:

fn test_one() -> u32 {
    unsafe { 2 }
}

fn test_two() -> bool {
    let x: u32 = unsafe { 2 };
    let y: u32 = unsafe { 2 };
    x == y
}

fn test_three() -> bool {
    unsafe { 2 } == unsafe { 2 }
}

The first two of these functions parse correctly, but the last one does not. The parser complains about seeing == after an unsafe block, and in its recovery, assumes that the unsafe block has type () instead of {integer}.

Exact output:

Compiling playground v0.0.1 (/playground)
error: expected expression, found `==`
  --> src/lib.rs:12:18
   |
12 |     unsafe { 2 } == unsafe { 2 }
   |                  ^^ expected expression

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
   |
12 |     unsafe { 2 } == unsafe { 2 }
   |              ^ expected `()`, found integer

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

This fails on the current stable compiler and nightly, those being 1.59.0 and 2022-03-27, respectively.

I found this code when using unions:

union Test {
    raw: u8,
    // other stuff that doesn't matter
}

impl PartialEq for Test {
    fn eq(&self, rhs: &Test) -> bool {
        unsafe { self.raw } == unsafe { rhs.raw }
    }
}
@clarfonthey clarfonthey added the C-bug Category: This is a bug. label Mar 28, 2022
@ehuss
Copy link
Contributor

ehuss commented Mar 28, 2022

Thanks for the report! This currently isn't a bug (AFAIK), as block-style expressions are not accepted in binary operators. I can understand how it is confusing, and the error message is not very helpful.

I'm going to close this as a duplicate of #54482 and #74854 (and others).

@ehuss ehuss closed this as completed Mar 28, 2022
@clarfonthey
Copy link
Contributor Author

Oh wow, I didn't realise this was an ongoing issue. Thank you for pointing those out; I should have made a stronger effort to search for it. .-.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants