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 @@ -44,7 +44,7 @@ public void union(T x, T y) {
// merge yRoot into xRoot
parent.put(yRoot, xRoot);
if (rank.get(xRoot).equals(rank.get(yRoot))) {
rank.put(xRoot, rank.get(xRoot));
rank.put(xRoot, rank.get(xRoot) + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import de.peeeq.wurstscript.translation.imtranslation.FunctionFlagCompiletime;
import de.peeeq.wurstscript.translation.imtranslation.FunctionFlagEnum;

import java.util.ArrayDeque;
import java.util.Deque;

public class ImAttributes {


Expand Down Expand Up @@ -80,18 +77,12 @@ public static de.peeeq.wurstscript.ast.Element getTrace(ElementWithTrace t) {
return t.getTrace();
}

/**
* The trace of the nearest enclosing element that has one; its source contains this element.
* A traced child or sibling is not used: its source covers only part of this element, or none of it.
*/
public static de.peeeq.wurstscript.ast.Element getTrace(Element t) {
Deque<Element> q = new ArrayDeque<>();
q.add(t);
while (q.isEmpty()) {
Element e = q.removeFirst();
if (e == null) {
continue;
}
q.add(e.getParent());
for (int i = 0; i < e.size(); i++) {
q.add(e.get(i));
}
for (Element e = t; e != null; e = e.getParent()) {
if (e instanceof ElementWithTrace elementWithTrace) {
return elementWithTrace.getTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public static String fileName(String path) {
}

public static String printException(Throwable e) {
return e + "\n" + Utils.printExceptionWithStackTrace(e);
return printExceptionWithStackTrace(e);
}


Expand Down
31 changes: 31 additions & 0 deletions de.peeeq.wurstscript/src/test/java/tests/utils/UnionFindTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tests.utils;

import de.peeeq.datastructures.UnionFind;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class UnionFindTests {

@Test
public void unionKeepsTheTallerTreeAsRoot() {
UnionFind<String> uf = new UnionFind<>();
// a and b form a tree of rank 1 with root a
uf.union("a", "b");
// the singleton c has rank 0, so it goes below a, whichever side of the union it is on
uf.union("c", "a");
assertEquals(uf.find("c"), "a");
assertEquals(uf.find("b"), "a");
}

@Test
public void unionOfEqualRanksMergesTheSecondIntoTheFirst() {
UnionFind<String> uf = new UnionFind<>();
uf.union("a", "b");
uf.union("c", "d");
uf.union("c", "a");
assertEquals(uf.find("a"), "c");
assertEquals(uf.find("b"), "c");
assertEquals(uf.find("d"), "c");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tests.wurstscript.tests;

import de.peeeq.wurstscript.WurstOperator;
import de.peeeq.wurstscript.ast.Ast;
import de.peeeq.wurstscript.ast.ExprIntVal;
import de.peeeq.wurstscript.ast.NoExpr;
import de.peeeq.wurstscript.jassIm.ImFunction;
import de.peeeq.wurstscript.jassIm.ImFunctionCall;
import de.peeeq.wurstscript.jassIm.ImOperatorCall;
import de.peeeq.wurstscript.jassIm.ImSet;
import de.peeeq.wurstscript.jassIm.ImVar;
import de.peeeq.wurstscript.jassIm.ImVarAccess;
import de.peeeq.wurstscript.jassIm.JassIm;
import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.imtranslation.CallType;
import org.testng.annotations.Test;

import java.util.Collections;

import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;

/**
* An IM element without a trace of its own reports the trace of the nearest enclosing element which
* has one: that source contains the element. Error positions, the Jass source map and the traces of
* elements the optimiser makes from an expression all read it.
*/
public class ImTraceTests {

private static ExprIntVal source(String value) {
return Ast.ExprIntVal(new WPos("test.wurst", null, 0, 0), value);
}

private static ImVar var(String name) {
return JassIm.ImVar(source(name), JassIm.ImSimpleType("integer"), name, false);
}

private static ImFunction function(String name, ImVar local, ImSet body) {
return JassIm.ImFunction(source(name), name, JassIm.ImTypeVars(), JassIm.ImVars(), JassIm.ImVoid(),
JassIm.ImVars(local), JassIm.ImStmts(body), Collections.emptyList());
}

@Test
public void anUntracedExpressionTakesTheTraceOfItsNearestTracedAncestor() {
ImVar x = var("x");
ImVarAccess read = JassIm.ImVarAccess(x);
ImOperatorCall sum = JassIm.ImOperatorCall(WurstOperator.PLUS, JassIm.ImExprs(read, JassIm.ImIntVal(1)));
ExprIntVal setTrace = source("set");
ImSet set = JassIm.ImSet(setTrace, JassIm.ImVarAccess(x), sum);
function("f", x, set);

assertSame(sum.attrTrace(), setTrace);
assertSame(read.attrTrace(), setTrace);
assertSame(sum.getArguments().attrTrace(), setTrace);
}

@Test
public void anArgumentTakesTheTraceOfItsCallNotOfASiblingArgument() {
ImVar x = var("x");
ImFunction g = JassIm.ImFunction(source("g"), "g", JassIm.ImTypeVars(), JassIm.ImVars(), JassIm.ImVoid(),
JassIm.ImVars(), JassIm.ImStmts(), Collections.emptyList());
ImVarAccess argument = JassIm.ImVarAccess(x);
ExprIntVal siblingTrace = source("sibling");
ImFunctionCall sibling = JassIm.ImFunctionCall(siblingTrace, g, JassIm.ImTypeArguments(), JassIm.ImExprs(),
false, CallType.NORMAL);
ExprIntVal callTrace = source("call");
ImFunctionCall call = JassIm.ImFunctionCall(callTrace, g, JassIm.ImTypeArguments(),
JassIm.ImExprs(JassIm.ImOperatorCall(WurstOperator.PLUS, JassIm.ImExprs(argument, sibling))),
false, CallType.NORMAL);
function("f", x, JassIm.ImSet(source("set"), JassIm.ImVarAccess(x), call));

assertSame(argument.attrTrace(), callTrace);
assertSame(sibling.attrTrace(), siblingTrace);
}

@Test
public void aDetachedUntracedElementHasNoTrace() {
ImVar x = var("x");
ImOperatorCall sum = JassIm.ImOperatorCall(WurstOperator.PLUS, JassIm.ImExprs(JassIm.ImVarAccess(x), JassIm.ImIntVal(1)));

assertTrue(sum.attrTrace() instanceof NoExpr, String.valueOf(sum.attrTrace()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,29 @@ public void aCompilerOwnedDeclarationUsingAStackTraceIsStillLeftAlone() throws I
assertTrue("stack traces must actually be on", compiled.contains("wurst_stack_depth"));
}

/**
* A concatenation lowered to the nil-safe helper is a call the IM operator it replaces did not
* have, so its stack-trace position comes from the enclosing statement. It used to be empty.
*/
@Test
public void aLoweredConcatenationKeepsItsPositionInAStackTrace() throws IOException {
test().testLua(true).stacktraces().withStdLib().lines(
"package Test",
"function describe(int a) returns string",
" return getStackTraceString() + a.toString()",
"init",
" print(describe(1))",
"endpackage");

String compiled = Files.toString(
new File("test-output/lua/LuaTranslationTests_aLoweredConcatenationKeepsItsPositionInAStackTrace.lua"),
Charsets.UTF_8);

assertTrue("the concatenation should go through the helper",
compiled.contains("\"when calling __wurst_stringConcat in aLoweredConcatenationKeepsItsPositionInAStackTrace, line 3\""));
assertFalse("no helper call may lose its position", compiled.contains("\"when calling __wurst_stringConcat\""));
}

@Test
public void keyedTableStaysNativeWithStackTraces() throws IOException {
test().testLua(true).stacktraces().inline().withStdLib().lines(keyedTableSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void compactCliSuppressesGeneratedJassNameResolutionWarnings() {
Assert.assertEquals(regularGui.getWarningList().size(), 1);
}

@Test
public void printExceptionPrintsTheExceptionOnce() {
String printed = Utils.printException(new IllegalStateException("boom", new RuntimeException("cause")));
Assert.assertTrue(printed.startsWith("java.lang.IllegalStateException: boom\n"), printed);
Assert.assertEquals(printed.split("IllegalStateException: boom", -1).length - 1, 1, printed);
Assert.assertTrue(printed.contains("Caused by: java.lang.RuntimeException: cause\n"), printed);
}

@Test
public void array() {
int[] ar1 = {1, 2, 3};
Expand Down
Loading