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 @@ -322,7 +322,11 @@ public static LocalState runFunc(ProgramState globalState, ImFunction f, @Nullab
if (p0t instanceof ImClassType sigThisType) {
ImClass cls = sigThisType.getClassDef();
ImTypeVars tvars = cls.getTypeVariables();
ImTypeArguments concreteArgs = receiverObj.getType().getTypeArguments();
// A method the receiver's class inherits is bound by the receiver's type as an instance of
// the method's class, through the superclasses (Sub extends G<int>), as ImAttrType does.
ImClassType asMethodClass = globalState.adaptToSuperclass(receiverObj.getType(), cls);
ImTypeArguments concreteArgs = (asMethodClass != null ? asMethodClass : receiverObj.getType())
.getTypeArguments();

int n = Math.min(tvars.size(), concreteArgs.size());
for (int i2 = 0; i2 < n; i2++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ public void pushStackframeWithTypes(ImFunction f, @Nullable ILconstObject receiv
return ownerInstantiation instanceof ImClassType ownerType ? adaptToOwner(ownerType, owner) : null;
}

private @Nullable ImClassType adaptToSuperclass(ImClassType ct, ImClass owner) {
/** {@code ct} as an instantiation of {@code owner}, through the superclass edges, or null if it is none. */
public @Nullable ImClassType adaptToSuperclass(ImClassType ct, ImClass owner) {
if (ct.getClassDef() == owner) return ct;

// Walk super types, substituting this ct's type args into the superclass types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1540,4 +1540,119 @@ public void typeIdOfGenericInstantiationReadAtCompileTimeMatchesOnLua() {
);
}

/** An object made with a superclass's type parameter in a method inherited from it has the subclass's instantiation. */
@Test
public void instanceofOfObjectMadeInInheritedGenericMethod() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"interface Marker",
"class Box<T:> implements Marker",
" T item",
"class G<T:>",
Comment thread
Frotty marked this conversation as resolved.
Resolved
" function make() returns Marker",
" return new Box<T>()",
"class Sub extends G<int>",
"init",
" let m = new Sub().make()",
" if m instanceof Box<int> and not (m instanceof Box<string>)",
" testSuccess()"
);
}

/** The same through two generic superclasses, the nearer of which forwards its own parameter. */
@Test
public void instanceofOfObjectMadeInMethodInheritedThroughTwoGenericClasses() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"interface Marker",
"class Box<T:> implements Marker",
" T item",
"class G<T:>",
" function make() returns Marker",
" return new Box<T>()",
"class H<U:> extends G<U>",
"class Sub extends H<int>",
"init",
" let m = new Sub().make()",
" if m instanceof Box<int> and not (m instanceof Box<string>)",
" testSuccess()"
);
}

/** The same for a default method of a generic interface. */
@Test
public void instanceofOfObjectMadeInInheritedInterfaceDefaultMethod() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"interface Marker",
"class Box<T:> implements Marker",
" T item",
"interface Maker<T:>",
" function make() returns Marker",
" return new Box<T>()",
"class Sub implements Maker<int>",
"init",
" let m = new Sub().make()",
" if m instanceof Box<int> and not (m instanceof Box<string>)",
" testSuccess()"
);
}

/** A method inherited from a generic class reads the statics of the subclass's instantiation. */
@Test
public void genericStaticReadInInheritedGenericMethod() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class G<T:>",
" static T last",
" function put(T v)",
" last = v",
" function get() returns T",
" return last",
"class SubI extends G<int>",
"class SubS extends G<string>",
"init",
" new SubI().put(3)",
" new SubS().put(\"x\")",
" if new SubI().get() == 3 and new SubS().get() == \"x\" and new G<int>().get() == 3",
" testSuccess()"
);
}

/**
* The same inherited methods with old generics, whose type parameter is erased: the value a method inherited
* through one or two generic superclasses returns is the object stored, in the interpreter and on both targets.
*/
@Test
public void oldGenericValueReturnedByInheritedMethod() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class A",
" int tag",
" construct(int tag)",
" this.tag = tag",
"class G<T>",
" T val",
" function set(T v)",
" val = v",
" function get() returns T",
" return val",
"class H<U> extends G<U>",
"class Sub extends G<A>",
"class Sub2 extends H<A>",
"init",
" let s = new Sub",
" s.set(new A(7))",
" let s2 = new Sub2",
" s2.set(new A(9))",
" if s.get().tag == 7 and s2.get().tag == 9",
" testSuccess()"
);
}

}
Loading