rust-lang / rust-lang/rust

Calling non-const function `core::slice::iter::<impl IntoIterator for &[u8]>::into_iter`

Open
#125,490 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-diagnostics D-terse T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I didn't find a clear issue that was talking about that, and I'm clearly in the "nightly scape" of Rust here. I'm trying to build the following code (don't take attention to the possible unsafe part of this code, I omitted voluntary some part for better clarity):

Playground link:

#![feature(const_mut_refs)]
#![feature(const_for)]
#![feature(const_maybe_uninit_write)]
#![feature(effects)]

use core::mem::MaybeUninit;

#[must_use]
const fn from_slice_unchecked(from_slice: &[u8]) -> [MaybeUninit<u8>; 1]
{
    let mut vec = [MaybeUninit::uninit(); 1];

    for byte in from_slice {
        vec[0].write(*byte);
    }

    vec
}

fn main() {
    let _ = const { from_slice_unchecked(&[0]) };
}
error[E0080]: evaluation of `main::{constant#0}` failed
  --> src/main.rs:13:17
   |
13 |     for byte in from_slice {
   |                 ^^^^^^^^^^ calling non-const function `core::slice::iter::<impl IntoIterator for &[u8]>::into_iter`
   |
note: inside `from_slice_unchecked`
  --> src/main.rs:13:17
   |
13 |     for byte in from_slice {
   |                 ^^^^^^^^^^
note: inside `main::{constant#0}`
  --> src/main.rs:21:21
   |
21 |     let _ = const { from_slice_unchecked(&[0]) };
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^

note: erroneous constant encountered
  --> src/main.rs:21:13
   |
21 |     let _ = const { from_slice_unchecked(&[0]) };
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.

It does build if I don't const inline the call to from_slice_unchecked() though. But here, I'm wanted to create this buffer at build time and I don't find a way to do it "easily".

The closer issue I could find is https://github.com/rust-lang/rust/issues/103265 which contains this closed PR https://github.com/rust-lang/rust/pull/102225.

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 nightly example in the linked Playground and inspect the failure at src/main.rs line 13. Read issue #103265 and pull request #102225 for related context; done means the const-inline call can iterate over the slice without the reported non-const-function error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.