-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Compiler can't infer const parameter from infallible array pattern #70529
Comments
Simplified: #![feature(const_generics)]
fn as_chunks<const N: usize>() -> [u8; N] {
loop {}
}
fn main() {
let [x, y] = as_chunks();
} |
The relevant code is defined here:
|
To solve this, I suspect we'd need to register an obligation |
What if we just handle the case in which they have to be equal, for now? Because that needs no obligation, you can just unify the constant with the length. And you'd get a proper mismatch error if the length is not an inference variable. |
Yeah, scratch what I said. We can tweak the branch here: } else {
// No idea what the length is, which happens if we have e.g.,
// `let [a, b] = arr` where `arr: [T; N]` where `const N: usize`.
self.error_scrutinee_unfixed_length(span);
} and as you say, unify the constant with the known length. |
@rustbot claim |
infer array len from pattern closes rust-lang#70529 This still errors in the following case ```rust #![feature(const_generics)] fn arr<const N: usize>() -> [u8; N] { todo!() } fn main() { match arr() { [5, ..] => (), //~^ ERROR cannot pattern-match on an array without a fixed length [_, _] => (), } } ``` Considering that this should be rare and is harder to implement I would merge this PR without *fixing* the above.
infer array len from pattern closes rust-lang#70529 This still errors in the following case ```rust #![feature(const_generics)] fn arr<const N: usize>() -> [u8; N] { todo!() } fn main() { match arr() { [5, ..] => (), //~^ ERROR cannot pattern-match on an array without a fixed length [_, _] => (), } } ``` Considering that this should be rare and is harder to implement I would merge this PR without *fixing* the above.
The compiler can't infer a const parameter based on the expected array type in an infallible pattern.
I tried this code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4673d663e2f78cdc15395c1cc3701d67
I expected the code to compile and run without errors.
Instead, I got this compiler error:
Meta
The compiler was 1.44.0-nightly (2020-03-28 7762131)
This issue has been assigned to @lcnr via this comment.
The text was updated successfully, but these errors were encountered: