Rust ICE: Index Out Of Bounds Error
Rust ICE: Index Out of Bounds: The Length is 0 But the Index is 0
Hey folks! π I ran into a nasty Internal Compiler Error (ICE) while playing around with Rust, and I wanted to share the details. Specifically, I'm seeing an "index out of bounds" error, even though the length is zero and the index is also zero. Sounds weird, right? Let's dive in and see what's happening.
The Code That Started It All
So, the problem pops up with some code that uses min_generic_const_args and MyTrait. Check it out:
#![feature(min_generic_const_args)]
#![allow(incomplete_features)]
trait Maybe<T> {}
trait MyTrait<const F: fn() -> ()> {}
fn foo<'a>(x: &'a ()) -> &'a () { x }
impl<T> Maybe<T> for T where T: MyTrait<{ foo }> {}
fn main() {}
This code defines a few traits and a function. The core of the issue seems to be around how the MyTrait trait is implemented, especially when combined with the min_generic_const_args feature. It appears the compiler gets a little confused when dealing with function pointers as const generic parameters.
Diving into the Error
The compiler throws a panic with the following message:
thread 'rustc' panicked at /rustc-dev/86a49fd71fecd25b0fd20247db0ba95eeceaba28/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs:622:20:
index out of bounds: the len is 0 but the index is 0
That's the main error. It points to a specific file and line within the compiler itself (hir_ty_lowering/generics.rs:622:20). The error message "index out of bounds" is pretty standard, but the fact that the length is zero and the index is also zero is confusing. It means the compiler is trying to access an element at position 0 in a list that has a length of 0. That's a definite no-no, and a sign of a bug.
The stack trace also offers clues. It shows the execution path within the compiler when the panic happened. It involves things like lower_generic_args_of_path, lower_poly_trait_ref, and other internal compiler functions. Basically, the compiler is getting lost when trying to process the generics in your code.
Compiler Version and Setup
I'm using the following compiler version:
rustc 1.94.0-nightly (86a49fd71 2026-01-14)
It's a nightly build, so the compiler is always evolving. I've included the complete output of rustc --version --verbose in the original report so you can see the details.
Digging Deeper: The min_generic_const_args Feature
This error arises when using the min_generic_const_args feature, which lets you use more complex expressions in const generics. This feature is still experimental, meaning it's not fully baked yet and might have some rough edges (like this ICE!). The error highlights potential issues in how the compiler handles generic constants, especially when they involve function pointers. This is a common area of complexity in the Rust compiler, and itβs where a lot of bugs can hide.
Errors and Notes
Besides the main ICE, the compiler also throws an additional error message:
error: using function pointers as const generic parameters is forbidden
--> <source>:7:24
|
7 | trait MyTrait<const F: fn() -> ()> {}
| ^^^^^^^^^^^
|
= note: the only supported types are integers, `bool`, and `char"
This error is more explicit, saying the compiler doesn't support function pointers as const generic parameters. This restriction explains the issue. The compiler is trying to use a function pointer (fn() -> ()) as a const generic argument, which it's not designed to do. This limitation is why the ICE happens. The compiler's internal logic probably isn't prepared to handle this case, leading to the "index out of bounds" panic.
Why This Matters
Internal Compiler Errors are a pain because they halt your build, and they indicate a bug in the compiler, not your code directly. If you encounter one, it's crucial to report it. These reports help the Rust developers squash bugs, making the language more stable and reliable. Reporting these issues helps improve Rust for everyone. If you find one, make a detailed bug report, including the code, the compiler version, and the full error output, as I've done here. This information helps the developers reproduce and fix the issue.
The Takeaway
This "index out of bounds" error arises because of the interaction of several factors, including the use of min_generic_const_args and function pointers as const generic parameters. It reveals limitations in the compiler's handling of these features. As Rust matures, the compiler will get more robust at handling these edge cases. Until then, we must be extra careful when using experimental features.
Next Steps
If you see this error yourself, the best thing to do is report it on the Rust issue tracker (https://github.com/rust-lang/rust/issues). Also, consider the following:
- Simplify: Try to create a minimal, reproducible example (MRE). It helps the developers focus on the core issue.
- Update: Make sure you're using the latest nightly build. The fix might be already in the nightly.
- Workarounds: In the meantime, you might have to avoid using function pointers as const generic parameters or restructure your code.
That's it for this ICE report, guys! Hopefully, this helps you understand what's going on. Happy coding, and let's keep making Rust awesome! π