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

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

Open
theemathas opened this issue Nov 25, 2024 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

theemathas commented Nov 25, 2024

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

@theemathas theemathas added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 25, 2024
@fmease
Copy link
Member

fmease commented Nov 25, 2024

Context: foo() gets represented as an anonymous constant, an AnonConst, which is an item.

But yes, we should either make this work or improve the diagnostic.

@makai410
Copy link
Contributor

/// A constant (expression) that's not an item or associated item,
/// but needs its own `DefId` for type-checking, const-eval, etc.
/// These are usually found nested inside types (e.g., array lengths)
/// or expressions (e.g., repeat counts), and also used to define
/// explicit discriminant values for enum variants.
///
/// You can check if this anon const is a default in a const param
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)`
#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct AnonConst {
pub hir_id: HirId,
pub def_id: LocalDefId,
pub body: BodyId,
pub span: Span,
}

There says AnonConst is not an item or associated item.

But AnonConst is treated as an item when it is being checked unsafety, as far as I can tell.

@fmease
Copy link
Member

fmease commented Nov 26, 2024

Right, it's not strictly speaking an item but it behaves like one in a multitude of contexts. It's an implementation detail. Think of [0; foo()] as [0; {constant#0}] where const {constant#0}: _ = foo();, an AnonConst.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants