Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Type inference bug?
Hi,
I'm trying out some code with const generics and I'm running into some issues with type inference. Is there any way to make this work without having to specify let _: Foo<4, 0> = ...;
?
Playground
struct Foo<const N: usize, const M: usize = 0> {
_x: [usize; N],
y: Option<[usize; M]>,
}
impl<const N: usize, const M: usize> Foo<N, M> {
fn new(x: [usize; N]) -> Self {
Self {
_x: x,
y: None
}
}
fn new_without_y(x: [usize; N]) -> Foo<N, 0> {
Foo {
_x: x,
y: None
}
}
fn with_y(mut self, y: [usize; M]) -> Self {
self.y = Some(y);
self
}
}
fn main() {
// This doesn't compile: `Foo<4, 0>` should be inferred
// https://github.com/rust-lang/rust/issues/98931
// let _ = Foo::new([0; 4]);
// This compiles: `Foo<4, 8>` is inferred
let _ = Foo::new([0; 4]).with_y([0; 8]);
// This doesn't compile:
// Even though the return type is Foo<N, 0>, the compiler says it can't infer M
// Not sure if known issue, it seems different from the one related to default const generics
let _: Foo = Foo::new_without_y([0; 4]);
}
6 posts - 4 participants
🏷️ rust_feed