Skip to content

Commit 4f3a71a

Browse files
committed
Patmat: Use less type variables in prefix inference
In code like: class Outer: sealed trait Foo case class Bar() extends Foo def mat(foo: Foo) = foo match case Bar() => When in the course of decomposing the scrutinee's type, which is `Outer.this.Foo`, we're trying to instantiate subclass `Outer.this.Bar`, the `Outer.this` is fixed - it needn't be inferred, via type variables and type bounds. Cutting down on type variables, particularly when GADT symbols are also present, can really speed up the operation, including making code that used to hang forever compile speedily.
1 parent b8ad7b1 commit 4f3a71a

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools
22
package dotc
33
package core
44

5-
import Contexts._, Types._, Symbols._, Names._, Flags._
5+
import Contexts._, Types._, Symbols._, Names._, NameKinds.*, Flags._
66
import SymDenotations._
77
import util.Spans._
88
import util.Stats
@@ -839,40 +839,59 @@ object TypeOps:
839839
}
840840
}
841841

842+
object TraverseTp2 extends TypeTraverser:
843+
val singletons = new mutable.ListBuffer[SingletonType]
844+
val gadtSyms = new mutable.ListBuffer[Symbol]
845+
def traverse(tp: Type) =
846+
val tpd = tp.dealias
847+
if tpd ne tp then traverse(tpd)
848+
else tp match
849+
case tp: SingletonType if !singletons.contains(tp)=>
850+
singletons += tp
851+
traverseChildren(tp)
852+
case tp: TypeRef if tp.symbol.isAbstractOrParamType =>
853+
gadtSyms += tp.symbol
854+
traverseChildren(tp)
855+
case _ =>
856+
traverseChildren(tp)
857+
TraverseTp2.traverse(tp2)
858+
val singletons = TraverseTp2.singletons.toList
859+
val gadtSyms = TraverseTp2.gadtSyms.toList
860+
842861
// Prefix inference, replace `p.C.this.Child` with `X.Child` where `X <: p.C`
843862
// Note: we need to strip ThisType in `p` recursively.
844863
//
845864
// See tests/patmat/i3938.scala
846865
class InferPrefixMap extends TypeMap {
847866
var prefixTVar: Type | Null = null
848867
def apply(tp: Type): Type = tp match {
849-
case ThisType(tref: TypeRef) if !tref.symbol.isStaticOwner =>
868+
case ThisType(tref) if !tref.symbol.isStaticOwner =>
850869
val symbol = tref.symbol
851-
if (symbol.is(Module))
870+
val singletonIx = singletons.indexWhere(_.classSymbol == symbol)
871+
if singletonIx >= 0 then
872+
prefixTVar = singletons(singletonIx)
873+
prefixTVar.uncheckedNN
874+
else if symbol.is(Module) then
852875
TermRef(this(tref.prefix), symbol.sourceModule)
853876
else if (prefixTVar != null)
854877
this(tref)
855878
else {
856879
prefixTVar = WildcardType // prevent recursive call from assigning it
857-
val tvars = tref.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
880+
val tvars = tref.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds, DepParamName.fresh(tparam.paramName)) }
858881
val tref2 = this(tref.applyIfParameterized(tvars))
859-
prefixTVar = newTypeVar(TypeBounds.upper(tref2))
882+
prefixTVar = newTypeVar(TypeBounds.upper(tref2), DepParamName.fresh(tref.name))
860883
prefixTVar.uncheckedNN
861884
}
862885
case tp => mapOver(tp)
863886
}
864887
}
865888

866889
val inferThisMap = new InferPrefixMap
867-
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
890+
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds, DepParamName.fresh(tparam.paramName)) }
868891
val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)
869892

870-
val getAbstractSymbols = new TypeAccumulator[List[Symbol]]:
871-
def apply(xs: List[Symbol], tp: Type) = tp.dealias match
872-
case tp: TypeRef if tp.symbol.exists && !tp.symbol.isClass => foldOver(tp.symbol :: xs, tp)
873-
case tp => foldOver(xs, tp)
874-
val syms2 = getAbstractSymbols(Nil, tp2).reverse
875-
if syms2.nonEmpty then ctx.gadtState.addToConstraint(syms2)
893+
if gadtSyms.nonEmpty then
894+
ctx.gadtState.addToConstraint(gadtSyms)
876895

877896
// If parent contains a reference to an abstract type, then we should
878897
// refine subtype checking to eliminate abstract types according to

tests/patmat/i12408.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
13: Pattern Match Exhaustivity: X[<?>] & (X.this : X[T]).A(_), X[<?>] & (X.this : X[T]).C(_)
1+
13: Pattern Match Exhaustivity: A(_), C(_)
22
21: Pattern Match

tests/pos/i16785.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class VarImpl[Lbl, A]
2+
3+
class Outer[|*|[_, _], Lbl1]:
4+
type Var[A1] = VarImpl[Lbl1, A1]
5+
6+
sealed trait Foo[G]
7+
case class Bar[T, U]()
8+
extends Foo[Var[T] |*| Var[U]]
9+
10+
def go[X](scr: Foo[Var[X]]): Unit = scr match // was: compile hang
11+
case Bar() => ()

0 commit comments

Comments
 (0)