Skip to content

engine: no compile path rejects duplicate canonical variable idents -- one twin silently dropped #885

Closed

Description

@bpowers

Summary

No compile path in simlin-engine rejects two variables whose names canonicalize to the same identifier. Canonicalization collapses case, whitespace, and underscores, so <flow name="Attrition"> vs <flow name="attrition">, or net flow vs net_flow, are the same canonical ident. A model containing such a pair compiles and simulates with one of the twins silently dropped (last-in-document-order wins on the salsa path) -- no diagnostic anywhere.

ErrorCode::DuplicateVariable exists but is raised only by edit operations in patch.rs; model sync/compile never emits it, so any reader or importer that tolerates duplicates (XMILE, JSON, protobuf, external tools writing datamodel directly) produces a silently-wrong simulation.

Evidence

  • XMILE reader stable-sorts variables by canonical ident but neither dedups nor rejects duplicates: src/simlin-engine/src/xmile/model.rs:539-564 (the sort at 561-564 makes duplicate pairs adjacent, then hands them downstream unchanged).
  • Salsa sync inserts variables into HashMaps keyed by canonical name, so a later duplicate silently replaces the earlier one: src/simlin-engine/src/db/sync.rs:284-293 (source_var_map.insert / variables.insert), and again in the incremental sync path (~lines 611-641).
  • The legacy/monolithic path does the same via HashMap<Ident<Canonical>, _> = variable_list.into_iter().map(|v| (Ident::new(v.ident()), v)).collect(): src/simlin-engine/src/model.rs:995 and :1099.
  • ErrorCode::DuplicateVariable (src/simlin-engine/src/common.rs:371) is raised only at src/simlin-engine/src/patch.rs:293 and :502 -- edit-time upsert/rename checks, never during compilation.

Why it matters

Components affected

  • src/simlin-engine/src/db/sync.rs (salsa sync -- the production compile entry)
  • src/simlin-engine/src/model.rs (legacy/monolithic path)
  • src/simlin-engine/src/xmile/model.rs and other readers/importers that tolerate duplicates

Suggested direction

Add duplicate-canonical-ident validation at model sync time (and/or as a model-level diagnostic in the salsa pipeline): when two datamodel variables in the same model canonicalize to the same ident, emit a loud ErrorCode::DuplicateVariable diagnostic naming both original spellings, instead of silently collapsing. Sync is the right chokepoint because every reader/importer funnels through it, and it sees the pre-collapse variable list. The legacy model.rs path and any pre-sync datamodel consumers (e.g. conveyor expansion) then inherit the guarantee or can keep local defensive handling.

Note the model-name analogue already has precedent: SourceProject.macro_declarations exists precisely because the canonical-name-keyed models map collapses duplicate model names needed for macro-registry validation (src/simlin-engine/src/db/macro_registry.rs) -- the same collapse happens per-variable with no corresponding validation.

Context

Identified while fixing #870 on the conveyor-engine branch (PR #869 review follow-ups): the panic there is reachable only because duplicate canonical flow idents survive to conveyor expansion, and investigating the assumed upstream validation showed it does not exist on any compile path. Related but distinct: #399 (systems-format translator can generate colliding idents -- a producer-side bug; this issue is the missing engine-side gate).

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