Skip to content

Commit 78faf05

Browse files
committed
Streamline dropUnusedDefs
The previous treatment could go wrong by confusing `bindings` and `termBindings` in a recursive step. test case: run/typeclass-derivation2c.scala
1 parent dcddc4b commit 78faf05

File tree

4 files changed

+404
-32
lines changed

4 files changed

+404
-32
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object ErrorReporting {
8181
if (tree.tpe.widen.exists)
8282
i"${exprStr(tree)} does not take ${kind}parameters"
8383
else {
84-
new FatalError("").printStackTrace()
84+
//new FatalError("").printStackTrace() //DEBUG
8585
i"undefined: $tree # ${tree.uniqueId}: ${tree.tpe.toString} at ${ctx.phase}"
8686
}
8787

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,29 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
10561056
*/
10571057
def dropUnusedDefs(bindings: List[MemberDef], tree: Tree)(implicit ctx: Context): (List[MemberDef], Tree) = {
10581058
// inlining.println(i"drop unused $bindings%, % in $tree")
1059-
1060-
def inlineTermBindings(termBindings: List[MemberDef], tree: Tree)(implicit ctx: Context): (List[MemberDef], Tree) = {
1059+
val (termBindings, typeBindings) = bindings.partition(_.symbol.isTerm)
1060+
if (typeBindings.nonEmpty) {
1061+
val typeBindingsSet = typeBindings.foldLeft[SimpleIdentitySet[Symbol]](SimpleIdentitySet.empty)(_ + _.symbol)
1062+
val inlineTypeBindings = new TreeTypeMap(
1063+
typeMap = new TypeMap() {
1064+
override def apply(tp: Type): Type = tp match {
1065+
case tr: TypeRef if tr.prefix.eq(NoPrefix) && typeBindingsSet.contains(tr.symbol) =>
1066+
val TypeAlias(res) = tr.info
1067+
res
1068+
case tp => mapOver(tp)
1069+
}
1070+
},
1071+
treeMap = {
1072+
case ident: Ident if ident.isType && typeBindingsSet.contains(ident.symbol) =>
1073+
val TypeAlias(r) = ident.symbol.info
1074+
TypeTree(r).withSpan(ident.span)
1075+
case tree => tree
1076+
}
1077+
)
1078+
val Block(termBindings1, tree1) = inlineTypeBindings(Block(termBindings, tree))
1079+
dropUnusedDefs(termBindings1.asInstanceOf[List[ValOrDefDef]], tree1)
1080+
}
1081+
else {
10611082
val refCount = newMutableSymbolMap[Int]
10621083
val bindingOfSym = newMutableSymbolMap[MemberDef]
10631084

@@ -1066,7 +1087,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
10661087
case vdef @ ValDef(_, _, _) => isPureExpr(vdef.rhs)
10671088
case _ => false
10681089
}
1069-
for (binding <- termBindings if isInlineable(binding)) {
1090+
for (binding <- bindings if isInlineable(binding)) {
10701091
refCount(binding.symbol) = 0
10711092
bindingOfSym(binding.symbol) = binding
10721093
}
@@ -1124,40 +1145,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
11241145
}
11251146
}
11261147

1127-
val retained = termBindings.filterConserve(binding => retain(binding.symbol))
1128-
if (retained `eq` termBindings) {
1129-
(termBindings, tree)
1148+
val retained = bindings.filterConserve(binding => retain(binding.symbol))
1149+
if (retained `eq` bindings) {
1150+
(bindings, tree)
11301151
}
11311152
else {
11321153
val expanded = inlineBindings.transform(tree)
11331154
dropUnusedDefs(retained, expanded)
11341155
}
11351156
}
1136-
1137-
val (termBindings, typeBindings) = bindings.partition(_.symbol.isTerm)
1138-
if (typeBindings.isEmpty) inlineTermBindings(termBindings, tree)
1139-
else {
1140-
val typeBindingsSet = typeBindings.foldLeft[SimpleIdentitySet[Symbol]](SimpleIdentitySet.empty)(_ + _.symbol)
1141-
val inlineTypeBindings = new TreeTypeMap(
1142-
typeMap = new TypeMap() {
1143-
override def apply(tp: Type): Type = tp match {
1144-
case tr: TypeRef if tr.prefix.eq(NoPrefix) && typeBindingsSet.contains(tr.symbol) =>
1145-
val TypeAlias(res) = tr.info
1146-
res
1147-
case tp => mapOver(tp)
1148-
}
1149-
},
1150-
treeMap = {
1151-
case ident: Ident if ident.isType && typeBindingsSet.contains(ident.symbol) =>
1152-
val TypeAlias(r) = ident.symbol.info
1153-
TypeTree(r).withSpan(ident.span)
1154-
case tree => tree
1155-
}
1156-
)
1157-
1158-
val Block(termBindings1, tree1) = inlineTypeBindings(Block(termBindings, tree))
1159-
inlineTermBindings(termBindings1.asInstanceOf[List[ValOrDefDef]], tree1)
1160-
}
11611157
}
11621158

11631159
private def expandMacro(body: Tree, span: Span)(implicit ctx: Context) = {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ListBuffer(0, 11, 0, 22, 0, 33, 1)
2+
Cons(11,Cons(22,Cons(33,Nil)))
3+
ListBuffer(0, 0, 11, 0, 22, 0, 33, 1, 0, 0, 11, 0, 22, 1, 1)
4+
Cons(Cons(11,Cons(22,Cons(33,Nil))),Cons(Cons(11,Cons(22,Nil)),Nil))
5+
ListBuffer(1, 2)
6+
Pair(1,2)

0 commit comments

Comments
 (0)