Skip to content

const { ... } blocks cannot be written outside of function #128338

@StackOverflowExcept1on

Description

@StackOverflowExcept1on

I tried this code:

// error
const { assert!(size_of::<u32>() <= size_of::<usize>()) };

fn main() {
    //ok
    const { assert!(size_of::<u32>() <= size_of::<usize>()) };
}

I expected to see this happen: it compiles

Instead, this happened: it doesn't compile

error: expected item, found keyword `const`
 --> src/main.rs:2:1
  |
2 | const { assert!(size_of::<u32>() <= size_of::<usize>()) };
  | ^^^^^ expected item
  |
  = note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>

error: could not compile `app` (bin "app") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.82.0-nightly (2cbbe8b8b 2024-07-28)
binary: rustc
commit-hash: 2cbbe8b8bb2be672b14cf741a2f0ec24a49f3f0b
commit-date: 2024-07-28
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 18.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-papercutDiagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions