{"record":{"id":"14b6d5e137e55213","repo":"BoundaryML/baml","slug":"array-access-should-be-either-map-or-array","errorCode":null,"errorMessage":"array access should be either map or array.","messagePattern":"array access should be either map or array\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/codegen.rs","lineNumber":621,"sourceCode":"                        let Some(&field_index) = resolved_fields.get(field) else {\n                            panic!(\"undefined field: {class_name}.{field}\");\n                        };\n\n                        // Generate bytecode: load base, load value, store field\n                        self.compile_expression(base);\n                        self.compile_expression_with_block_behavior(value, true);\n                        self.emit(Instruction::StoreField(field_index));\n                    }\n                    thir::Expr::ArrayAccess {base, index, meta: _} => {\n\n                        self.compile_expression(base);\n                        self.compile_expression(index);\n                        self.compile_expression_with_block_behavior(value, true);\n\n                        self.emit(match base.meta().1.as_ref().expect(\"must have a resolved type\") {\n                            TypeIR::List(_, _) => Instruction::StoreArrayElement,\n                            TypeIR::Map(_, _, _) => Instruction::StoreMapElement,\n                            _ => panic!(\"array access should be either map or array.\")\n                        });\n\n                    }\n                    _ => panic!(\"Invalid left hand of assignment, only variables, instance fields and array elements can be assigned\"),\n                }\n            }\n            thir::Statement::AssignOp {\n                left,\n                value,\n                assign_op,\n                ..\n            } => {\n                let binop = match assign_op {\n                    hir::AssignOp::AddAssign => Instruction::BinOp(BinOp::Add),\n                    hir::AssignOp::SubAssign => Instruction::BinOp(BinOp::Sub),\n                    hir::AssignOp::MulAssign => Instruction::BinOp(BinOp::Mul),\n                    hir::AssignOp::DivAssign => Instruction::BinOp(BinOp::Div),\n                    hir::AssignOp::ModAssign => Instruction::BinOp(BinOp::Mod),","sourceCodeStart":603,"sourceCodeEnd":639,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/codegen.rs#L603-L639","documentation":"A panic during codegen of an element-assignment statement: the compiler matched the resolved type of the indexed base expression expecting List or Map, and hit anything else (e.g. a primitive, string, or class type). The type resolution metadata lacked a List/Map type where indexed assignment was used.","triggerScenarios":"`arr[i] = v` where `arr`'s resolved type is neither List nor Map; unresolved type metadata; assigning into a string or scalar via index syntax.","commonSituations":"Index-assigning into a non-collection variable after a type change; code where type inference marked the variable as a different kind; strings mistakenly treated as indexable.","solutions":["Ensure the indexed variable is declared/typed as a list or map in BAML","Fix the variable's type annotation or initializer","If the resolved type is legitimately missing, report a compiler bug to the BAML maintainers"],"exampleFix":"// before\nlet name = \"hello\"\nname[0] = 'H'\n// after\nlet chars = [\"h\", \"e\", \"l\", \"l\", \"o\"]\nchars[0] = \"H\"","handlingStrategy":"validation","validationCode":"fn ensure_indexable(t: &TypeIR) -> Result<(), String> {\n    match t {\n        TypeIR::List(_, _) | TypeIR::Map(_, _, _) => Ok(()),\n        other => Err(format!(\"cannot index-assign into type {:?}\", other)),\n    }\n}","typeGuard":"fn is_indexable(t: Option<&TypeIR>) -> bool {\n    matches!(t, Some(TypeIR::List(_, _)) | Some(TypeIR::Map(_, _, _)))\n}","tryCatchPattern":null,"preventionTips":["Only use index assignment on declared lists/maps","Check inferred types when refactoring variables","Avoid treating strings as indexable in BAML"],"tags":["rust","compiler","codegen","type-mismatch","panic"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}