Skip to content

Commit 0a15a45

Browse files
committed
Optimize access of function symbols
1 parent 8fbf771 commit 0a15a45

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,13 @@ class Definitions {
11461146
else funType(n)
11471147
).symbol.asClass
11481148

1149-
@tu lazy val Function0_apply: Symbol = FunctionClass(0).requiredMethod(nme.apply)
1149+
@tu lazy val Function0_apply: Symbol = Function0.requiredMethod(nme.apply)
1150+
1151+
@tu lazy val Function0: Symbol = FunctionClass(0)
1152+
@tu lazy val Function1: Symbol = FunctionClass(1)
1153+
@tu lazy val Function2: Symbol = FunctionClass(2)
1154+
1155+
@tu lazy val SpecializableFunctions: IArray[Symbol] = IArray(Function0, Function1, Function2)
11501156

11511157
def FunctionType(n: Int, isContextual: Boolean = false, isErased: Boolean = false)(using Context): TypeRef =
11521158
FunctionClass(n, isContextual && !ctx.erasedTypes, isErased).typeRef

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class ExpandSAMs extends MiniPhase {
162162
cpy.Block(tree)(pfDef :: Nil, New(pfSym.typeRef, Nil))
163163

164164
case _ =>
165-
val found = tpe.baseType(defn.FunctionClass(1))
165+
val found = tpe.baseType(defn.Function1)
166166
report.error(TypeMismatch(found, tpe), tree.srcPos)
167167
tree
168168
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
3232

3333
var arity = 0
3434
while (arity < 3) {
35-
val func = defn.FunctionClass(arity)
35+
val func = defn.SpecializableFunctions(arity)
3636
if (sym != func && sym.derivesFrom(func)) {
3737
val baseType = tp.cls.typeRef.baseType(func)
3838
val paramTypes = baseType.argInfos
@@ -149,8 +149,8 @@ class SpecializeFunctions extends MiniPhase with InfoTransformer {
149149
case _ => tree
150150
}
151151

152-
private def derivesFromFn012(sym: Symbol)(using Context): Boolean =
153-
sym.derivesFrom(defn.FunctionClass(0)) ||
154-
sym.derivesFrom(defn.FunctionClass(1)) ||
155-
sym.derivesFrom(defn.FunctionClass(2))
152+
private def derivesFromFn012(cls: ClassSymbol)(using Context): Boolean =
153+
cls.baseClasses.exists { p =>
154+
p == defn.Function0 || p == defn.Function1 || p == defn.Function2
155+
}
156156
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class TransformByNameApply extends MiniPhase { thisPhase: DenotTransfor
4747
ref(defn.cbnArg).appliedToType(argType).appliedTo(arg).withSpan(arg.span)
4848
arg match {
4949
case Apply(Select(qual, nme.apply), Nil)
50-
if qual.tpe.derivesFrom(defn.FunctionClass(0)) && (isPureExpr(qual) || qual.symbol.isAllOf(Inline | Param)) =>
50+
if qual.tpe.derivesFrom(defn.Function0) && (isPureExpr(qual) || qual.symbol.isAllOf(Inline | Param)) =>
5151
wrap(qual)
5252
case _ =>
5353
if (isByNameRef(arg) || arg.symbol == defn.cbnArg) arg

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ object Implicits:
174174
// We keep the old behavior under -source 3.0-migration.
175175
val isFunctionInS2 =
176176
migrateTo3
177-
&& tpw.derivesFrom(defn.FunctionClass(1))
177+
&& tpw.derivesFrom(defn.Function1)
178178
&& ref.symbol != defn.Predef_conforms
179179
val isImplicitConversion = tpw.derivesFrom(defn.ConversionClass)
180180
// An implementation of <:< counts as a view

0 commit comments

Comments
 (0)