From b6ef47d416ae28848a0a1641d3111d54eeaf1c4f Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 4 Dec 2024 13:35:57 +0000 Subject: [PATCH 1/5] Introduce bindTypeSymbols utility in tpd.TreeOps [Cherry-picked de44d078ba68c1e0d503343a97b53195f5858ca1] --- compiler/src/dotty/tools/dotc/ast/tpd.scala | 4 ++++ .../dotty/tools/dotc/inlines/InlineReducer.scala | 16 +++------------- .../dotty/tools/dotc/typer/TypeAssigner.scala | 8 +------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/ast/tpd.scala b/compiler/src/dotty/tools/dotc/ast/tpd.scala index 8999773ec248..c119dfd8d982 100644 --- a/compiler/src/dotty/tools/dotc/ast/tpd.scala +++ b/compiler/src/dotty/tools/dotc/ast/tpd.scala @@ -1127,6 +1127,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 */ diff --git a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala index 4680ea77df9c..1fa4fc624979 100644 --- a/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala +++ b/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala @@ -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 } diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala index c764b035cba1..ba9975923df0 100644 --- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -421,13 +421,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 From b6ed6733805849b83a2a1e928022de611d5da016 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 4 Dec 2024 13:36:55 +0000 Subject: [PATCH 2/5] Add type avoidance to inferred MT bound lubbing --- tests/pos/i21256.scala | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/pos/i21256.scala diff --git a/tests/pos/i21256.scala b/tests/pos/i21256.scala new file mode 100644 index 000000000000..e51484c73ef7 --- /dev/null +++ b/tests/pos/i21256.scala @@ -0,0 +1,5 @@ +object Test { + type MTWithBind[X] = X match { + case List[t] => t + } +} From 67d64ae00fb38a60a30887edad08f8db6bb0a876 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Wed, 12 Mar 2025 18:02:22 +0100 Subject: [PATCH 3/5] Add type avoidance to inferred MT bound lubbing [Cherry-picked 2f6e60d5819873763609943962360c2f85f338bb][modified] From 75ef1d36c45c0ae9ffa4b9b81209cb66b92a21f4 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 4 Dec 2024 13:38:05 +0000 Subject: [PATCH 4/5] Consider S a pseudo match alias Fixes tests/pos/i18211.scala, where type avoidance would cause `S[? <: Int]` which would otherwise be consider an unreducible wild and lead to Any which isn't <: Int. [Cherry-picked fd7f451eeccfa8adb260d9b2cf86f47acfc1bc43] --- compiler/src/dotty/tools/dotc/core/Types.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 1552b2047a72..6cfe58bae2ea 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -4557,6 +4557,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 From be7c860f75e948da48f15f9e5f419b882a86177c Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 4 Dec 2024 16:07:15 +0000 Subject: [PATCH 5/5] Exclude testPickling difference [Cherry-picked 045b92f8c40cd6c173a02b8e7e0f989810178a5e] --- compiler/test/dotc/pos-test-pickling.blacklist | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/test/dotc/pos-test-pickling.blacklist b/compiler/test/dotc/pos-test-pickling.blacklist index 3219fb7c1473..1cb73da4c2eb 100644 --- a/compiler/test/dotc/pos-test-pickling.blacklist +++ b/compiler/test/dotc/pos-test-pickling.blacklist @@ -19,6 +19,7 @@ i12299a.scala i13871.scala i15181.scala i15922.scala +i15926.scala t5031_2.scala i16997.scala i7414.scala