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

unsafe blocks do not apply to array length expressions they contain #72359

Open
ecstatic-morse opened this issue May 19, 2020 · 5 comments
Open
Labels
A-array Area: `[T; N]` A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented May 19, 2020

The following example fails to compile on the latest nightly.

#![feature(core_intrinsics)]
#![feature(const_int_unchecked_arith)]

fn main() {
    let _ = unsafe { [0i32; std::intrinsics::unchecked_add(4, 2)] };
}

Output:

warning: unnecessary `unsafe` block
 --> src/main.rs:5:13
  |
5 |     let _ = unsafe { [0i32; std::intrinsics::unchecked_add(4, 2)] };
  |             ^^^^^^ unnecessary `unsafe` block
  |
  = note: `#[warn(unused_unsafe)]` on by default

error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
 --> src/main.rs:5:29
  |
5 |     let _ = unsafe { [0i32; std::intrinsics::unchecked_add(4, 2)] };
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to previous error; 1 warning emitted

I would have expected this to compile successfully, but it seems that unsafe is not applied to array length expressions. The nightly features are only needed because there are no stable const unsafe functions. A user-defined const unsafe fn runs into the same error on stable.

This arose from discussion in rust-lang/rfcs#2920.

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels May 19, 2020
@estebank
Copy link
Contributor

The ticket is valid, but of curiosity, why would you need to use unchecked_add in the array length? The const argument would be executed at compile time to a specific value, netting no value in using unsafe code for this. Although I'm sure it is a convenient MCVE.

@RalfJung
Copy link
Member

FWIW, unsafe blocks also do not propagate into nested functions:

fn foo() { unsafe {
  fn bar() { /* not considered in unsafe block */ }
} }

But AFAIK they do propagate to closures.

I am not sure which one array length expressions are "closer" to... they feel somewhat distinct from both, honestly.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented May 20, 2020

The ticket is valid, but of curiosity, why would you need to use unchecked_add in the array length?

The example is not derived from real code.

not sure which one array length expressions are "closer" to.

To me, array length expressions feel even more part of their containing item than closures, and much more so than nested functions. Not sure how widely shared this view is. I suppose we don't allow array length expressions to reference in-scope generic parameters, while we do allow closures to refer to them. I think this is more a technical limitation than a concious choice, however.

@LeSeulArtichaut
Copy link
Contributor

I'll try to see if we can special-case const blocks as we do with closures. @rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: `[T; N]` A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. 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

7 participants