Skip to content

Commit 858b609

Browse files
committed
WIP: handle inherited classes
1 parent e8e295a commit 858b609

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,8 +1516,8 @@ object SymDenotations {
15161516
*
15171517
* See tests/neg/i19619/Test.scala
15181518
*/
1519-
def javaTypeRef(using Context) =
1520-
TypeRef(maybeOwner.reachablePrefix.widen, symbol)
1519+
def javaTypeRef(from: Symbol)(using Context) =
1520+
TypeRef(from.reachablePrefix.widen, symbol)
15211521

15221522
/** Like typeRef, but objects in the prefix are represented by their singleton type,
15231523
* this means we output `pre.O.member` rather than `pre.O$.this.member`.

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ class Namer { typer: Typer =>
15521552
if (cls.isRefinementClass) ptype
15531553
else {
15541554
val pt = checkClassType(ptype, parent.srcPos,
1555-
traitReq = parent ne parents.head, stablePrefixReq = true)
1555+
traitReq = parent ne parents.head, stablePrefixReq = !ctx.isJava)
15561556
if (pt.derivesFrom(cls)) {
15571557
val addendum = parent match {
15581558
case Select(qual: Super, _) if Feature.migrateTo3 =>

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
463463
defDenot.symbol.namedType
464464
}
465465
else if (ctx.isJava && defDenot.symbol.isClass) {
466-
// in a java context a raw identifier to a class should have a widened prefix.
467-
defDenot.symbol.javaTypeRef
466+
val correctScope = if curOwner.is(ModuleClass) then curOwner.companionClass else curOwner
467+
val from =
468+
if correctScope != defDenot.symbol.maybeOwner then correctScope // inherited
469+
else defDenot.symbol.maybeOwner // defined in the same class
470+
defDenot.symbol.javaTypeRef(from)
468471
} else {
469472
val effectiveOwner =
470473
if (curOwner.isTerm && defDenot.symbol.maybeOwner.isType)

tests/run/i19619/InnerClassSub.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// InnerClass.java
2+
3+
package lib;
4+
5+
public class InnerClassSub extends InnerClass {
6+
7+
public class InnerSub<U> extends Inner<U> {
8+
public InnerSub(U innerField) {
9+
super(innerField);
10+
}
11+
}
12+
13+
public class OuterSub<U> extends Outer<U> {
14+
public OuterSub() {
15+
super();
16+
}
17+
}
18+
19+
public <U> Inner<U> createInnerSub(U innerField) {
20+
return new InnerSub<>(innerField);
21+
}
22+
23+
public <U, V> Outer<U>.Nested<V> createNestedSub(U outerField, V innerField) {
24+
OuterSub<U> outer = new OuterSub<>();
25+
return outer.new Nested<>(outerField, innerField);
26+
}
27+
28+
public <U> InnerClass.Inner<U> createInnerSub2(U innerField) {
29+
return new InnerSub<>(innerField);
30+
}
31+
32+
public <U, V> InnerClass.Outer<U>.Nested<V> createNestedSub2(U outerField, V innerField) {
33+
OuterSub<U> outer = new OuterSub<>();
34+
return outer.new Nested<>(outerField, innerField);
35+
}
36+
37+
public static <U> InnerClass.Inner<U> createInnerStatic(U innerField) {
38+
InnerClassSub innerClass = new InnerClassSub();
39+
return innerClass.new Inner<>(innerField);
40+
}
41+
42+
public static <U, V> InnerClass.Outer<U>.Nested<V> createNestedStatic(U outerField, V innerField) {
43+
InnerClassSub innerClass = new InnerClassSub();
44+
InnerClassSub.Outer<U> outer = innerClass.new Outer<>();
45+
return outer.new Nested<>(outerField, innerField);
46+
}
47+
48+
public static <U, V> void consumeNestedStatic(InnerClass.Outer<U>.Nested<V> nested) {
49+
}
50+
51+
public static <U, V> void consumeNestedStatic2(Outer<U>.Nested<V> nested) {
52+
}
53+
54+
}

tests/run/i19619/Test.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import lib.InnerClass
22
import lib.InnerClassGen
33
import lib.RawTypes
4+
import lib.InnerClassSub
45

56
@main def Test =
67

@@ -40,3 +41,19 @@ import lib.RawTypes
4041

4142
RawTypes.mii_Raw_Raw(cd_ii_Raw)
4243
RawTypes.mii_Raw_Raw2(cd_ii_Raw)
44+
45+
locally:
46+
val ici: InnerClassSub = new InnerClassSub()
47+
// val ici_inner1: ici.Inner[Long] = ici.createInner[Long](47L) // error
48+
val ici_inner2: InnerClassSub#Inner[Long] = ici.createInnerSub[Long](47L)
49+
val ici_inner2_2: InnerClass#Inner[Long] = ici.createInnerSub2[Long](47L)
50+
val ici_inner3: InnerClass#Inner[Long] = InnerClassSub.createInnerStatic[Long](47L)
51+
52+
val ici_outer: InnerClassSub#Outer[Long] = new ici.Outer[Long]()
53+
val ici_nested1: InnerClassSub#Outer[Long]#Nested[Int] = new ici_outer.Nested[Int](47L, 23)
54+
val ici_nested2: InnerClassSub#Outer[Long]#Nested[Int] = ici.createNestedSub[Long, Int](47L, 23)
55+
val ici_nested2_2: InnerClass#Outer[Long]#Nested[Int] = ici.createNestedSub2[Long, Int](47L, 23)
56+
val ici_nested3: InnerClass#Outer[Long]#Nested[Int] = InnerClassSub.createNestedStatic[Long, Int](47L, 23)
57+
58+
InnerClass.consumeNestedStatic(ici_nested3)
59+
InnerClass.consumeNestedStatic2(ici_nested3)

0 commit comments

Comments
 (0)