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
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ public LuaCompilationUnit transformProgToLua() {
beginPhase(2, "remove unreachable functions");
TreeShaker.removeUnreachableFunctions(getImTranslator());
timeTaker.endPhase();
imTranslator.assertNoLiveErasedGenericStatic();
// Before stack traces: that pass appends a parameter to every affected function, and on
// Lua every non-native function is affected, so the exact signatures the keyed-table
// operations are recognised by would stop matching - silently leaving their Jass bodies on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.jassIm.Element;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.lua.translation.RemoveGarbage;
import de.peeeq.wurstscript.types.*;
import de.peeeq.wurstscript.utils.Pair;
import de.peeeq.wurstscript.utils.Utils;
Expand Down Expand Up @@ -2968,6 +2969,42 @@ public void assertFlat(String where) {
}
}

/**
* In a unit test: fails when live code reads or writes a generic static through its erased original although the
* static has specialised copies. One instantiation then has two stores, and one is written where the other is read
* (wurstscript/WurstScript#1498). Live is {@link RemoveGarbage#reachableFunctions}, because the tree shaker keeps
* dispatch to classes nothing allocates. Asked before the garbage removal, which deletes a store nothing reads and
* so turns the split into a dropped write nothing can see any more.
*/
public void assertNoLiveErasedGenericStatic() {
if (!isUnitTestMode || genericStaticOwners.isEmpty()) {
return;
}
for (ImFunction function : RemoveGarbage.reachableFunctions(imProg, this)) {
function.accept(new Element.DefaultVisitor() {
@Override
public void visit(ImVarAccess access) {
super.visit(access);
check(access, access.getVar());
}

@Override
public void visit(ImVarArrayAccess access) {
super.visit(access);
check(access, access.getVar());
}

private void check(Element access, ImVar global) {
if (genericStaticOwnerOf(global) != null && specialisationOf(global) == null) {
throw new CompileError(access, "Internal error: " + function.getName()
+ " uses the erased generic static " + global.getName()
+ ", which has specialised copies.");
}
}
});
}
}

private final Map<ExprClosure, ImClass> classForClosure = Maps.newLinkedHashMap();

public ImClass getClassForClosure(ExprClosure s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ public static Set<ImMethod> reachableMethods(ImProg prog, ImTranslator translato
return collectUsed(prog, translator).getMethods();
}

/**
* Read-only function reachability. A method counts only once its class is allocated, so
* unlike for the tree shaker, dispatch to a class nothing allocates is not live.
*/
public static Set<ImFunction> reachableFunctions(ImProg prog, ImTranslator translator) {
return collectUsed(prog, translator).getFunctions();
}

private static Used collectUsed(ImProg prog, ImTranslator translator) {
return collectUsed(prog, translator, Collections.emptySet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
import de.peeeq.wurstscript.RunArgs;
import de.peeeq.wurstscript.ast.WurstModel;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
import de.peeeq.wurstscript.jassIm.ImFunction;
import de.peeeq.wurstscript.jassIm.ImProg;
import de.peeeq.wurstscript.jassIm.ImSet;
import de.peeeq.wurstscript.jassIm.ImVar;
Expand All @@ -31,6 +33,7 @@
import java.util.Random;
import java.util.TreeMap;

import static org.testng.Assert.expectThrows;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
Expand Down Expand Up @@ -2889,6 +2892,55 @@ public void detachedGenericStaticInitializerRemainsMetadataOnly() {
2, storages.size());
}

@Test
public void liveErasedGenericStaticFailsTheUnitTestCheck() {
RunArgs runArgs = new RunArgs().with("-lua");
WurstGuiCliImpl gui = new WurstGuiCliImpl();
WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(null, gui, null, runArgs);
compiler.getErrorHandler().enableUnitTestMode();
WurstModel model = parseFiles(Collections.emptyList(), Collections.singletonList(new CU(
"liveErasedGenericStaticFailsTheUnitTestCheck.wurst", String.join("\n",
"package Test",
"native testSuccess()",
"class Slot<T:>",
" static T value",
" function set(T newValue)",
" value = newValue",
" function get() returns T",
" return value",
"init",
" let ints = new Slot<int>()",
" let strings = new Slot<string>()",
" ints.set(7)",
" strings.set(\"ok\")",
" if ints.get() == 7 and strings.get() == \"ok\"",
" testSuccess()"
))), false, compiler);
assertTrue("unexpected parse/type errors: " + gui.getErrorList(), gui.getErrorList().isEmpty());
compiler.checkProg(model);
assertTrue("unexpected compile errors: " + gui.getErrorList(), gui.getErrorList().isEmpty());
compiler.translateProgToIm(model);
compiler.runCompiletime(WurstProjectConfigData.empty(), false, false);
ImProg prog = compiler.getImProg();

// The split store of wurstscript/WurstScript#1498: live code which still uses the erased
// static after both instantiations have their own copy. A read, because the tree shaker
// drops an effect-free write to a static nothing reads any more.
ImVar value = prog.getGlobals().stream()
.filter(global -> global.getName().contains("value"))
.findFirst()
.orElseThrow(() -> new AssertionError("expected the translated Slot.value global"));
ImFunction main = prog.getFunctions().stream()
.filter(function -> function.getName().equals("main"))
.findFirst()
.orElseThrow(() -> new AssertionError("expected main"));
main.getBody().add(JassIm.ImVarAccess(value));

CompileError error = expectThrows(CompileError.class, compiler::transformProgToLua);
assertTrue(error.getMessage(), error.getMessage().contains(value.getName())
&& error.getMessage().contains("main"));
}

@Test
public void constructedErasedInstantiationDoesNotDuplicateSpecializedStaticInitializer() {
test().testLua(true).executeProg().lines(
Expand Down
Loading