Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type avoidance in MT bound inference #22142

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compiletime.ops.S is intrinsic right? Maybe we could alter symbol / type somehow (in definitions?) to make isMatchAlias true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there's an argument for that, but I didn't feel like pushing my luck today attempting that..


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
}
}
Loading