Skip to content

Malformed error message with wrong use of min const generics #77092

Closed
@leonardo-m

Description

@leonardo-m

This is wrong code (because there's no way to infer N):

#![feature(min_const_generics)]

use std::convert::TryInto;

#[inline(never)]
fn take_array_from_mut<T, const N: usize>(data: &mut [T], start: usize) -> &mut [T; N] {
    (&mut data[start .. start + N]).try_into().unwrap()
}

fn main() {
    let mut arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];

    for i in 1 .. 4 {
        println!("{:?}", take_array_from_mut(&mut arr, i));
    }
}

rustc V. 1.48.0-nightly 0da580074 2020-09-22 gives:

error[E0282]: type annotations needed
  |
  = note: unable to infer the value of a const parameter

error: aborting due to previous error

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

Tool completed with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.const-generics-bad-diagnosticsAn error is correctly emitted, but is confusing, for `min_const_generics`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions