{"record":{"id":"c024f0c9b79ae743","repo":"BoundaryML/baml","slug":"c-style-for-loop-condition-must-be-boolean","errorCode":null,"errorMessage":"C-style for loop condition must be boolean","messagePattern":"C-style for loop condition must be boolean","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":1064,"sourceCode":"                    } => {\n                        loop {\n                            // Check condition (if present)\n                            if let Some(cond_expr) = condition {\n                                let cond_val = expect_value(\n                                    evaluate_expr(\n                                        cond_expr,\n                                        scopes,\n                                        thir,\n                                        run_llm_function,\n                                        watch_handler,\n                                        function_name,\n                                    )\n                                    .await?,\n                                )?;\n                                match cond_val {\n                                    BamlValueWithMeta::Bool(false, _) => break,\n                                    BamlValueWithMeta::Bool(true, _) => {}\n                                    _ => bail!(\"C-style for loop condition must be boolean\"),\n                                }\n                            }\n\n                            // Execute loop body\n                            match evaluate_block_with_control_flow(\n                                block,\n                                scopes,\n                                thir,\n                                run_llm_function,\n                                watch_handler,\n                                function_name,\n                            )\n                            .await?\n                            {\n                                ControlFlow::Break => break,\n                                ControlFlow::Continue => {\n                                    // Execute after statement if present\n                                    if let Some(after_stmt) = after {","sourceCodeStart":1046,"sourceCodeEnd":1082,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L1046-L1082","documentation":"Raised by the BAML interpreter while executing a C-style `for (init; cond; after)` loop: after evaluating the condition, the result must be Bool(true) to continue or Bool(false) to break. Any other value type (Int, String, null, etc.) makes the match fall through to this bail. BAML does not truthiness-coerce loop conditions.","triggerScenarios":"A C-for loop whose condition expression evaluates to a non-Bool, e.g. `for (let i = 0; i; i += 1)` (Int condition) or a condition returning a string/null from a function call.","commonSituations":"Porting C/JS/Python loops that rely on numeric truthiness (`while i`); calling a helper in the condition that returns Int/String instead of bool; using a nullable comparison result.","solutions":["Make the condition a real boolean expression, e.g. `i > 0` instead of `i`.","Fix any helper function used in the condition to return Bool.","Handle null-producing conditions explicitly (`cond != null && cond)` style)."],"exampleFix":"// before (BAML)\nfor (let i = 10; i; i -= 1) { ... } // error: condition must be boolean\n\n// after\nfor (let i = 10; i > 0; i -= 1) { ... }","handlingStrategy":"type-guard","validationCode":"// ensure a loop condition is boolean before running the C-for\nfunction assertBoolCondition(cond: unknown): void {\n  if (typeof cond !== \"boolean\") {\n    throw new Error(\"C-style for loop condition must be a Bool expression\");\n  }\n}","typeGuard":"const isBool = (v: unknown): v is boolean => typeof v === \"boolean\";","tryCatchPattern":"try {\n  await runBaml(program);\n} catch (e) {\n  if (String(e).includes(\"C-style for loop condition must be boolean\")) {\n    // rewrite the condition as an explicit comparison (e.g. `i > 0`) and retry\n  } else { throw e; }\n}","preventionTips":["Always write C-for conditions as explicit comparisons (`i < n`), never bare values.","Don't port truthiness idioms (`while i`) from C/JS/Python into BAML.","Verify any helper called in the condition returns Bool.","Add a lint/test that flags non-comparison C-for conditions."],"tags":["baml","interpreter","loop","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}