E0903: Type annotation required
| Code | Category | Stability |
|---|---|---|
E0903 | Array | Permanent |
Explanation
Type inference could not fix the element type of an array literal. Inference runs over one function body and solves the constraints that body generates (3.11:2, 3.11:4); an empty literal [] generates no element constraint at all, so nothing determines its element type variable. The same happens to a literal whose elements leave the element type open — a nested [[]] — and to an array left unresolved by a malformed or partially specialized comptime construction. Rather than let an unconstrained type reach code generation, the compiler reports E0903 at the literal. The diagnostic is about the element type and not the length: an array literal's type is [T; n] for one shared element type T (7.1:2), and [] is perfectly legal once something supplies that T.
Likely cause
An empty array literal was written where nothing supplies its element type, most often let xs = [];. Annotate the binding — let xs: [i32; 0] = []; — or put the literal in a position that fixes the element type, such as an argument at a call or a return expression. When the literal is not empty, the message still means the element type is open after inference: annotate the binding, or annotate the element expression inference could not solve.
Examples
An empty array literal with no context
fn main() -> i32 {
let xs = [];
0
}
Annotate the element type
fn main() -> i32 {
let xs: [i32; 0] = [];
0
}