rust-lang / rust-lang/rust

An unsafe const fn being used to compute an array length or const generic is incorrectly described as being an "item".

Open
#133,441 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics I-lang-radar T-compiler T-lang T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
const unsafe fn foo() -> usize { 1 }

fn main() {
    unsafe {
        let _x = [0; foo()];
    }
}
Current output
Compiling playground v0.0.1 (/playground)
warning: unnecessary `unsafe` block
 --> src/main.rs:4:5
  |
4 |     unsafe {
  |     ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default

error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
 --> src/main.rs:5:22
  |
4 |     unsafe {
  |     ------ items do not inherit unsafety from separate enclosing items
5 |         let _x = [0; foo()];
  |                      ^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

For more information about this error, try `rustc --explain E0133`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted
Desired output
Say something other than "items do not inherit unsafety from separate enclosing items"
Rationale and extra context

Given that unsafe blocks apply "through" closures, I find it a bit weird that it doesn't apply through array lengths or const generics. Maybe this is fine, but at the very least, the error message should not describe the problem as being about "items", since there aren't any relevant items in sight.

Other cases

Other similar cases with similar errors:

const unsafe fn foo() -> usize { 1 }

fn main() {
    unsafe {
        <[i32; foo()]>::default();
    }
}
const unsafe fn foo() -> usize { 1 }

fn lol<const N: usize>() {}

fn main() {
    unsafe {
        lol::<{foo()}>();
    }
}
const unsafe fn foo() -> usize { 1 }

struct Thing<const N: usize>;

fn main() {
    unsafe {
        let _x: Thing<{foo()}>;
    }
}
Rust Version
Reproducible on the playground with stable rust version 1.82.0, and nightly rust version `1.85.0-nightly (2024-11-22 a47555110cf09b3ed598)`
Anything else?

No response

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

Reproduce the array-length and const-generic examples in the Rust playground, then trace the E0133 diagnostic and its wording around the unsafe call. Done means these cases no longer describe the problem as involving unrelated “items”, with coverage for the examples shown.

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.