Skip to content

Commit b6ef47d

Browse files
dwijnandtgodzik
authored andcommitted
Introduce bindTypeSymbols utility in tpd.TreeOps
[Cherry-picked de44d07]
1 parent 8f067e0 commit b6ef47d

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11271127
/** Replace Ident nodes references to the underlying tree that defined them */
11281128
def underlying(using Context): Tree = MapToUnderlying().transform(tree)
11291129

1130+
/** Collect all the TypeSymbol's of the type Bind nodes in the tree. */
1131+
def bindTypeSymbols(using Context): List[TypeSymbol] =
1132+
tree.collectSubTrees { case b: Bind if b.isType => b.symbol.asType }
1133+
11301134
// --- Higher order traversal methods -------------------------------
11311135

11321136
/** Apply `f` to each subtree of this tree */

compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,21 @@ class InlineReducer(inliner: Inliner)(using Context):
216216
type TypeBindsMap = SimpleIdentityMap[TypeSymbol, java.lang.Boolean]
217217

218218
def getTypeBindsMap(pat: Tree, tpt: Tree): TypeBindsMap = {
219-
val getBinds = new TreeAccumulator[Set[TypeSymbol]] {
220-
def apply(syms: Set[TypeSymbol], t: Tree)(using Context): Set[TypeSymbol] = {
221-
val syms1 = t match {
222-
case t: Bind if t.symbol.isType =>
223-
syms + t.symbol.asType
224-
case _ => syms
225-
}
226-
foldOver(syms1, t)
227-
}
228-
}
229-
230219
// Extractors can contain Bind nodes in type parameter lists,
231220
// for that case tree looks like this:
232221
// UnApply[t @ t](pats)(implicits): T[t]
233222
// Test case is pos/inline-caseclass.scala.
223+
//
234224
// Alternatively, for explicitly specified type binds in type annotations like in
235225
// case A(B): A[t]
236226
// the tree will look like this:
237227
// Unapply[t](pats)(implicits) : T[t @ t]
238228
// and the binds will be found in the type tree instead
239229
// Test case is pos-macros/i15971
240-
val tptBinds = getBinds(Set.empty[TypeSymbol], tpt)
230+
val tptBinds = tpt.bindTypeSymbols.toSet
241231
val binds: Set[TypeSymbol] = pat match {
242232
case UnApply(TypeApply(_, tpts), _, _) =>
243-
getBinds(Set.empty[TypeSymbol], tpts) ++ tptBinds
233+
tpts.flatMap(_.bindTypeSymbols).toSet ++ tptBinds
244234
case _ => tptBinds
245235
}
246236

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,7 @@ trait TypeAssigner {
421421
def assignType(tree: untpd.CaseDef, pat: Tree, body: Tree)(using Context): CaseDef = {
422422
val ownType =
423423
if (body.isType) {
424-
val getParams = new TreeAccumulator[mutable.ListBuffer[TypeSymbol]] {
425-
def apply(ps: mutable.ListBuffer[TypeSymbol], t: Tree)(using Context) = t match {
426-
case t: Bind if t.symbol.isType => foldOver(ps += t.symbol.asType, t)
427-
case _ => foldOver(ps, t)
428-
}
429-
}
430-
val params1 = getParams(new mutable.ListBuffer[TypeSymbol](), pat).toList
424+
val params1 = pat.bindTypeSymbols
431425
val params2 = pat.tpe match
432426
case AppliedType(tycon, args) =>
433427
val tparams = tycon.typeParamSymbols

0 commit comments

Comments
 (0)