diff --git a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtojass/ImAttrType.java b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtojass/ImAttrType.java index 08790da85..92c94deb8 100644 --- a/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtojass/ImAttrType.java +++ b/de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtojass/ImAttrType.java @@ -1,6 +1,7 @@ package de.peeeq.wurstscript.translation.imtojass; import com.google.common.collect.Lists; +import de.peeeq.wurstscript.ast.FuncDef; import de.peeeq.wurstscript.jassIm.*; import de.peeeq.wurstscript.types.*; @@ -23,8 +24,25 @@ public static ImType getType(ImFuncRef e) { } public static ImType getType(ImFunctionCall e) { - ImType t = e.getFunc().getReturnType(); - t = substituteType(t, e.getTypeArguments(), e.getFunc().getTypeVariables()); + ImFunction function = e.getFunc(); + ImType t = function.getReturnType(); + // A direct instance call (such as an overloaded operator) carries its receiver as the first argument. + // Before generic elimination lifts class variables onto the function, its self parameter still uses them. + if (!function.getParameters().isEmpty() + && function.getParameters().get(0).getType() instanceof ImClassType self + && !self.getClassDef().getTypeVariables().isEmpty() + && !self.getTypeArguments().isEmpty() + && self.getTypeArguments().get(0).getType() instanceof ImTypeVarRef first + && first.getTypeVariable().equals(self.getClassDef().getTypeVariables().get(0)) + && function.getTrace() instanceof FuncDef source && source.attrIsDynamicClassMember() + && !e.getArguments().isEmpty() + && e.getArguments().get(0).attrTyp() instanceof ImClassType receiver) { + ImClassType owner = asSuperclass(receiver, self.getClassDef()); + if (owner != null && owner.getTypeArguments().size() == self.getClassDef().getTypeVariables().size()) { + t = substituteType(t, owner.getTypeArguments(), self.getClassDef().getTypeVariables()); + } + } + t = substituteType(t, e.getTypeArguments(), function.getTypeVariables()); if (e.getTuplesEliminated()) { if (t instanceof ImTupleType) { ImTupleType tt = (ImTupleType) t; diff --git a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OpOverloading.java b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OpOverloading.java index 8b568b3d6..d905256f5 100644 --- a/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OpOverloading.java +++ b/de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/OpOverloading.java @@ -232,6 +232,36 @@ public void testOverloading_indexReadMissing() { "endpackage"); } + @Test + public void genericIndexReadInConditionalArgument() { + test().testLua(true).luaOnly(false).stacktraces().executeProg().lines( + "package test", + "native testSuccess()", + "tuple Pair(real x, real y)", + "class Box", + " T value", + " construct(T value)", + " this.value = value", + " function op_index(int i) returns T", + " return value", + "class PairBox extends Box", + " construct(Pair value)", + " super(value)", + "function matches(Pair pair, real x, real y) returns boolean", + " return pair.x == x and pair.y == y", + "function check(boolean readBox, Box box, real x, real y) returns boolean", + " return matches(readBox ? box[0] : Pair(4., 8.), x, y)", + "function checkInherited(boolean readBox, PairBox box, real x, real y) returns boolean", + " return matches(readBox ? box[0] : Pair(4., 8.), x, y)", + "init", + " let box = new Box(Pair(1., 2.))", + " let inherited = new PairBox(Pair(16., 32.))", + " if check(true, box, 1., 2.) and check(false, box, 4., 8.)", + " if checkInherited(true, inherited, 16., 32.) and checkInherited(false, inherited, 4., 8.)", + " testSuccess()" + ); + } + @Test public void testOverloading_indexWrite() { testAssertOkLines(true,