@@ -61,6 +61,8 @@ private record RuntimeTypeUse(ImClass clazz, GenericTypes generics) {
6161 // NEW: Track specialized global variables for generic static fields
6262 // Key: (original generic global var, concrete type instantiation) -> specialized var
6363 private final Table <ImVar , GenericTypes , ImVar > specializedGlobals = HashBasedTable .create ();
64+ /** Last specialized initializer emitted for each original initializer, preserving discovery order. */
65+ private final Map <ImStmt , ImStmt > specializedInitializerTails = new IdentityHashMap <>();
6466
6567 // NEW: Track which global vars belong to which generic class
6668 // This helps us know which globals need specialization
@@ -2188,10 +2190,14 @@ private void createSpecializedGlobals(ImClass originalClass, GenericTypes generi
21882190 ImLExpr newLeft = specializeLhs .apply (origSet .getLeft ());
21892191 ImSet specSet = JassIm .ImSet (originalGlobal .attrTrace (), newLeft , rhs );
21902192
2191- // schedule insertion right after origSet in its parent ImStmts
2193+ // Append after earlier specializations of this initializer. Each invocation of
2194+ // createSpecializedGlobals has its own insertion batch; always inserting after
2195+ // origSet would therefore reverse specialization discovery/initializer order.
2196+ ImStmt insertionPoint = specializedInitializerTails .getOrDefault (origSet , origSet );
21922197 IdentityHashMap <ImStmt , List <ImStmt >> byStmt =
21932198 insertsByParent .computeIfAbsent (parentStmts , k -> new IdentityHashMap <>());
2194- byStmt .computeIfAbsent (origSet , k -> new ArrayList <>(1 )).add (specSet );
2199+ byStmt .computeIfAbsent (insertionPoint , k -> new ArrayList <>(1 )).add (specSet );
2200+ specializedInitializerTails .put (origSet , specSet );
21952201
21962202 // keep prog.getGlobalInits consistent, but do NOT reuse the tree-attached node elsewhere
21972203 specializedInitsForMap .add ((ImSet ) specSet .copy ());
0 commit comments