Skip to content

Unable to Utilize unsafe {} Blocks as an ExpressionΒ #98093

Open
@HTGAzureX1212

Description

@HTGAzureX1212

I tried this code:

fn predicate() -> bool {
    unsafe { std::str::from_utf8_unchecked(&[65]) } == "A"
}

fn main() {}

I expected to see this happen: compilation succeeds.

Instead, this happened: two errors are emitted.

error: expected expression, found `==`
 --> src/main.rs:2:49
  |
2 | unsafe { std::str::from_utf8_unchecked(&[65]) } == "A"
  |                                                 ^^ expected expression

error[E0308]: mismatched types
 --> src/main.rs:2:10
  |
2 | unsafe { std::str::from_utf8_unchecked(&[65]) } == "A"
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here: `;`
  |          |
  |          expected `()`, found `&str`

Meta

rustc --version --verbose:

rustc 1.63.0-nightly (ca122c7eb 2022-06-13)
binary: rustc
commit-hash: ca122c7ebb3ab50149c9d3d24ddb59c252b32272
commit-date: 2022-06-13
host: x86_64-pc-windows-msvc
release: 1.63.0-nightly
LLVM version: 14.0.5

Even though I know that I can simply wrap the entire unsafe block with parentheses, but this still doesn't seem right when an if block can be combined with boolean expressions in a similar fashion:

if true && if true { true } else { false } { }

I know that the if case seem to be very spaghetti, but I feel like unsafe blocks should exhibit this same behaviour during compilation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-grammarArea: The grammar of RustA-parserArea: The lexing & parsing of Rust source code to an ASTD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions