{"record":{"id":"3bf64e125e66baab","repo":"BoundaryML/baml","slug":"encountered-impossible-class-constructor-during-parsing","errorCode":null,"errorMessage":"Encountered impossible class constructor during parsing","messagePattern":"Encountered impossible class constructor during parsing","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-lib/ast/src/parser/parse_expression.rs","lineNumber":652,"sourceCode":"    if let Some(value) = value {\n        value\n    } else {\n        unreachable!(\"Encountered impossible jinja expression during parsing\")\n    }\n}\n\npub fn parse_class_constructor(token: Pair<'_>, diagnostics: &mut Diagnostics) -> Expression {\n    assert_correct_parser(&token, &[Rule::class_constructor], diagnostics);\n\n    let span = diagnostics.span(token.as_span());\n    let mut tokens = token.into_inner();\n\n    let ident_token = tokens.next().expect(\"Guaranteed by the grammar\");\n\n    let class_name = match ident_token.as_rule() {\n        Rule::identifier => parse_identifier(ident_token, diagnostics),\n        Rule::path_identifier => parse_path_identifier(ident_token, diagnostics),\n        _ => panic!(\"Encountered impossible class constructor during parsing\"),\n    };\n\n    let mut fields = Vec::new();\n    while let Some(field_or_close_bracket) = tokens.next() {\n        if field_or_close_bracket.as_str() == \"}\" {\n            break;\n        }\n        if field_or_close_bracket.as_str() == \",\" {\n            continue;\n        }\n        if field_or_close_bracket.as_rule() == Rule::NEWLINE {\n            continue;\n        }\n\n        assert_correct_parser(\n            &field_or_close_bracket,\n            &[Rule::class_field_value_pair],\n            diagnostics,","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-lib/ast/src/parser/parse_expression.rs#L634-L670","documentation":"A panic in parse_class_constructor when the first child token of a class_constructor pair is neither Rule::identifier nor Rule::path_identifier. The grammar is expected to constrain the first token to those two rules; anything else triggers panic!(\"Encountered impossible class constructor during parsing\"). This is a grammar/parser desynchronization bug rather than a designed user-facing error.","triggerScenarios":"parse_class_constructor receiving a class_constructor pair whose first inner token is an unexpected rule (e.g. a keyword or literal after a grammar change).","commonSituations":"Hit when BAML grammar .pest rules are edited without updating the match arms in the parser, or when hand-crafting parse trees in tests.","solutions":["Fix the class constructor declaration in your .bml file at the reported span (name must be a plain or dotted identifier)","Report the reproducing input to BAML as a parser bug","Update the match arms in parse_class_constructor if you modified the grammar to allow new rule types","Pin a BAML version where grammar and parser agree"],"exampleFix":"// before\nclass 123Invalid { }\n// after\nclass ValidName { }","handlingStrategy":"validation","validationCode":"// Reject class names that are not identifiers before parsing\nfn valid_class_name(src: &str) -> bool {\n    src.lines().filter_map(|l| l.trim().strip_prefix(\"class \"))\n        .all(|rest| {\n            let name: String = rest.chars().take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '.').collect();\n            !name.is_empty() && name.chars().next().map_or(false, |c| c.is_alphabetic() || c == '_')\n        })\n}","typeGuard":"fn is_identifier(s: &str) -> bool {\n    !s.is_empty()\n        && s.chars().next().map_or(false, |c| c.is_alphabetic() || c == '_')\n        && s.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '.')\n}","tryCatchPattern":"let ok = std::panic::catch_unwind(|| parse_class_constructor(pair, &mut diagnostics));\nif ok.is_err() { diagnostics.push(\"unrecognized class constructor name rule\"); }","preventionTips":["Use alphanumeric/underscore class names in .bml files","Keep grammar .pest rules and parse_class_constructor match arms in lockstep","Run parser round-trip tests after any grammar change","Validate inputs via the BAML CLI before embedding the parser"],"tags":["parser","rust","panic","grammar","internal-bug"],"backgroundTag":"internal-invariant-violation","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"}