Skip to content

Commit 4fd7a90

Browse files
committed
Add subtype-based fallback in inferPrefixMap
add new line second approach revert previous approach address reviews
1 parent e9cb3bb commit 4fd7a90

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,13 @@ object TypeOps:
805805
prefixTVar.uncheckedNN
806806
case ThisType(tref) if !tref.symbol.isStaticOwner =>
807807
val symbol = tref.symbol
808+
val compatibleSingleton = singletons.valuesIterator.find(_.underlying.derivesFrom(symbol))
808809
if singletons.contains(symbol) then
809810
prefixTVar = singletons(symbol) // e.g. tests/pos/i16785.scala, keep Outer.this
810811
prefixTVar.uncheckedNN
812+
else if compatibleSingleton.isDefined then
813+
prefixTVar = compatibleSingleton.get
814+
prefixTVar.uncheckedNN
811815
else if symbol.is(Module) then
812816
TermRef(this(tref.prefix), symbol.sourceModule)
813817
else if (prefixTVar != null)
@@ -905,10 +909,11 @@ object TypeOps:
905909
}
906910

907911
val inferThisMap = new InferPrefixMap
908-
val tvars = tp1.etaExpand match
912+
val prefixInferredTp = inferThisMap(tp1)
913+
val tvars = prefixInferredTp.etaExpand match
909914
case eta: TypeLambda => constrained(eta)
910915
case _ => Nil
911-
val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)
916+
val protoTp1 = prefixInferredTp.appliedTo(tvars)
912917

913918
if gadtSyms.nonEmpty then
914919
ctx.gadtState.addToConstraint(gadtSyms)

tests/warn/i23369.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Module {
2+
type BarTy
3+
sealed trait Adt[A]
4+
case class Foo() extends Adt[String]
5+
case class Bar[A <: BarTy](x: BarTy) extends Adt[A]
6+
}
7+
8+
object Basic extends Module {
9+
type BarTy = String
10+
}
11+
12+
def test(a: Basic.Adt[String]) = {
13+
a match { // warn: match may not be exhaustive
14+
case Basic.Foo() =>
15+
}
16+
}
17+
18+
object Basic2 extends Module {
19+
type BarTy = Int
20+
}
21+
22+
def test2(a: Basic2.Adt[String]) = {
23+
a match {
24+
case Basic2.Foo() =>
25+
}
26+
}

0 commit comments

Comments
 (0)