Repository navigation
Description
Summary
The MDL writer silently rebinds references to user variables whose names collide with Vensim control variables or bare builtin keywords (Time Step, Time, Initial Time, Final Time, SAVEPER). The definition line is emitted with the user's name, but every reference to the variable is printed as the builtin keyword, so re-importing the exported MDL silently changes simulation results.
Repro
An XMILE model with:
- aux
Time Step=5 - aux
if=Time_Step * 2
exports to MDL where:
- the definition line reads
Time Step = 5(user variable, kept), but - the reference in
if's equation is routed throughmdl_bare_keyword(src/simlin-engine/src/mdl/writer.rs:181, used in the expression printer's zero-argAppbranch around line 648) and printed as the builtinTIME STEP.
On re-import, the equation reads the simulation dt (DT * 2) instead of the user's variable -- silently changing results. The exported file also contains two conflicting TIME STEP definitions: the user's and the .Control section's.
The same class of corruption applies to user variables named Time, Initial Time, Final Time, and SAVEPER.
Related import-direction sibling: PI
A user variable literally named PI (e.g. test/metasd/industrial-dynamics/IDch15/IDch15d.mdl has PI=3.14159) has its references rewritten to the engine builtin pi() on import, which then exports as unparseable PI(). Same root confusion between user-variable names and zero-arity builtin/keyword names, in the opposite direction.
Root cause
The MDL expression printer is a context-free printer today: it has no access to the model's variable-name set, so it cannot distinguish "zero-arg builtin call that should print as a bare keyword" from "reference to a user variable that happens to share the keyword's canonical name". (Upstream, the XMILE-side parse likely resolves the colliding name as the zero-arity builtin in the first place, so the corruption may begin before the writer -- see #681 for the same shadowing class with inf.)
Why it matters
- Severity: high -- silent semantic corruption. Simulation results change on an export/import round trip with no diagnostic.
- Exported MDL can also be structurally invalid (duplicate
TIME STEPdefinitions; unparseablePI()).
Components affected
src/simlin-engine/src/mdl/writer.rs(mdl_bare_keyword, expression printerwalk)- XMILE-side equation parsing / builtin resolution (zero-arity builtin shadowing of user variables)
- MDL import (PI direction)
Possible approaches
- Thread the model's variable-name set into the MDL expression printer so keyword-vs-variable is decided per name; quote or rename colliding user variables on export (and keep the reference spelling consistent with the definition line).
- On the import/parse side, prefer a declared user variable over a zero-arity builtin when a name collides (mirroring whatever resolution engine: bare builtin name silently shadows a same-named variable #681 lands on for
inf). - Add round-trip tests for user variables named
Time Step,Time,Initial Time,Final Time,SAVEPER, andPI.
Related issues
- mdl: macro parameter named like a control variable is ignored #552 -- import-direction sibling: macro formal parameter named like a control variable is silently dropped on MDL import (distinct mechanism).
- engine: bare builtin name silently shadows a same-named variable #681 -- variable named
inf(or other zero-arity builtin name) collides with the builtin and silently reads +infinity (same shadowing class, XMILE side).
Discovery context
Identified while auditing MDL export fidelity: the writer's bare-keyword mapping was observed rebinding references without consulting the model's variable names.
Activity