Skip to content

Commit

Permalink
Assert correct sorting without duplicates (#150)
Browse files Browse the repository at this point in the history
We seem to be hitting some JDK or hardware problem that causes array sorting to produce incorrect results. This check prevents them from making it into a generated file unnoticed.
  • Loading branch information
szeiger authored May 25, 2022
1 parent 256c037 commit a3516ad
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sjsonnet/src/sjsonnet/Materializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ abstract class Materializer {
storePos(obj.pos)
obj.triggerAllAsserts(obj)
val objVisitor = visitor.visitObject(obj.visibleKeyNames.length , -1)
obj.foreachElement(!evaluator.settings.preserveOrder, evaluator.emptyMaterializeFileScopePos) { (k, v) =>
val sort = !evaluator.settings.preserveOrder
var prevKey: String = null
obj.foreachElement(sort, evaluator.emptyMaterializeFileScopePos) { (k, v) =>
storePos(v)
objVisitor.visitKeyValue(objVisitor.visitKey(-1).visitString(k, -1))
objVisitor.visitValue(
apply0(v, objVisitor.subVisitor.asInstanceOf[Visitor[T, T]]),
-1
)
if(sort) {
if(prevKey != null && k.compareTo(prevKey) <= 0)
Error.fail(s"""Internal error: Unexpected key "$k" after "$prevKey" in sorted object materialization""")
prevKey = k
}
}
objVisitor.visitEnd(-1)
case Val.Num(pos, n) => storePos(pos); visitor.visitFloat64(n, -1)
Expand Down

0 comments on commit a3516ad

Please sign in to comment.