BoundaryML/baml · error

expected function call when parsing method call

Error message

expected function call when parsing method call

What it means

This is a `unreachable!()` panic in `parse_expression`'s postfix method-call handling in the BAML AST parser. When the grammar rule `fn_app` is parsed as a postfix operator (a method call `receiver.method(...)`), the result of `parse_fn_call` must be an `Expression::App`; any other variant breaks the contract and the parser panics. This indicates the function-call parser returned something other than a call expression.

Source

Thrown at engine/baml-lib/ast/src/parser/parse_expression.rs:109

                    Expression::FieldAccess(Box::new(left), field, span.clone())
                }

                Rule::method_call => {
                    let inner = operator.into_inner().next()?;

                    match inner.as_rule() {
                        Rule::fn_app => match parse_fn_app(inner, diagnostics)? {
                            Expression::App(fn_call) => Expression::MethodCall {
                                receiver: Box::new(left),
                                method: fn_call.name,
                                args: fn_call.args,
                                type_args: fn_call.type_args,
                                span: span.clone(),
                            },

                            _ => {
                                unreachable!("expected function call when parsing method call")
                            }
                        },

                        Rule::generic_fn_app => match parse_generic_fn_app(inner, diagnostics)? {
                            Expression::App(fn_call) => Expression::MethodCall {
                                receiver: Box::new(left),
                                method: fn_call.name,
                                args: fn_call.args,
                                type_args: fn_call.type_args,
                                span: span.clone(),
                            },

                            _ => {
                                unreachable!("expected function call when parsing method call")
                            }
                        },

                        _ => unreachable!("Unexpected method call rule: {:?}", inner.as_rule()),

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Simplify the offending method call expression in the BAML source (e.g. split chained calls into intermediate variables) to bypass the mis-parse
  2. Check the BAML source at the panic span for unusual method-call syntax (generics on methods, chained calls) and rewrite it in a plainer form
  3. If you are a contributor: make `parse_fn_call` return `Expression::App` unconditionally on the `fn_app` path, or handle other variants instead of `unreachable!()`
  4. Upgrade BAML to a build where fn_app parsing and this match agree

Example fix

// before (may hit the panic)
let v = obj.method1().method2();
// after
let a = obj.method1();
let v = a.method2();
Defensive patterns

Strategy: validation

Validate before calling

// Avoid exotic method-call shapes before parsing:
function assertSimpleMethodCall(expr) {
  if (/\.\s*[\w<>]+\s*\(/.test(expr) && (expr.match(/\./g) || []).length > 2) {
    throw new Error('Deeply chained method calls may hit a parser invariant; simplify: ' + expr);
  }
}

Type guard

const isAppCall = (parsed) => parsed && parsed.type === 'App';

Try / catch

catch (panicOutput) {
  if (String(panicOutput).includes('expected function call when parsing method call')) {
    logParserBug('parse_fn_app returned non-App expression', panicOutput);
    return null;
  }
  throw panicOutput;
}

Prevention

When it happens

Trigger: Parsing `receiver.method(args)` where `parse_fn_app(inner, diagnostics)` returns a non-`App` expression variant — e.g. after `parse_fn_call` is changed to return `Expression::Identifier` or `Expression::MethodCall` in some path.

Common situations: Contributors refactoring `parse_fn_call`/`parse_fn_app` so it can return variants other than `App`; running a mismatched grammar/parser build where the `fn_app` rule produces unexpected shapes while parsing chained method calls on strings, arrays, or maps.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/bc94299a41b73e9e. Report an issue: GitHub.