Skip to content

Commit 6df672c

Browse files
authored
Merge pull request #1962 from dotty-staging/centralize-function-logic
Factor out logic for scala functions.
2 parents 39af2f5 + 93a2d06 commit 6df672c

File tree

7 files changed

+111
-52
lines changed

7 files changed

+111
-52
lines changed

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

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,9 @@ class Definitions {
637637
FunctionType(args.length, isImplicit).appliedTo(args ::: resultType :: Nil)
638638
def unapply(ft: Type)(implicit ctx: Context) = {
639639
val tsym = ft.typeSymbol
640-
val isImplicitFun = isImplicitFunctionClass(tsym)
641-
if (isImplicitFun || isFunctionClass(tsym)) {
642-
val targs = ft.argInfos
643-
val numArgs = targs.length - 1
644-
if (numArgs >= 0 && FunctionType(numArgs, isImplicitFun).symbol == tsym)
645-
Some(targs.init, targs.last, isImplicitFun)
646-
else None
640+
if (isFunctionClass(tsym)) {
641+
val targs = ft.dealias.argInfos
642+
Some(targs.init, targs.last, tsym.name.isImplicitFunction)
647643
}
648644
else None
649645
}
@@ -696,20 +692,17 @@ class Definitions {
696692
lazy val TupleType = mkArityArray("scala.Tuple", MaxTupleArity, 2)
697693
lazy val ProductNType = mkArityArray("scala.Product", MaxTupleArity, 0)
698694

699-
def FunctionClass(n: Int)(implicit ctx: Context) =
700-
if (n < MaxImplementedFunctionArity) FunctionClassPerRun()(ctx)(n)
695+
def FunctionClass(n: Int, isImplicit: Boolean = false)(implicit ctx: Context) =
696+
if (isImplicit) ctx.requiredClass("scala.ImplicitFunction" + n.toString)
697+
else if (n <= MaxImplementedFunctionArity) FunctionClassPerRun()(ctx)(n)
701698
else ctx.requiredClass("scala.Function" + n.toString)
702699

703700
lazy val Function0_applyR = ImplementedFunctionType(0).symbol.requiredMethodRef(nme.apply)
704701
def Function0_apply(implicit ctx: Context) = Function0_applyR.symbol
705702

706-
def ImplicitFunctionClass(n: Int)(implicit ctx: Context) =
707-
ctx.requiredClass("scala.ImplicitFunction" + n.toString)
708-
709703
def FunctionType(n: Int, isImplicit: Boolean = false)(implicit ctx: Context): TypeRef =
710-
if (isImplicit && !ctx.erasedTypes) ImplicitFunctionClass(n).typeRef
711-
else if (n < MaxImplementedFunctionArity) ImplementedFunctionType(n)
712-
else FunctionClass(n).typeRef
704+
if (n <= MaxImplementedFunctionArity && (!isImplicit || ctx.erasedTypes)) ImplementedFunctionType(n)
705+
else FunctionClass(n, isImplicit).typeRef
713706

714707
private lazy val TupleTypes: Set[TypeRef] = TupleType.toSet
715708
private lazy val ProductTypes: Set[TypeRef] = ProductNType.toSet
@@ -733,14 +726,61 @@ class Definitions {
733726
def isBottomType(tp: Type) =
734727
tp.derivesFrom(NothingClass) || tp.derivesFrom(NullClass)
735728

736-
def isFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.Function)
737-
def isImplicitFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.ImplicitFunction)
738-
/** Is a class that will be erased to FunctionXXL */
739-
def isXXLFunctionClass(cls: Symbol) = cls.name.functionArity > MaxImplementedFunctionArity
729+
/** Is a function class.
730+
* - FunctionN for N >= 0
731+
* - ImplicitFunctionN for N >= 0
732+
*/
733+
def isFunctionClass(cls: Symbol) = scalaClassName(cls).isFunction
734+
735+
/** Is an implicit function class.
736+
* - ImplicitFunctionN for N >= 0
737+
*/
738+
def isImplicitFunctionClass(cls: Symbol) = scalaClassName(cls).isImplicitFunction
739+
740+
/** Is a class that will be erased to FunctionXXL
741+
* - FunctionN for N >= 22
742+
* - ImplicitFunctionN for N >= 22
743+
*/
744+
def isXXLFunctionClass(cls: Symbol) = scalaClassName(cls).functionArity > MaxImplementedFunctionArity
745+
746+
/** Is a synthetic function class
747+
* - FunctionN for N > 22
748+
* - ImplicitFunctionN for N >= 0
749+
*/
750+
def isSyntheticFunctionClass(cls: Symbol) = scalaClassName(cls).isSyntheticFunction
751+
740752
def isAbstractFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.AbstractFunction)
741753
def isTupleClass(cls: Symbol) = isVarArityClass(cls, tpnme.Tuple)
742754
def isProductClass(cls: Symbol) = isVarArityClass(cls, tpnme.Product)
743755

756+
/** Returns the erased class of the function class `cls`
757+
* - FunctionN for N > 22 becomes FunctionXXL
758+
* - FunctionN for 22 > N >= 0 remains as FunctionN
759+
* - ImplicitFunctionN for N > 22 becomes FunctionXXL
760+
* - ImplicitFunctionN for 22 > N >= 0 becomes FunctionN
761+
* - anything else becomes a NoSymbol
762+
*/
763+
def erasedFunctionClass(cls: Symbol): Symbol = {
764+
val arity = scalaClassName(cls).functionArity
765+
if (arity > 22) defn.FunctionXXLClass
766+
else if (arity >= 0) defn.FunctionClass(arity)
767+
else NoSymbol
768+
}
769+
770+
/** Returns the erased type of the function class `cls`
771+
* - FunctionN for N > 22 becomes FunctionXXL
772+
* - FunctionN for 22 > N >= 0 remains as FunctionN
773+
* - ImplicitFunctionN for N > 22 becomes FunctionXXL
774+
* - ImplicitFunctionN for 22 > N >= 0 becomes FunctionN
775+
* - anything else becomes a NoType
776+
*/
777+
def erasedFunctionType(cls: Symbol): Type = {
778+
val arity = scalaClassName(cls).functionArity
779+
if (arity > 22) defn.FunctionXXLType
780+
else if (arity >= 0) defn.FunctionType(arity)
781+
else NoType
782+
}
783+
744784
val predefClassNames: Set[Name] =
745785
Set("Predef$", "DeprecatedPredef", "LowPriorityImplicits").map(_.toTypeName)
746786

@@ -811,16 +851,13 @@ class Definitions {
811851
def isFunctionType(tp: Type)(implicit ctx: Context) = {
812852
val arity = functionArity(tp)
813853
val sym = tp.dealias.typeSymbol
814-
arity >= 0 && (
815-
isFunctionClass(sym) && tp.isRef(FunctionType(arity, isImplicit = false).typeSymbol) ||
816-
isImplicitFunctionClass(sym) && tp.isRef(FunctionType(arity, isImplicit = true).typeSymbol)
817-
)
854+
arity >= 0 && isFunctionClass(sym) && tp.isRef(FunctionType(arity, sym.name.isImplicitFunction).typeSymbol)
818855
}
819856

820857
def functionArity(tp: Type)(implicit ctx: Context) = tp.dealias.argInfos.length - 1
821858

822859
def isImplicitFunctionType(tp: Type)(implicit ctx: Context) =
823-
isFunctionType(tp) && tp.dealias.typeSymbol.name.startsWith(tpnme.ImplicitFunction)
860+
isFunctionType(tp) && tp.dealias.typeSymbol.name.isImplicitFunction
824861

825862
// ----- primitive value class machinery ------------------------------------------
826863

@@ -894,9 +931,6 @@ class Definitions {
894931

895932
// ----- Initialization ---------------------------------------------------
896933

897-
private def maxImplemented(name: Name) =
898-
if (name `startsWith` tpnme.Function) MaxImplementedFunctionArity else 0
899-
900934
/** Give the scala package a scope where a FunctionN trait is automatically
901935
* added when someone looks for it.
902936
*/
@@ -906,7 +940,7 @@ class Definitions {
906940
val newDecls = new MutableScope(oldDecls) {
907941
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
908942
val res = super.lookupEntry(name)
909-
if (res == null && name.isTypeName && name.functionArity > maxImplemented(name))
943+
if (res == null && name.isTypeName && name.isSyntheticFunction)
910944
newScopeEntry(newFunctionNTrait(name.asTypeName))
911945
else res
912946
}

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Names._, StdNames._, Contexts._, Symbols._, Flags._
88
import Decorators.StringDecorator
99
import util.{Chars, NameTransformer}
1010
import Chars.isOperatorPart
11+
import Definitions._
1112

1213
object NameOps {
1314

@@ -231,13 +232,43 @@ object NameOps {
231232
}
232233
}
233234

234-
def functionArity: Int = {
235-
def test(prefix: Name): Int =
236-
if (name.startsWith(prefix))
237-
try name.drop(prefix.length).toString.toInt
238-
catch { case ex: NumberFormatException => -1 }
239-
else -1
240-
test(tpnme.Function) max test(tpnme.ImplicitFunction)
235+
/** Is a synthetic function name
236+
* - N for FunctionN
237+
* - N for ImplicitFunctionN
238+
* - (-1) otherwise
239+
*/
240+
def functionArity: Int =
241+
functionArityFor(tpnme.Function) max functionArityFor(tpnme.ImplicitFunction)
242+
243+
/** Is a function name
244+
* - FunctionN for N >= 0
245+
* - ImplicitFunctionN for N >= 0
246+
* - false otherwise
247+
*/
248+
def isFunction: Boolean = functionArity >= 0
249+
250+
/** Is a implicit function name
251+
* - ImplicitFunctionN for N >= 0
252+
* - false otherwise
253+
*/
254+
def isImplicitFunction: Boolean = functionArityFor(tpnme.ImplicitFunction) >= 0
255+
256+
/** Is a synthetic function name
257+
* - FunctionN for N > 22
258+
* - ImplicitFunctionN for N >= 0
259+
* - false otherwise
260+
*/
261+
def isSyntheticFunction: Boolean = {
262+
functionArityFor(tpnme.Function) > MaxImplementedFunctionArity ||
263+
functionArityFor(tpnme.ImplicitFunction) >= 0
264+
}
265+
266+
/** Parsed function arity for function with some specific prefix */
267+
private def functionArityFor(prefix: Name): Int = {
268+
if (name.startsWith(prefix))
269+
try name.toString.substring(prefix.length).toInt
270+
catch { case _: NumberFormatException => -1 }
271+
else -1
241272
}
242273

243274
/** The name of the generic runtime operation corresponding to an array operation */

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object TypeErasure {
4444
val sym = tp.symbol
4545
sym.isClass &&
4646
sym != defn.AnyClass && sym != defn.ArrayClass &&
47-
!defn.isXXLFunctionClass(sym) && !defn.isImplicitFunctionClass(sym)
47+
!defn.isSyntheticFunctionClass(sym)
4848
case _: TermRef =>
4949
true
5050
case JavaArrayType(elem) =>
@@ -358,8 +358,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
358358
if (!sym.isClass) this(tp.info)
359359
else if (semiEraseVCs && isDerivedValueClass(sym)) eraseDerivedValueClassRef(tp)
360360
else if (sym == defn.ArrayClass) apply(tp.appliedTo(TypeBounds.empty)) // i966 shows that we can hit a raw Array type.
361-
else if (defn.isXXLFunctionClass(sym)) defn.FunctionXXLType
362-
else if (defn.isImplicitFunctionClass(sym)) apply(defn.FunctionType(sym.name.functionArity))
361+
else if (defn.isSyntheticFunctionClass(sym)) defn.erasedFunctionType(sym)
363362
else eraseNormalClassRef(tp)
364363
case tp: RefinedType =>
365364
val parent = tp.parent
@@ -370,7 +369,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
370369
case SuperType(thistpe, supertpe) =>
371370
SuperType(this(thistpe), this(supertpe))
372371
case ExprType(rt) =>
373-
defn.FunctionClass(0).typeRef
372+
defn.FunctionType(0)
374373
case AndType(tp1, tp2) =>
375374
erasedGlb(this(tp1), this(tp2), isJava)
376375
case OrType(tp1, tp2) =>
@@ -496,8 +495,8 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
496495
val erasedVCRef = eraseDerivedValueClassRef(tp)
497496
if (erasedVCRef.exists) return sigName(erasedVCRef)
498497
}
499-
if (defn.isImplicitFunctionClass(sym))
500-
sigName(defn.FunctionType(sym.name.functionArity))
498+
if (defn.isSyntheticFunctionClass(sym))
499+
sigName(defn.erasedFunctionType(sym))
501500
else
502501
normalizeClass(sym.asClass).fullName.asTypeName
503502
case defn.ArrayOf(elem) =>

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
126126
case AppliedType(tycon, args) =>
127127
val cls = tycon.typeSymbol
128128
if (tycon.isRepeatedParam) return toTextLocal(args.head) ~ "*"
129-
if (defn.isFunctionClass(cls)) return toTextFunction(args, isImplicit = false)
130-
if (defn.isImplicitFunctionClass(cls)) return toTextFunction(args, isImplicit = true)
129+
if (defn.isFunctionClass(cls)) return toTextFunction(args, cls.name.isImplicitFunction)
131130
if (defn.isTupleClass(cls)) return toTextTuple(args)
132131
return (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close
133132
case tp: TypeRef =>

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ object Erasure extends TypeTestsCasts{
349349
if ((owner eq defn.AnyClass) || (owner eq defn.AnyValClass)) {
350350
assert(sym.isConstructor, s"${sym.showLocated}")
351351
defn.ObjectClass
352-
} else if (defn.isXXLFunctionClass(owner))
353-
defn.FunctionXXLClass
354-
else if (defn.isImplicitFunctionClass(owner))
355-
recur(defn.FunctionClass(owner.name.functionArity))
352+
} else if (defn.isSyntheticFunctionClass(owner))
353+
defn.erasedFunctionClass(owner)
356354
else
357355
owner
358356
recur(sym.owner)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class TreeChecker extends Phase with SymTransformer {
8181
val sym = symd.symbol
8282

8383
if (sym.isClass && !sym.isAbsent) {
84-
val validSuperclass = sym.isPrimitiveValueClass || defn.syntheticCoreClasses.contains(sym) ||
84+
val validSuperclass = sym.isPrimitiveValueClass || defn.syntheticCoreClasses.contains(sym) ||
8585
(sym eq defn.ObjectClass) || (sym is NoSuperClass) || (sym.asClass.superClass.exists)
8686
if (!validSuperclass)
8787
printError(s"$sym has no superclass set")

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
669669
def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") {
670670
val untpd.Function(args, body) = tree
671671
if (ctx.mode is Mode.Type) {
672-
val funCls =
673-
if (tree.isInstanceOf[untpd.ImplicitFunction]) defn.ImplicitFunctionClass(args.length)
674-
else defn.FunctionClass(args.length)
672+
val funCls = defn.FunctionClass(args.length, tree.isInstanceOf[untpd.ImplicitFunction])
675673
typed(cpy.AppliedTypeTree(tree)(
676674
untpd.TypeTree(funCls.typeRef), args :+ body), pt)
677675
}
@@ -1942,7 +1940,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
19421940
!untpd.isImplicitClosure(tree) &&
19431941
!isApplyProto(pt) &&
19441942
!ctx.isAfterTyper) {
1945-
typr.println("insert apply on implicit $tree")
1943+
typr.println(i"insert apply on implicit $tree")
19461944
typed(untpd.Select(untpd.TypedSplice(tree), nme.apply), pt)
19471945
}
19481946
else if (ctx.mode is Mode.Pattern) {

0 commit comments

Comments
 (0)