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

arrays as const parameters result in bad error messages #81698

Closed
workingjubilee opened this issue Feb 3, 2021 · 5 comments · Fixed by #107190
Closed

arrays as const parameters result in bad error messages #81698

workingjubilee opened this issue Feb 3, 2021 · 5 comments · Fixed by #107190
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. F-adt_const_params `#![feature(adt_const_params)]`

Comments

@workingjubilee
Copy link
Member

workingjubilee commented Feb 3, 2021

This is a much abbreviated version of the code from my experiments with adding a shuffle API to the stdsimd repo, it probably needs further reduction but I wanted to file this before I forgot:

#![feature(const_generics, repr_simd, platform_intrinsics)]

#[repr(simd)]
pub struct SimdU32<const LANES: usize>([u32; LANES]);

impl<const LANES: usize> SimdU32<LANES> {
     pub fn shuffle2<const M: [u32; 2]>(self, other: Self) -> SimdU32<2> {
         // this is just the rustc platform intrinsic, don't worry too much about what it's doing
         unsafe { crate::intrinsics::simd_shuffle2(self, other, M) }
     }
}
// and now in another file, over in the tests... 
use core_simd::SimdU32;

#[test]
fn attempt_shuffle() {
    let a = SimdU32::from_array([2, 4, 1, 9]);
    let b = a;
    let c = a.shuffle2::<[3, 1]>(b);
}

I expected to see this happen: it works, provides a useful error, or even just ICEs.

Instead, this happened: I got a request to input a type instead!

error: unmatched angle bracket
  --> crates/core_simd/tests/permute.rs:11:25
   |
11 |     let c = a.shuffle2::<[3, 1]>(b);
   |                         ^ help: remove extra angle bracket

error: expected type, found `3`
  --> crates/core_simd/tests/permute.rs:11:27
   |
11 |     let c = a.shuffle2::<[3, 1]>(b);
   |                           ^ expected type

And no, that doesn't work either if you put it in, because instead you get:

error[E0747]: type provided when a constant was expected
 --> crates/core_simd/tests/permute.rs:9:26
  |
9 |     let c = a.shuffle2::<[u32; 2]>(b);
  |                          ^^^^^^^^

error: aborting due to previous error

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

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (a2f8f6281 2021-01-27)
binary: rustc
commit-hash: a2f8f6281817d430e20726128b739d3c6708561c
commit-date: 2021-01-27
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1
@workingjubilee workingjubilee added C-bug Category: This is a bug. A-const-generics Area: const generics (parameters and arguments) labels Feb 3, 2021
@petrochenkov
Copy link
Contributor

The fix is

let c = a.shuffle2::<{ [3, 1] }>(b);

Due to parsing ambiguities generic arguments currently need to be put into braces, unless they are really simple (like 1 or A), [3, 1] is not simple enough.

@workingjubilee
Copy link
Member Author

Oh, cool! That does compile! I knew about that but wasn't sure if that was the problem because I thought that was just for expressions? I guess that's technically an array expression?

That is none of the errors I was presented with, though, which makes this a diagnostics problem I suppose?

@lcnr lcnr added D-confusing Diagnostics: Confusing error or lint that should be reworked. F-const_generics `#![feature(const_generics)]` labels Feb 8, 2021
@pickfire
Copy link
Contributor

error: unmatched angle bracket
 --> src/lib.rs:8:10
  |
8 |     foo::<(20 * 100 + 20 * 10 + 1)>(); // ok: const expression contains no generic parameters
  |          ^ help: remove extra angle bracket

I believe diagnostic for const generic (example taken from https://blog.rust-lang.org/2021/02/26/const-generics-mvp-beta) needs to be improved too before release, at least I thought that ( can be used instead, if one didn't know about { then they need to search it up.

@BoxyUwU BoxyUwU added A-diagnostics Area: Messages for errors, warnings, and lints F-generic_const_exprs `#![feature(generic_const_exprs)]` F-adt_const_params `#![feature(adt_const_params)]` and removed F-const_generics `#![feature(const_generics)]` labels Jun 28, 2022
@lcnr lcnr removed the F-generic_const_exprs `#![feature(generic_const_exprs)]` label Jun 28, 2022
@lcnr
Copy link
Contributor

lcnr commented Jun 28, 2022

const generics triage

Arrays as const parameters still have bad error messages.

#![feature(adt_const_params)]

pub struct SimdU32<const LANES: usize>([u32; LANES]);

impl<const LANES: usize> SimdU32<LANES> {
    pub fn shuffle2<const M: [u32; 2]>(self) -> SimdU32<2> {
        todo!()
    }
}

fn main() {
    let a = SimdU32([2, 4, 1, 9]);
    let c = a.shuffle2::<[3, 1]>();
}

same with tuples

#![feature(adt_const_params)]

pub struct SimdU32<const LANES: usize>([u32; LANES]);

impl<const LANES: usize> SimdU32<LANES> {
    pub fn shuffle2<const M: (u32, u32)>(self) -> SimdU32<2> {
        todo!()
    }
}

fn main() {
    let a = SimdU32([2, 4, 1, 9]);
    let c = a.shuffle2::<(3, 1)>();
}

@lcnr lcnr changed the title Const generic array parameters are too funny arrays as const parameter result in bad error messages Jun 28, 2022
@lcnr lcnr changed the title arrays as const parameter result in bad error messages arrays as const parameters result in bad error messages Jun 28, 2022
@fmease
Copy link
Member

fmease commented Jan 19, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. F-adt_const_params `#![feature(adt_const_params)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants