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 @@ -140,11 +140,40 @@ public static ImType getType(ImMethodCall mc) {
returnType = substituteType(returnType, mc.getTypeArguments(), mc.getMethod().getImplementation().getTypeVariables());
ImType rt = mc.getReceiver().attrTyp();
if (rt instanceof ImClassType ct) {
// A method the receiver's class inherits returns its own class's type variables, whose arguments the
// receiver's type gives through the superclasses (Sub extends G<pa>).
ImClassType methodClassType = asSuperclass(ct, mc.getMethod().attrClass());
if (methodClassType != null) {
ct = methodClassType;
}
returnType = substituteType(returnType, ct.getTypeArguments(), ct.getClassDef().getTypeVariables());
}
return returnType;
}

/**
* The type {@code ct} has as an instance of {@code superclass}, or null if its class is not that or below it, or if
* {@code ct} leaves out type arguments of its class, which the superclasses' arguments may name.
*/
private static @org.eclipse.jdt.annotation.Nullable ImClassType asSuperclass(ImClassType ct, ImClass superclass) {
if (ct.getClassDef() == superclass) {
return ct;
}
if (ct.getTypeArguments().size() != ct.getClassDef().getTypeVariables().size()) {
return null;
}
for (ImClassType direct : ct.getClassDef().getSuperClasses()) {
ImType st = substituteType(direct, ct.getTypeArguments(), ct.getClassDef().getTypeVariables());
if (st instanceof ImClassType superType) {
ImClassType result = asSuperclass(superType, superclass);
if (result != null) {
return result;
}
}
}
return null;
}

public static ImType getType(ImMemberAccess e) {
ImType t = e.getVar().getType();
ImType receiverType1 = e.getReceiver().attrTyp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2505,4 +2505,80 @@ public void genericOverrideOverriddenAgainBySubclassWithInterface() {
" testSuccess()");
}

/** Runs a program on Jass and on Lua; optimised adds -inline -localOptimizations to every run, the Lua one too. */
private void runOnBothTargets(String name, boolean optimised, String... lines) {
TestConfig config = testNamed(name).testLua(true).luaOnly(false).executeProg();
if (optimised) {
config = config.inline().localOptimizations();
}
config.compilationUnits(new CU(name, String.join("\n", lines) + "\n"));
}

/**
* A field of the tuple which a method of a generic superclass returns. The translation keeps the result in a
* temporary of the call's IM type, which named the superclass's type parameter when the receiver was a subclass.
*/
private static final String[] TUPLE_FIELD_OF_INHERITED_GENERIC_METHOD = {
"package test",
"native testSuccess()",
"tuple pa(int x, int y)",
"class G<T:>",
" static T sv",
" function w(T v)",
" sv = v",
" function r() returns T",
" return sv",
"class Sub extends G<pa>",
"class GenericSub<U:> extends G<U>",
"init",
" let g = new Sub()",
" g.w(pa(1, 2))",
" let first = g.r().x == 1 and g.r().y == 2",
" let h = new GenericSub<pa>()",
" h.w(pa(3, 4))",
" if first and h.r().x == 3 and h.r().y == 4",
" testSuccess()"
};

@Test
public void tupleFieldOfInheritedGenericMethodResult() {
runOnBothTargets("tupleFieldOfInheritedGenericMethodResult", false, TUPLE_FIELD_OF_INHERITED_GENERIC_METHOD);
}

@Test
public void tupleFieldOfInheritedGenericMethodResult_opt() {
runOnBothTargets("tupleFieldOfInheritedGenericMethodResult_opt", true, TUPLE_FIELD_OF_INHERITED_GENERIC_METHOD);
}

/**
* The same call as a branch of a conditional expression, whose temporary takes the branch's IM type. Only without
* the optimiser: the inliner folds the conditional away before the temporary's type matters.
*/
private static final String[] CONDITIONAL_OF_INHERITED_GENERIC_METHOD = {
"package test",
"native testSuccess()",
"tuple pa(int x, int y)",
"class G<T:>",
" static T sv",
" function w(T v)",
" sv = v",
" function r() returns T",
" return sv",
"class Sub extends G<pa>",
"function pick(Sub g, boolean b) returns pa",
" return b ? g.r() : pa(0, 0)",
"init",
" let g = new Sub()",
" g.w(pa(1, 2))",
" let p = pick(g, true)",
" let q = pick(g, false)",
" if p.x == 1 and p.y == 2 and q.x == 0 and q.y == 0",
" testSuccess()"
};

@Test
public void conditionalOfInheritedGenericMethodResult() {
runOnBothTargets("conditionalOfInheritedGenericMethodResult", false, CONDITIONAL_OF_INHERITED_GENERIC_METHOD);
}

}
Loading