Skip to content

engine: print_eqn emits stacked unary negatives (--x) that the equation parser rejects #863

Closed

Description

@bpowers

Problem

crate::ast::print_eqn and the equation parser (Expr0::new, LexerType::Equation) disagree on stacked unary negatives, so print_eqn can emit XMILE equation text that does not round-trip through the parser.

Minimal repro AST:

Expr0::Op1(Negative, Op1(Negative, App("pi", [])))

print_eqn serializes this as --pi(), but Expr0::new rejects --pi(). The printer is therefore not a total inverse of the parser: for a doubly-negated sub-expression it produces text the parser will not accept.

This is a property of the shared AST printer + equation parser -- independent of the MDL writer (it is not about mdl_bare_keyword / PI() rendering as in #850; the pi() in the repro is incidental, any inner expression triggers it).

Why it matters

Defense-in-depth / correctness of the printer<->parser contract. print_eqn output is expected to re-parse to an equivalent AST. Any consumer that serializes an AST via print_eqn and re-parses (round-trip harnesses, the MDL writer's equation_to_mdl re-parse fallback, potential future tooling) can be tripped by such an AST.

Impact / reachability

Effectively unreachable from real models today. Every stored datamodel equation originated from the parser, which never produces stacked -- (a leading -- is rejected at parse time), so no real equation's AST can contain Op1(Negative, Op1(Negative, _)). The concern is a latent printer/parser asymmetry, not a live data-corruption bug.

  • On the MDL-writer side specifically, equation_to_mdl falls back to raw text when it cannot re-parse. In principle that path could leak unparseable XMILE into MDL output for such an equation -- but since the offending AST cannot arise from a real datamodel equation, this is not reachable in production.

How it was discovered

Surfaced by the MDL-writer property tests (branch `mdl-writer-hardening`, `src/simlin-engine/src/mdl/writer_proptest.rs`). The proptest currently works around it with a `prop_filter` that restricts generated ASTs to those whose `print_eqn` output re-parses.

Component(s)

  • `src/simlin-engine/src/ast.rs` (`print_eqn`)
  • the equation parser (`Expr0::new`, `LexerType::Equation`)

Possible approaches

  • Make `print_eqn` parenthesize or space stacked unary negatives, e.g. emit `-(-x)` instead of `--x`; or
  • Make the equation parser accept `--x` (stacked unary minus).

Either fixes the round-trip contract; the printer-side change is the more conservative option. Once fixed, the `prop_filter` workaround in `writer_proptest.rs` can be removed.

Severity

Low (unreachable from real models; defense-in-depth for the printer/parser contract).

Activity

  1. bpowers commented on Jul 8, 2026

    @bpowers
    OwnerAuthor

    Fresh minimal reproducer from a flaky trip of mdl::writer::proptest_tests::equation_write_reparse_is_fixpoint (RNG draw during unrelated review on branch conveyor-engine):

    expr = Op1(Negative, Op2(Exp, Op1(Negative, Const("0")), App(sum(arr[*]))))
    i.e.  -((-0) ^ SUM(arr[*]))
    

    proptest seed (from the transient proptest-regressions file, not committed so the unrelated branch's pre-commit gate isn't held hostage to this pre-existing bug):

    cc b9f5e68dcc3465aac430b5a8c2172adc709ebf051da7abe8dfc0e5f23d4b4cca
    
  2. bpowers commented on Jul 9, 2026

    @bpowers
    OwnerAuthor

    Reachability correction: this is reachable from real models, via the MDL importer rather than print_eqn.

    Vensim accepts a space-separated double unary minus, and the importer's xmile_compat formatter collapses it to --. Verified at e1c1cca9:

    MDL source `x = - -3`  ->  imported datamodel equation "--3"  ->  Expr0::new REJECTS it
    

    So the "every stored datamodel equation originated from the parser, which never produces stacked --" argument in the Impact section does not hold: the MDL parser produces one. The consequence is that a legal Vensim model imports as a project whose variable cannot compile.

    Downstream, the equation_to_mdl raw-text fallback then leaks XMILE spelling into exported MDL (INT(0) instead of INTEGER(0)), which the next import reads as a lookup call on an undefined table -- filed separately as #912, with the full repro chain.

    Found by mdl::writer::proptest_tests::equation_write_reparse_is_fixpoint (the prop_filter only guards the first write's input; the stacked -- re-enters via parse_mdl(mdl1)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions