@@ -850,11 +850,15 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
850
850
}
851
851
852
852
/** When called from `pre1.A <:< pre2.A` does `pre1` relate to `pre2` so that
853
- * the subtype test is true? This is the case if `pre1 <:< pre2`, or
854
- * `pre1` and `pre2` are both this-types of related classes. Here, two classes
855
- * are related if each of them has a self type that derives from the other.
853
+ * the subtype test is true? This is the case if
856
854
*
857
- * This criterion is a bit dubious. I.e. in the test
855
+ * 1. `pre1 <:< pre2`, or
856
+ * 2. One of `pre1` and `pre2` refers to a package and the other to a
857
+ * package object in that package, or
858
+ * 3. `pre1` and `pre2` are both this-types of related classes.
859
+ *
860
+ * Here, two classes are related if each of them has a self type that derives from the other.
861
+ * The third criterion is a bit dubious. I.e. in the test
858
862
*
859
863
* A.this.T <:< B.this.T
860
864
*
@@ -874,18 +878,32 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
874
878
|* does not conform to dotty.tools.dotc.util.Property.Key[Typer.this.Deriver & Namer.this.Deriver]
875
879
*/
876
880
def isSubPrefix (pre1 : Type , pre2 : Type ): Boolean =
881
+ def samePkg (sym1 : Symbol , sym2 : Symbol ) =
882
+ sym2.is(Package ) && sym1.isPackageObject && sym1.owner == sym2.moduleClass
883
+ || sym1.is(Package ) && sym2.isPackageObject && sym2.owner == sym1.moduleClass
877
884
pre1 match
878
885
case pre1 : ThisType =>
879
886
pre2 match
880
887
case pre2 : ThisType =>
888
+ if samePkg(pre1.cls, pre2.cls) then return true
881
889
if pre1.cls.classInfo.selfType.derivesFrom(pre2.cls)
882
890
&& pre2.cls.classInfo.selfType.derivesFrom(pre1.cls)
883
891
then
884
892
subtyping.println(i " assume equal prefixes $pre1 $pre2" )
885
893
return true
894
+ case pre2 : TermRef =>
895
+ if samePkg(pre1.cls, pre2.symbol) then return true
896
+ case _ =>
897
+ case pre1 : TermRef =>
898
+ pre2 match
899
+ case pre2 : TermRef =>
900
+ if samePkg(pre1.symbol, pre2.symbol) then return true
901
+ case pre2 : ThisType =>
902
+ if samePkg(pre1.symbol, pre2.cls) then return true
886
903
case _ =>
887
904
case _ =>
888
905
isSubType(pre1, pre2)
906
+ end isSubPrefix
889
907
890
908
/** Compare `tycon[args]` with `other := otherTycon[otherArgs]`, via `>:>` if fromBelow is true, `<:<` otherwise
891
909
* (we call this relationship `~:~` in the rest of this comment).
0 commit comments