Skip to content

Commit

Permalink
Type avoidance in MT bound inference (#22142)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored Dec 9, 2024
2 parents c61897d + 045b92f commit 1775d0b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
/** Replace Ident nodes references to the underlying tree that defined them */
def underlying(using Context): Tree = MapToUnderlying().transform(tree)

/** Collect all the TypeSymbol's of the type Bind nodes in the tree. */
def bindTypeSymbols(using Context): List[TypeSymbol] =
tree.collectSubTrees { case b: Bind if b.isType => b.symbol.asType }

// --- Higher order traversal methods -------------------------------

/** Apply `f` to each subtree of this tree */
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4633,6 +4633,7 @@ object Types extends TypeUtils {
*/
def isUnreducibleWild(using Context): Boolean =
tycon.isLambdaSub && hasWildcardArg && !isMatchAlias
&& !(args.sizeIs == 1 && defn.isCompiletime_S(tycon.typeSymbol)) // S is a pseudo Match Alias

def tryCompiletimeConstantFold(using Context): Type =
if myEvalRunId == ctx.runId then myEvalued
Expand Down
16 changes: 3 additions & 13 deletions compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,31 +216,21 @@ class InlineReducer(inliner: Inliner)(using Context):
type TypeBindsMap = SimpleIdentityMap[TypeSymbol, java.lang.Boolean]

def getTypeBindsMap(pat: Tree, tpt: Tree): TypeBindsMap = {
val getBinds = new TreeAccumulator[Set[TypeSymbol]] {
def apply(syms: Set[TypeSymbol], t: Tree)(using Context): Set[TypeSymbol] = {
val syms1 = t match {
case t: Bind if t.symbol.isType =>
syms + t.symbol.asType
case _ => syms
}
foldOver(syms1, t)
}
}

// Extractors can contain Bind nodes in type parameter lists,
// for that case tree looks like this:
// UnApply[t @ t](pats)(implicits): T[t]
// Test case is pos/inline-caseclass.scala.
//
// Alternatively, for explicitly specified type binds in type annotations like in
// case A(B): A[t]
// the tree will look like this:
// Unapply[t](pats)(implicits) : T[t @ t]
// and the binds will be found in the type tree instead
// Test case is pos-macros/i15971
val tptBinds = getBinds(Set.empty[TypeSymbol], tpt)
val tptBinds = tpt.bindTypeSymbols.toSet
val binds: Set[TypeSymbol] = pat match {
case UnApply(TypeApply(_, tpts), _, _) =>
getBinds(Set.empty[TypeSymbol], tpts) ++ tptBinds
tpts.flatMap(_.bindTypeSymbols).toSet ++ tptBinds
case _ => tptBinds
}

Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,7 @@ trait TypeAssigner {
def assignType(tree: untpd.CaseDef, pat: Tree, body: Tree)(using Context): CaseDef = {
val ownType =
if (body.isType) {
val getParams = new TreeAccumulator[mutable.ListBuffer[TypeSymbol]] {
def apply(ps: mutable.ListBuffer[TypeSymbol], t: Tree)(using Context) = t match {
case t: Bind if t.symbol.isType => foldOver(ps += t.symbol.asType, t)
case _ => foldOver(ps, t)
}
}
val params1 = getParams(new mutable.ListBuffer[TypeSymbol](), pat).toList
val params1 = pat.bindTypeSymbols
val params2 = pat.tpe match
case AppliedType(tycon, args) =>
val tparams = tycon.typeParamSymbols
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
val lub = cases1.foldLeft(defn.NothingType: Type): (acc, case1) =>
if !acc.exists then NoType
else if case1.body.tpe.isProvisional then NoType
else acc | case1.body.tpe
else acc | TypeOps.avoid(case1.body.tpe, case1.pat.bindTypeSymbols)
if lub.exists then
if !lub.isAny then
val msg = em"Match type upper bound inferred as $lub, where previously it was defaulted to Any"
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotc/pos-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ i12299a.scala
i13871.scala
i15181.scala
i15922.scala
i15926.scala
t5031_2.scala
i16997.scala
i7414.scala
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i21256.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
type MTWithBind[X] = X match {
case List[t] => t
}
}

0 comments on commit 1775d0b

Please sign in to comment.