Fix nondeterministic private field ordering in Lifter#459
Merged
LPTK merged 10 commits intohkust-taco:hkmc2from Apr 15, 2026
Merged
Fix nondeterministic private field ordering in Lifter#459LPTK merged 10 commits intohkust-taco:hkmc2from
LPTK merged 10 commits intohkust-taco:hkmc2from
Conversation
The private fields generated by the lifter had nondeterministic ordering because they were built by iterating over Map values derived from Set operations. Scala's Set and Map do not guarantee stable iteration order. The fix uses sorted ordered lists (passedSymsOrdered, capturesOrdered, passedDefnsOrdered, and new liftedObjsOrdered) to look up map values in a deterministic order based on symbol UIDs, instead of iterating map values directly. Agent-Logs-Url: https://github.com/LPTK/mlscript/sessions/07ac81ed-dfff-467c-af13-4a25818d2faa
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses nondeterministic ordering of generated private fields in the lifter by avoiding iteration over Set/Map.values (which have no guaranteed stable order) and instead indexing map entries via UID-sorted key lists. This reduces “no-op” diffs in generated output caused by field reordering.
Changes:
- Introduce
liftedObjsOrdered(UID-sorted) and use it instead ofliftedObjsSyms.values.toListwhen emittingprivateFieldsfor rewritten classes/companions. - Make
LiftedClass.extraPrivSymsdeterministic by building it via the already-ordered key lists (passedDefnsOrdered,capturesOrdered,passedSymsOrdered) instead ofMap.values. - Update the golden lifter output test to reflect the new stable field order.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| hkmc2/shared/src/main/scala/hkmc2/codegen/Lifter.scala | Reworks private-field list construction to use UID-sorted keys rather than Map.values iteration. |
| hkmc2/shared/src/test/mlscript/lifter/ClassInFun.mls | Updates expected output ordering for private fields in a lifter snapshot test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Author
|
Argh, closed because I mistakenly deleted the source branch... |
…le ScopedInfo ordering Sort reqCaptures by the stable UIDs of the original symbols/scopes (via a new given Ordering[ScopedInfo]) instead of by freshly-created VarSymbol UIDs that depend on nondeterministic Set iteration order. Also iterate reqCaptures in sorted order when building capSymsMap_ so that VarSymbol UID allocation is deterministic. Agent-Logs-Url: https://github.com/LPTK/mlscript/sessions/27dbdac4-4044-4c61-b8ca-808142c295c6 Co-authored-by: LPTK <2545083+LPTK@users.noreply.github.com>
Contributor
Author
CAG2Mark
reviewed
Apr 15, 2026
…Uid[Symbol]] instead of toInt - capSymsMap_ reverted to iterate reqCaptures directly (sorting before .toMap has no effect on the resulting Map) - Use existing given Ordering[Uid[A]] instead of adding toInt extension - Remove toInt from Uid.scala Agent-Logs-Url: https://github.com/LPTK/mlscript/sessions/da829502-087c-4ed2-bf08-2d2834b1f5a4 Co-authored-by: LPTK <2545083+LPTK@users.noreply.github.com>
…into copilot/fix-deterministic-ordering # Conflicts: # hkmc2/shared/src/test/mlscript/handlers/Effects.mls
# Conflicts: # hkmc2/shared/src/test/mlscript/codegen/FirstClassFunctionTransform.mls
be9bd44 to
6f91b57
Compare
CAG2Mark
approved these changes
Apr 15, 2026
Contributor
CAG2Mark
left a comment
There was a problem hiding this comment.
Pls merge this first and then it will be good to go: https://github.com/CAG2Mark/mlscript/commits/fix-ordering-2/
Co-authored-by: Mark Ng <55091936+CAG2Mark@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The private fields generated by the lifter had nondeterministic ordering because they were built by iterating over Map values derived from Set operations. Scala's Set and Map do not guarantee stable iteration order.
The fix uses sorted ordered lists (passedSymsOrdered, capturesOrdered, passedDefnsOrdered, and new liftedObjsOrdered) to look up map values in a deterministic order based on symbol UIDs, instead of iterating map values directly.
Agent-Logs-Url: https://github.com/LPTK/mlscript/sessions/07ac81ed-dfff-467c-af13-4a25818d2faa
Original PR comment by Copilot:
Private field order in generated code was nondeterministic across runs, caused by iterating Scala
Map.values/Setelements which have no guaranteed iteration order. This manifested as "bump test" commits that only reordered fields likex¹andy²swapping.Changes
LiftedClass.extraPrivSyms: ReplacedMap.valuesconcatenation with lookups through the already-sortedpassedDefnsOrdered,capturesOrdered,passedSymsOrderedlists — matching the ordering convention used byauxParamsandformatArgsRewrittenClass/RewrittenCompanion: ReplacedliftedObjsSyms.values.toListwithliftedObjsOrdered.map(liftedObjsSyms)inprivateFieldsconstructionGenericRewrittenScope/ClsLikeRewrittenScope: AddedliftedObjsOrdered: List[InnerSymbol]sorted byuid, paralleling the existingpassedSymsOrdered/capturesOrdered/passedDefnsOrderedpattern