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

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

Open
jmagnuson opened this issue Jul 28, 2020 · 3 comments
Open

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

jmagnuson opened this issue Jul 28, 2020 · 3 comments
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug.

Comments

@jmagnuson
Copy link

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)

@jmagnuson jmagnuson added the C-bug Category: This is a bug. label Jul 28, 2020
@ehuss
Copy link
Contributor

ehuss commented Jul 28, 2020

I don't think this has anything to do with being unsafe in particular. Any block-style expression in the last expression statement of a block cannot have braces (particularly as the first expression of a compound expression). For example, {{1}==0} doesn't parse. Some more details are at rust-lang/reference#569.

I don't know if this is particularly intended, or if it is fixable. There is ambiguity in some circumstances (like if condition expressions), but it doesn't seem like there is any particular ambiguity as the tail of a block.

@LeSeulArtichaut LeSeulArtichaut added the A-parser Area: The parsing of Rust source code to an AST. label Jul 28, 2020
@Lonami
Copy link
Contributor

Lonami commented Jul 28, 2020

Recently I needed this but in my case it suggested me to wrap the block inside (), as in:

(unsafe { ... }) != 0

Maybe I had an older compiler or I tried other things for the suggestion to appear.

@LeSeulArtichaut
Copy link
Contributor

Related to #54482/#7909?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants