Skip to content

Can't write generic const fns #116354

Open
Open
@ilyvion

Description

@ilyvion

I was going to leave this as a remark on #80384, but you're apparently supposed to make separate issues instead of leaving comments on tracking issues, so here goes:

I was just playing around and couldn't even get past this simple code before getting yelled at:

const fn foo<T, const N: usize>(values: [T; N]) -> [T; N] {
    let slice = values.as_slice();
    let len = slice.len();
    
    values
}

Both as_slice() and len() are declared as const fn, so you'd think they'd be fine to use in another const fn, but no:

error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
 --> src/lib.rs:2:17
  |
2 |     let slice = values.as_slice();
  |                 ^^^^^^^^^^^^^^^^^
  |
  = note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more information

I don't even understand what this is trying to tell me. Issue #80384, which is the one linked, does not at all explain the source/cause of this error. I'm not working with UnsafeCell or interior mutability here. I'm attempting to make a &[T] from a [T; N].

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.F-const_refs_to_cell`#[feature(const_refs_to_cell)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions