Repository navigation
Description
Description
format_mdl_ident in src/simlin-engine/src/mdl/writer.rs (around line 89) applies underbar_to_space to the whole identifier BEFORE checking whether it is a quoted identifier. In Vensim MDL, quoted names are literal -- underscores inside quotes are part of the name -- so this rewrites the interior of quoted identifiers and silently renames variables on export.
Two concrete failure modes (verified via a simlin-cli convert round-trip):
- Interior corruption: MDL
"rate_of_change!" = 5round-trips as"rate of change!"-- a different identifier in Vensim. - Quoting dropped entirely:
"a_b c"round-trips as the UNQUOTEDa b c. After the underscore-to-space conversion,needs_mdl_quotingno longer sees any character requiring quotes, so the quotes are stripped.
Why it matters
Silent rename on export is a correctness bug: a model exported to MDL and opened in Vensim (or re-imported) has different variable identities than the source model. Case 2 additionally changes the lexical class of the name (quoted literal vs. normal ident), which can collide with or diverge from other names.
Part of the space-vs-underscore ambiguity is baked in by simlin's canonicalization (spaces and underscores collapse), so a perfect fix may need canonicalization-aware handling. But at minimum the writer should not rewrite the interior of identifiers that are already quoted: detect the quoted form first, and pass the quoted body through literally.
Components affected
src/simlin-engine/src/mdl/writer.rs--format_mdl_ident(ordering ofunderbar_to_spacevs. quote detection) andneeds_mdl_quotingsrc/simlin-engine/src/mdl/reader -- for verifying the round-trip contract
Possible approaches
- In
format_mdl_ident, check for (or strip) the surrounding quotes before applyingunderbar_to_space; emit the quoted body verbatim. - Ensure
needs_mdl_quotingis evaluated against the original (pre-conversion) name so that names which required quoting keep their quotes. - Add re-parsing round-trip tests: the existing escape tests in
writer_tests.rsare string-level only and never re-parse the writer output. This overlaps with mdl: test saving and re-reading names with special characters #335, which asks for a special-character-identifier round-trip integration test; that test should cover quoted identifiers containing underscores.
Context
Discovered during MDL writer investigation; verified with simlin-cli convert round-trips. Related: #335 (round-trip integration test gap for special-character identifiers).
Severity: medium (silent rename on export).
Activity