Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ of save formats, `ChunkedString`, hashes, or `Serializable`.

* Field iteration expands after module expansion, when inherited and injected fields are concrete.
* Jass may use normal generic elimination. Lua specialization remains targeted to paths that reach generic
construction; do not turn this into general Lua generic monomorphization.
construction and to instantiations whose members need their type argument
(`EliminateGenerics.allocatesFromSpecialisedClass`); do not turn this into general Lua generic monomorphization.
* Lua reachability must traverse both `ImFunctionCall` and `ImMethodCall`, including dispatch submethods.
* Targeted specialized methods needed by Lua dispatch must remain attached to the IM classes consumed by
`LuaDispatchPreparation`. Concrete implementations for erased generic objects must bind the same root slot used
Expand All @@ -450,8 +451,10 @@ of save formats, `ChunkedString`, hashes, or `Serializable`.
(`FieldIterationTests.genericConstructionOnFreshlyConstructedReceiver`).
* Lua generic-construction dispatch through multi-parameter generic interfaces is outside the supported loader
shape. The supported generic loader has a single construction type parameter.
* Do not call `wurstNewInstance<T>()` from the constructor of a generic class. Construct the simple state object in the
generic loader, then initialize any nested state explicitly after construction.
* `wurstNewInstance<T>()` in the constructor of a generic class, in an `ondestroy`, or behind a `super` call is supported
on both backends: on Lua each reaches a copy specialised for its instantiation
(`FieldIterationTests.genericConstructionInAConstructorAnOndestroyAndBehindSuper`, and the `NEW_INSTANCE` cases of
`GenericSpecialisationMatrixTests`).
* `wurstNewInstance<T>()` is a runtime Jass/Lua construction surface, not a supported use inside `compiletime(...)`
evaluation: the interpreter happens to evaluate the marker (`EvaluateExpr.evaluateGenericNew`), but no test covers it.
Do not rely on it or extend it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
roundFunctions = allFunctions;
}

// On Lua a field of a specialised class has the storage of the field it was copied from
// (LuaTranslator.fieldStorage), so a value written through the one is read through the other: the
// specialised methods of an object reach the copy, the erased code beside them the original.
final Set<ImVar> readFieldStorage = trans.isLuaTarget() ? readFieldStorage(readVars) : Set.of();

if (variablesLost) {
for (ImClass c : prog.getClasses()) {
// A field of a specialised class is a copy which nothing refers to, an access made
Expand All @@ -285,7 +290,8 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
// with no fields while the emitted code goes on reading them.
changes |= c.getFields().retainAll(c.getFields().stream()
.filter(field -> readVars.contains(field)
|| readVars.contains(trans.canonical(field)))
|| readVars.contains(trans.canonical(field))
|| readFieldStorage.contains(trans.canonical(field)))
.collect(Collectors.toCollection(LinkedHashSet::new)));
}
}
Expand Down Expand Up @@ -335,7 +341,8 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
}
} else if(e.getLeft() instanceof ImMemberAccess) {
ImMemberAccess va = ((ImMemberAccess) e.getLeft());
if (!readVars.contains(va.getVar()) && !NamePreservation.isPreserved(va.getVar())) {
if (!readVars.contains(va.getVar()) && !readFieldStorage.contains(trans.canonical(va.getVar()))
&& !NamePreservation.isPreserved(va.getVar())) {
replacements.add(Pair.create(e, effectsOfAssignment(e, sideEffectAnalyzer)));
}
}
Expand Down Expand Up @@ -364,6 +371,17 @@ private boolean garbageRound(boolean incremental, @Nullable List<ImVar> newlyUnr
return changes;
}

/** The storage of the fields which are read: the field each read field was copied from, or itself. */
private Set<ImVar> readFieldStorage(Set<ImVar> readVars) {
Set<ImVar> storage = Collections.newSetFromMap(new IdentityHashMap<>());
for (ImVar v : readVars) {
if (v.getParent() != null && v.getParent().getParent() instanceof ImClass) {
storage.add(trans.canonical(v));
}
}
return storage;
}

/**
* Puts what each of the assignments does besides assigning in its place, in the statement list it is in, as
* statements. A function which was flat stays flat: nothing is wrapped in a statement expression for a later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public ImType case_ImTypeVarRef(ImTypeVarRef t) {
ImTypeVar newTypevar = result.get(oldTypevar);
if (newTypevar == null) {
newTypevar = JassIm.ImTypeVar(oldTypevar.getName() + "_captured");
// The same parameter as the one it captures: what the closure's body reads of the enclosing
// class, such as a static, belongs to the instantiation the closure was made for.
tr.recordSpecialisation(newTypevar, oldTypevar);
result.put(oldTypevar, newTypevar);
c.getTypeVariables().add(newTypevar);
thisType.getTypeArguments().add(
Expand Down
Loading
Loading