-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-const-genericsArea: const generics (parameters and arguments)Area: const generics (parameters and arguments)A-inferenceArea: Type inferenceArea: Type inferenceC-bugCategory: This is a bug.Category: This is a bug.F-const_generics`#![feature(const_generics)]``#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
This compiles:
use arrayvec::{Array, ArrayVec};
#[ext(pub, name = IterExt)]
impl<T: Iterator<Item = U>, U> T {
fn collect_arr<const N: usize>(self) -> [U; N]
where
[U; N]: Array<Item = U>,
ArrayVec<[U; N]>: Debug,
{
self.collect::<ArrayVec<[U; N]>>().into_inner().expect("collect_arr")
}
}
(Using the extend
and arrayvec
crates.)
But using it with a constant doesn't compile:
Even though pub const CUE_POINT_COUNT: usize = 8;
is in scope!
If I write .collect_arr::<8usize>()
instead, it compiles.
Also, if I don't use the extension trait method but inline it (.collect::<ArrayVec<[_; CUE_POINT_COUNT]>>().into_inner().unwrap()
) it works with the constant!
So for some reason rustc can't see that CUE_POINT_COUNT == 8
here.
Metadata
Metadata
Assignees
Labels
A-const-genericsArea: const generics (parameters and arguments)Area: const generics (parameters and arguments)A-inferenceArea: Type inferenceArea: Type inferenceC-bugCategory: This is a bug.Category: This is a bug.F-const_generics`#![feature(const_generics)]``#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
rodrimati1992 commentedon Mar 8, 2020
Sorry for all the edits,apparently this only errors if the function is a method, when defined as a free function it doesn't error.
As a method
When it's defined as a method,this code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=cdf1b7f44fe0c7ae24ef2df775f841d8
Emits this error message
Curiously,by changing the method call line to
let arr:[u32;10]=IterExt::collect_arr::<N>((0..10));
it compiles without errors.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7aff9f76b3d31e931ef1b66077d40e6f
As a free function
When it's defined as a free function no error happens:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=9e7bae5d74089c7177aaa22226e1013c
rodrimati1992 commentedon Mar 8, 2020
This is the most minimal code I could get to output a very similar error:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f45606455c01151f0b16cd1484b3222c
The error message for that code
Boscop commentedon Mar 8, 2020
@rodrimati1992 But your minimized example behaves differently when writing
10usize
instead ofN
! Instead of compiling fine (like with my original example), the compiler panics:https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2bbd3b7f2d16e643a8dedb82659aa0c4
Boscop commentedon Mar 8, 2020
Btw, instead of writing
8usize
in.collect_arr::<8usize>()
, the compiler should be able to infer that8
is anusize
. IOW, this should compile:.collect_arr::<8>()
. Is there already a ticket for this?10 remaining items