-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Const-generics don't work with constants #69816
Comments
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 methodWhen it's defined as a method,this code: #![feature(const_generics)]
use arrayvec::{Array, ArrayVec};
use std::fmt::Debug;
trait IterExt: Sized + Iterator {
fn collect_arr<const N: usize>(self) -> [Self::Item; N]
where
[Self::Item; N]: Array<Item = Self::Item>,
ArrayVec<[Self::Item; N]>: Debug,
{
self.into_iter()
.collect::<ArrayVec<[Self::Item; N]>>()
.into_inner()
.expect("collect_arr")
}
}
impl<This:Iterator> IterExt for This{}
fn main(){
const N:usize=10;
let arr:[u32;10]=(0..10).collect_arr::<N>();
} Emits this error message
Curiously,by changing the method call line to As a free functionWhen it's defined as a free function no error happens: #![feature(const_generics)]
use arrayvec::{Array, ArrayVec};
use std::fmt::Debug;
fn collect_arr<T, const N: usize>(this: T) -> [T::Item; N]
where
T: IntoIterator,
[T::Item; N]: Array<Item = T::Item>,
ArrayVec<[T::Item; N]>: Debug,
{
this.into_iter()
.collect::<ArrayVec<[T::Item; N]>>()
.into_inner()
.expect("collect_arr")
}
fn main(){
const N:usize=10;
let arr:[u32;10]=collect_arr::<_,N>(0..10);
} |
This is the most minimal code I could get to output a very similar error: #![feature(const_generics)]
trait IterExt: Sized + Iterator {
fn default_for_size<const N: usize>(self) -> [Self::Item; N]
where
[Self::Item; N]: Default,
{
Default::default()
}
}
impl<This:Iterator> IterExt for This{}
fn main(){
const N:usize=10;
let arr:[u32;10]=(0..10).default_for_size::<N>();
} The error message for that code
|
@rodrimati1992 But your minimized example behaves differently when writing error: internal compiler error: unexpected const parent in type_of_def_id(): Expr(expr(HirId { owner: DefIndex(10), local_id: 20 }: ::std::ops::Range{start: 0, end: 10,}.default_for_size::<>()))
error: internal compiler error: mir_const_qualif: MIR had errors
--> src/main.rs:16:49
|
16 | let arr:[u32;10]=(0..10).default_for_size::<10usize>();
| ^^^^^^^
error: internal compiler error: PromoteTemps: MIR had errors
--> src/main.rs:16:49
|
16 | let arr:[u32;10]=(0..10).default_for_size::<10usize>();
| ^^^^^^^
error: internal compiler error: broken MIR in DefId(0:13 ~ playground[a375]::main[0]::{{constant}}[1]) ("return type"): bad type [type error]
--> src/main.rs:16:49
|
16 | let arr:[u32;10]=(0..10).default_for_size::<10usize>();
| ^^^^^^^
error: internal compiler error: broken MIR in DefId(0:13 ~ playground[a375]::main[0]::{{constant}}[1]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: src/main.rs:16:49: 16:56, scope: scope[0] } }): bad type [type error]
--> src/main.rs:16:49
|
16 | let arr:[u32;10]=(0..10).default_for_size::<10usize>();
| ^^^^^^^
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:355:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: internal compiler error: unexpected panic |
Btw, instead of writing |
const generics triage I went through all const generics issues and closed all issues which are already fixed. Some issues already have a regression test but were not closed. Also doing this as part of this PR. uff r? @eddyb @varkor closes rust-lang#61936 closes rust-lang#62878 closes rust-lang#63695 closes rust-lang#67144 closes rust-lang#68596 closes rust-lang#69816 closes rust-lang#70217 closes rust-lang#70507 closes rust-lang#70586 closes rust-lang#71348 closes rust-lang#71805 closes rust-lang#73120 closes rust-lang#73508 closes rust-lang#73730 closes rust-lang#74255
This compiles:
(Using the
extend
andarrayvec
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.The text was updated successfully, but these errors were encountered: