Skip to content

Commit b4f3c67

Browse files
committed
Make sure specialized functions get the correct name
1 parent f1bcacd commit b4f3c67

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

compiler/src/dotty/tools/dotc/transform/SpecializeFunctions.scala

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,25 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
2929
* the class parents with specialized versions.
3030
*/
3131
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context) = tp match {
32-
case tp: ClassInfo
33-
if !sym.is(Flags.Package) &&
34-
!blacklistedSymbols.contains(sym) &&
35-
(tp.decls ne EmptyScope)
36-
=> {
37-
var newApplys: List[Symbol] = Nil
32+
case tp: ClassInfo if !sym.is(Flags.Package) && (tp.decls ne EmptyScope) => {
33+
var newApplys = Map.empty[Name, Symbol]
3834

3935
val newParents = tp.parents.mapConserve { parent =>
4036
List(0, 1, 2, 3).flatMap { arity =>
4137
val func = defn.FunctionClass(arity)
42-
if (!parent.isRef(func)) Nil
38+
if (!parent.derivesFrom(func)) Nil
4339
else {
4440
val typeParams = tp.typeRef.baseArgInfos(func)
4541
val interface = specInterface(typeParams)
4642

4743
if (interface.exists) {
48-
val specializedApply = {
49-
val specializedMethodName = specializedName(nme.apply, typeParams)
50-
ctx.newSymbol(
51-
sym,
52-
specializedMethodName,
53-
Flags.Override | Flags.Method,
54-
interface.info.decls.lookup(specializedMethodName).info
55-
)
44+
if (tp.decls.lookup(nme.apply).exists) {
45+
val specializedMethodName = nme.apply.specializedFunction(typeParams.last, typeParams.init)
46+
newApplys = newApplys + (specializedMethodName -> interface)
5647
}
57-
newApplys = specializedApply :: newApplys
58-
List(interface.typeRef)
48+
49+
if (parent.isRef(func)) List(interface.typeRef)
50+
else Nil
5951
}
6052
else Nil
6153
}
@@ -66,9 +58,18 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
6658

6759
def newDecls =
6860
if (newApplys.isEmpty) tp.decls
69-
else newApplys.foldLeft(tp.decls.cloneScope) {
70-
(scope, sym) => scope.enter(sym); scope
71-
}
61+
else
62+
newApplys.toList.map { case (name, interface) =>
63+
ctx.newSymbol(
64+
sym,
65+
name,
66+
Flags.Override | Flags.Method,
67+
interface.info.decls.lookup(name).info
68+
)
69+
}
70+
.foldLeft(tp.decls.cloneScope) {
71+
(scope, sym) => scope.enter(sym); scope
72+
}
7273

7374
tp.derivedClassInfo(
7475
classParents = newParents,
@@ -88,13 +89,22 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
8889
val applyBuf = new mutable.ListBuffer[Tree]
8990
val newBody = tree.body.mapConserve {
9091
case dt: DefDef if dt.name == nme.apply && dt.vparamss.length == 1 => {
91-
val specializedApply = tree.symbol.enclosingClass.info.decls.lookup {
92-
specializedName(
93-
nme.apply,
94-
dt.vparamss.head.map(_.symbol.info) :+ dt.tpe.widen.finalResultType
95-
)
92+
val specName = nme.apply.specializedFunction(
93+
dt.tpe.widen.finalResultType,
94+
dt.vparamss.head.map(_.symbol.info)
95+
)
96+
97+
val specializedApply = tree.symbol.enclosingClass.info.decls.lookup(specName)//member(specName).symbol
98+
//val specializedApply = tree.symbol.enclosingClass.info.member(specName).symbol
99+
100+
if (false) {
101+
println(tree.symbol.enclosingClass.show)
102+
println("'" + specName.show + "'")
103+
println(specializedApply)
104+
println(specializedApply.exists)
96105
}
97106

107+
98108
if (specializedApply.exists) {
99109
val apply = specializedApply.asTerm
100110
val specializedDecl =
@@ -103,7 +113,6 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
103113
.changeOwner(dt.symbol, apply)
104114
.subst(dt.vparamss.flatten.map(_.symbol), vrefss.flatten.map(_.symbol))
105115
})
106-
107116
applyBuf += specializedDecl
108117

109118
// create a forwarding to the specialized apply
@@ -147,7 +156,7 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
147156
val params = (fun.tpe.widen.firstParamTypes :+ tree.tpe).map(_.widenSingleton.dealias)
148157
val specializedApply = specializedName(nme.apply, params)
149158

150-
if (!params.exists(_.isInstanceOf[ExprType]) && defn.FunctionClass(args.length).info.member(specializedApply).exists) {
159+
if (!params.exists(_.isInstanceOf[ExprType]) && fun.symbol.owner.info.decls.lookup(specializedApply).exists) {
151160
val newSel = fun match {
152161
case Select(qual, _) =>
153162
qual.select(specializedApply)
@@ -174,7 +183,7 @@ class SpecializeFunctions extends MiniPhaseTransform with InfoTransformer {
174183
@inline private def specInterface(typeParams: List[Type])(implicit ctx: Context) = {
175184
val specName =
176185
("JFunction" + (typeParams.length - 1)).toTermName
177-
.specializedFor(typeParams, typeParams.map(_.typeSymbol.name), Nil, Nil)
186+
.specializedFunction(typeParams.last, typeParams.init)
178187

179188
ctx.getClassIfDefined("scala.compat.java8.".toTermName ++ specName)
180189
}

0 commit comments

Comments
 (0)