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
@@ -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.*;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:>",
" T value",
" construct(T value)",
" this.value = value",
" function op_index(int i) returns T",
" return value",
"class PairBox extends Box<Pair>",
" 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<Pair> 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>(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,
Expand Down
Loading