rust-lang / rust-lang/rust

`const { ... }` blocks cannot be written outside of function

Open
#128,338 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-papercut D-terse
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in src/main.rs with the reported rustc nightly version and compare the top-level const block with the function-local version. Done means the reported top-level form has behavior matching the issue's expected compilation result, with the relevant compiler tests updated or added.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.