Skip to content

Commit 5b30038

Browse files
committed
Fix problem comparing overloaded TermRefs
Overloaded TermRefs do not have an info, and consequently do not support =:=. Yet in Typer#checkNewOrShadowed we compared termrefs with =:=. This gives an exception if the termrefs are overloaded. The fix is to provide a new method isSameRef in TypeComparer which is called instead of =:= in Typer#checkNewOrShadowed.
1 parent ccb4f8a commit 5b30038

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,25 @@ class TypeComparer(initctx: Context) extends DotClass {
715715

716716
/** Two types are the same if are mutual subtypes of each other */
717717
def isSameType(tp1: Type, tp2: Type): Boolean =
718-
if (tp1 == NoType || tp2 == NoType) false
719-
else if (tp1 eq tp2) true
720-
else isSubType(tp1, tp2) && isSubType(tp2, tp1)
718+
isSubType(tp1, tp2) && isSubType(tp2, tp1)
719+
720+
/** Same as `isSameType` but also can be applied to overloaded TermRefs, where
721+
* two overloaded refs are the same if they have pairwise equal alternatives
722+
*/
723+
def isSameRef(tp1: Type, tp2: Type): Boolean = ctx.traceIndented(s"isSameRef($tp1, $tp2") {
724+
def isSubRef(tp1: Type, tp2: Type): Boolean = tp1 match {
725+
case tp1: TermRef if tp1.isOverloaded =>
726+
tp1.alternatives forall (isSubRef(_, tp2))
727+
case _ =>
728+
tp2 match {
729+
case tp2: TermRef if tp2.isOverloaded =>
730+
tp2.alternatives exists (isSubRef(tp1, _))
731+
case _ =>
732+
isSubType(tp1, tp2)
733+
}
734+
}
735+
isSubRef(tp1, tp2) && isSubRef(tp2, tp1)
736+
}
721737

722738
/** The greatest lower bound of two types */
723739
def glb(tp1: Type, tp2: Type): Type = /*>|>*/ ctx.traceIndented(s"glb(${tp1.show}, ${tp2.show})", typr, show = true) /*<|<*/ {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Typer extends Namer with Applications with Implicits {
244244
* does properly shadow the new one from an outer context.
245245
*/
246246
def checkNewOrShadowed(found: Type, newPrec: Int): Type =
247-
if (!previous.exists || (previous =:= found)) found
247+
if (!previous.exists || ctx.typeComparer.isSameRef(previous, found)) found
248248
else if ((prevCtx.scope eq ctx.scope) &&
249249
(newPrec == definition ||
250250
newPrec == namedImport && prevPrec == wildImport)) {

0 commit comments

Comments
 (0)