Skip to content

engine: MDL writer corrupts quoted identifiers containing underscores, can drop quoting entirely #846

Closed

Description

@bpowers

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):

  1. Interior corruption: MDL "rate_of_change!" = 5 round-trips as "rate of change!" -- a different identifier in Vensim.
  2. Quoting dropped entirely: "a_b c" round-trips as the UNQUOTED a b c. After the underscore-to-space conversion, needs_mdl_quoting no 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 of underbar_to_space vs. quote detection) and needs_mdl_quoting
  • src/simlin-engine/src/mdl/ reader -- for verifying the round-trip contract

Possible approaches

  1. In format_mdl_ident, check for (or strip) the surrounding quotes before applying underbar_to_space; emit the quoted body verbatim.
  2. Ensure needs_mdl_quoting is evaluated against the original (pre-conversion) name so that names which required quoting keep their quotes.
  3. Add re-parsing round-trip tests: the existing escape tests in writer_tests.rs are 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

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