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 @@ -181,9 +181,20 @@ public static ImType getType(ImMemberAccess e) {
ImTypeArguments typeArgs = e.getTypeArguments();
try {
if (typeArgs.isEmpty()) {
typeArgs = receiverType.getTypeArguments();
// A field the receiver's class inherits has its own class's type variables, whose arguments the
// receiver's type gives through the superclasses (Sub extends G<int>).
ImClassType ct = receiverType;
Element fields = e.getVar().getParent();
if (fields != null && fields.getParent() instanceof ImClass fieldClass) {
ImClassType fieldClassType = asSuperclass(receiverType, fieldClass);
if (fieldClassType != null) {
ct = fieldClassType;
}
}
t = substituteType(t, ct.getTypeArguments(), ct.getClassDef().getTypeVariables());
} else {
t = substituteType(t, typeArgs, receiverType.getClassDef().getTypeVariables());
}
t = substituteType(t, typeArgs, receiverType.getClassDef().getTypeVariables());

if (!e.getIndexes().isEmpty()) {
if (t instanceof ImArrayType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1655,4 +1655,88 @@ public void oldGenericValueReturnedByInheritedMethod() {
);
}

/** A field of type T which a non-generic subclass inherits has the superclass's type argument as its type. */
@Test
public void inheritedGenericFieldReadInConditional() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class G<T:>",
" T v",
"class Sub extends G<int>",
" construct()",
" v = 3",
"init",
" let s = new Sub()",
" let b = s != null",
" let z = b ? s.v : 5",
" if z == 3",
" testSuccess()"
);
}

/** The same through the implicit receiver in a method of the subclass. */
@Test
public void inheritedGenericFieldReadInConditionalInSubclassMethod() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class G<T:>",
" T v",
"class Sub extends G<int>",
" construct()",
" v = 3",
" function get(bool b) returns int",
" return b ? v : 5",
"init",
" if new Sub().get(true) == 3",
" testSuccess()"
);
}

/** A field inherited through two generic superclasses has the argument the chain passes down as its type. */
@Test
public void genericFieldInheritedTwoLevelsReadInConditional() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class G<T:>",
" T v",
"class H<U:> extends G<U>",
"class Sub extends H<int>",
" construct()",
" v = 3",
"init",
" let s = new Sub()",
" let b = s != null",
" let z = b ? s.v : 5",
" if z == 3",
" testSuccess()"
);
}

/** An inherited field takes its type from the superclass argument a generic subclass passes to that position. */
@Test
public void genericFieldInheritedFromSecondTypeParameterReadInConditional() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package Test",
"native testSuccess()",
"class G<A:, B:>",
" A a",
" B v",
"class Sub<U:> extends G<int, U>",
" construct(U u)",
" a = 1",
" v = u",
"init",
" let s = new Sub<string>(\"x\")",
" let t = new Sub<int>(3)",
" let b = s != null",
" let y = b ? s.v : \"y\"",
" let z = b ? t.v : 5",
" if y == \"x\" and z == 3 and s.a + t.a == 2",
" testSuccess()"
);
}

}
Loading