{"record":{"id":"a04ded2fe9026f9b","repo":"BoundaryML/baml","slug":"unsupported-assign-op-in-c-for-after-clause","errorCode":null,"errorMessage":"unsupported assign op in C-for after clause","messagePattern":"unsupported assign op in C-for after clause","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1128,"sourceCode":"                                                        watch_handler,\n                                                        function_name,\n                                                    )\n                                                    .await?,\n                                                )?;\n\n                                                let result_val = match assign_op {\n                                                    AssignOp::AddAssign => {\n                                                        match (current_val.clone(), rhs_val.clone()) {\n                                                    (\n                                                        BamlValueWithMeta::Int(a, meta),\n                                                        BamlValueWithMeta::Int(b, _),\n                                                    ) => BamlValueWithMeta::Int(a + b, meta),\n                                                    _ => bail!(\n                                                        \"unsupported types for += in C-for after clause\"\n                                                    ),\n                                                }\n                                                    }\n                                                    _ => bail!(\n                                                    \"unsupported assign op in C-for after clause\"\n                                                ),\n                                                };\n                                                assign_to_expr(\n                                                    left,\n                                                    result_val,\n                                                    scopes,\n                                                    thir,\n                                                    run_llm_function,\n                                                    watch_handler,\n                                                    function_name,\n                                                )\n                                                .await?;\n                                            }\n                                            Statement::Assign { left, value } => {\n                                                let v = expect_value(\n                                                    evaluate_expr(\n                                                        value,","sourceCodeStart":1110,"sourceCodeEnd":1146,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1110-L1146","documentation":"This error is raised by the BAML expression interpreter (handle_statement in interpret.rs) while executing the after-clause of a C-style for loop (`for (init; cond; after)`). When a loop body executes `continue` or finishes normally, the interpreter runs the after statement, but it only supports plain assignment (`i = ...`) and `+=` (AddAssign). Any other compound assign operator (`-=`, `*=`, `/=`, etc.) in the after clause hits this bail!.","triggerScenarios":"A BAML C-style for loop whose after clause uses a compound assign operator other than `+=`, e.g. `for (let i = 0; i < 10; i -= 1)` or `i *= 2`. The error fires when the loop body completes an iteration or a `continue` executes, not at parse time.","commonSituations":"Porting loop idioms from C/JS/Python into BAML (`i--`, `i *= 2`, `i /= 2` in the update clause) before the interpreter added support for those operators; writing decrementing loops.","solutions":["Rewrite the after clause to use `+=` only, e.g. `i -= 1` becomes `i += -1`.","Rewrite the after clause as a plain assignment: `i = i * 2` instead of `i *= 2`.","Replace the C-style for loop with a foreach loop over a range if the loop is a simple counter.","Upgrade BAML: newer versions may support more assign operators in the after clause."],"exampleFix":"// before (BAML)\nfor (let i = 10; i > 0; i -= 1) { ... }\n// after\nfor (let i = 10; i > 0; i += -1) { ... }","handlingStrategy":"validation","validationCode":"// Before running a BAML C-style for loop, ensure the after clause only uses += or plain assignment:\nconst ALLOWED = /^^\\s*[A-Za-z_]\\w*\\s*(\\+=|=)\\s*.+$/;\nif (!ALLOWED.test(afterClauseSrc)) {\n  throw new Error(\"C-for after clause must use '=' or '+=' (e.g. 'i += 1' or 'i = i * 2')\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict C-for update clauses to `i += step` or `i = expr` in your BAML style guide.","Write decrement loops as `i += -1`.","Prefer foreach-over-range loops, which avoid the after clause entirely.","Add a lint/test that greps BAML sources for `-=`/`*=`/`/=` in for-update position."],"tags":["baml","interpreter","loops","unsupported-operator"],"backgroundTag":"unsupported-operation","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"}