Skip to content

Commit d78c157

Browse files
committed
Add defn.FunctionNOf.{apply,unapply}
This provides variant to `defn.FunctionOf` that only deals with proper `FunctionN` and `ContextFunctionN` types. This avoids some overhead. A difference between the two `unapply`s is that this one does not dealias the type, it needs to be dealiased at call site. Part of #18305
1 parent 6e45dd7 commit d78c157

11 files changed

+42
-25
lines changed

compiler/src/dotty/tools/dotc/cc/Synthetics.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ object Synthetics:
174174
val (et: ExprType) = symd.info: @unchecked
175175
val (enclThis: ThisType) = symd.owner.thisType: @unchecked
176176
def mapFinalResult(tp: Type, f: Type => Type): Type =
177-
val defn.FunctionOf(args, res, isContextual) = tp: @unchecked
177+
val defn.FunctionNOf(args, res, isContextual) = tp: @unchecked
178178
if defn.isFunctionNType(res) then
179-
defn.FunctionOf(args, mapFinalResult(res, f), isContextual)
179+
defn.FunctionNOf(args, mapFinalResult(res, f), isContextual)
180180
else
181181
f(tp)
182182
val resType1 =

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,9 @@ class Definitions {
11111111
object FunctionOf {
11121112
def apply(args: List[Type], resultType: Type, isContextual: Boolean = false)(using Context): Type =
11131113
val mt = MethodType.companion(isContextual, false)(args, resultType)
1114-
if mt.hasErasedParams then
1115-
RefinedType(PolyFunctionClass.typeRef, nme.apply, mt)
1116-
else
1117-
FunctionType(args.length, isContextual).appliedTo(args ::: resultType :: Nil)
1114+
if mt.hasErasedParams then RefinedType(PolyFunctionClass.typeRef, nme.apply, mt)
1115+
else FunctionNOf(args, resultType, isContextual)
1116+
11181117
def unapply(ft: Type)(using Context): Option[(List[Type], Type, Boolean)] = {
11191118
ft.dealias match
11201119
case PolyFunctionOf(mt: MethodType) =>
@@ -1129,6 +1128,24 @@ class Definitions {
11291128
}
11301129
}
11311130

1131+
object FunctionNOf {
1132+
/** Create a `FunctionN` or `ContextFunctionN` type applied to the arguments and result type */
1133+
def apply(args: List[Type], resultType: Type, isContextual: Boolean = false)(using Context): Type =
1134+
FunctionType(args.length, isContextual).appliedTo(args ::: resultType :: Nil)
1135+
1136+
/** Matches a (possibly aliased) `FunctionN[...]` or `ContextFunctionN[...]`.
1137+
* Extracts the list of function argument types, the result type and whether function is contextual.
1138+
*/
1139+
def unapply(tpe: Type)(using Context): Option[(List[Type], Type, Boolean)] = {
1140+
val tsym = tpe.typeSymbol
1141+
if isFunctionSymbol(tsym) && tpe.isRef(tsym) then
1142+
val targs = tpe.argInfos
1143+
if (targs.isEmpty) None
1144+
else Some(targs.init, targs.last, tsym.name.isContextFunction)
1145+
else None
1146+
}
1147+
}
1148+
11321149
object RefinedFunctionOf {
11331150
/** Matches a refined `PolyFunction`/`FunctionN[...]`/`ContextFunctionN[...]`.
11341151
* Extracts the method type type and apply info.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
933933
case tp: TermRef =>
934934
sigName(underlyingOfTermRef(tp))
935935
case ExprType(rt) =>
936-
sigName(defn.FunctionOf(Nil, rt))
936+
sigName(defn.FunctionNOf(Nil, rt))
937937
case tp: TypeVar if !tp.isInstantiated =>
938938
tpnme.Uninstantiated
939939
case tp @ defn.PolyFunctionOf(_) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ object Types {
18901890
case res: MethodType => res.toFunctionType(isJava)
18911891
case res => res
18921892
}
1893-
defn.FunctionOf(
1893+
defn.FunctionNOf(
18941894
mt.paramInfos.mapConserve(_.translateFromRepeated(toArray = isJava)),
18951895
result1, isContextual)
18961896
if mt.hasErasedParams then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ object PickleQuotes {
326326
defn.QuotedExprClass.typeRef.appliedTo(defn.AnyType)),
327327
args =>
328328
val cases = holeContents.zipWithIndex.map { case (splice, idx) =>
329-
val defn.FunctionOf(argTypes, defn.FunctionOf(quotesType :: _, _, _), _) = splice.tpe: @unchecked
329+
val defn.FunctionNOf(argTypes, defn.FunctionNOf(quotesType :: _, _, _), _) = splice.tpe: @unchecked
330330
val rhs = {
331331
val spliceArgs = argTypes.zipWithIndex.map { (argType, i) =>
332332
args(1).select(nme.apply).appliedTo(Literal(Constant(i))).asInstance(argType)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class SpecializeFunctions extends MiniPhase {
8888
// Need to cast to regular function, since specialized apply methods
8989
// are not members of ContextFunction0. The cast will be eliminated in
9090
// erasure.
91-
qual.cast(defn.FunctionOf(Nil, res))
91+
qual.cast(defn.FunctionNOf(Nil, res))
9292
case _ =>
9393
qual
9494
qual1.select(specializedApply)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,9 @@ object TreeChecker {
749749
if isTerm then defn.QuotedExprClass.typeRef.appliedTo(tree1.typeOpt)
750750
else defn.QuotedTypeClass.typeRef.appliedTo(tree1.typeOpt)
751751
val contextualResult =
752-
defn.FunctionOf(List(defn.QuotesClass.typeRef), expectedResultType, isContextual = true)
752+
defn.FunctionNOf(List(defn.QuotesClass.typeRef), expectedResultType, isContextual = true)
753753
val expectedContentType =
754-
defn.FunctionOf(argQuotedTypes, contextualResult)
754+
defn.FunctionNOf(argQuotedTypes, contextualResult)
755755
assert(content.typeOpt =:= expectedContentType, i"unexpected content of hole\nexpected: ${expectedContentType}\nwas: ${content.typeOpt}")
756756

757757
tree1

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ trait Applications extends Compatibility {
17241724
def apply(t: Type) = t match {
17251725
case t @ AppliedType(tycon, args) =>
17261726
def mapArg(arg: Type, tparam: TypeParamInfo) =
1727-
if (variance > 0 && tparam.paramVarianceSign < 0) defn.FunctionOf(arg :: Nil, defn.UnitType)
1727+
if (variance > 0 && tparam.paramVarianceSign < 0) defn.FunctionNOf(arg :: Nil, defn.UnitType)
17281728
else arg
17291729
mapOver(t.derivedAppliedType(tycon, args.zipWithConserve(tycon.typeParams)(mapArg)))
17301730
case _ => mapOver(t)
@@ -1951,7 +1951,7 @@ trait Applications extends Compatibility {
19511951
/** The shape of given tree as a type; cannot handle named arguments. */
19521952
def typeShape(tree: untpd.Tree): Type = tree match {
19531953
case untpd.Function(args, body) =>
1954-
defn.FunctionOf(
1954+
defn.FunctionNOf(
19551955
args.map(Function.const(defn.AnyType)), typeShape(body),
19561956
isContextual = untpd.isContextualClosure(tree))
19571957
case Match(EmptyTree, _) =>
@@ -1991,8 +1991,8 @@ trait Applications extends Compatibility {
19911991
def paramCount(ref: TermRef) =
19921992
val formals = ref.widen.firstParamTypes
19931993
if formals.length > idx then
1994-
formals(idx) match
1995-
case defn.FunctionOf(args, _, _) => args.length
1994+
formals(idx).dealias match
1995+
case defn.FunctionNOf(args, _, _) => args.length
19961996
case _ => -1
19971997
else -1
19981998

@@ -2077,8 +2077,8 @@ trait Applications extends Compatibility {
20772077
else resolveMapped(alts1, _.widen.appliedTo(targs1.tpes), pt1)
20782078

20792079
case pt =>
2080-
val compat0 = pt match
2081-
case defn.FunctionOf(args, resType, _) =>
2080+
val compat0 = pt.dealias match
2081+
case defn.FunctionNOf(args, resType, _) =>
20822082
narrowByTypes(alts, args, resType)
20832083
case _ =>
20842084
Nil
@@ -2266,7 +2266,7 @@ trait Applications extends Compatibility {
22662266
false
22672267
val commonFormal =
22682268
if (isPartial) defn.PartialFunctionOf(commonParamTypes.head, WildcardType)
2269-
else defn.FunctionOf(commonParamTypes, WildcardType, isContextual = untpd.isContextualClosure(arg))
2269+
else defn.FunctionNOf(commonParamTypes, WildcardType, isContextual = untpd.isContextualClosure(arg))
22702270
overload.println(i"pretype arg $arg with expected type $commonFormal")
22712271
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
22722272
withMode(Mode.ImplicitsEnabled) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ object ProtoTypes {
383383
def allArgTypesAreCurrent()(using Context): Boolean =
384384
state.typedArg.size == args.length
385385

386-
private def isUndefined(tp: Type): Boolean = tp match {
386+
private def isUndefined(tp: Type): Boolean = tp.dealias match {
387387
case _: WildcardType => true
388-
case defn.FunctionOf(args, result, _) => args.exists(isUndefined) || isUndefined(result)
388+
case defn.FunctionNOf(args, result, _) => args.exists(isUndefined) || isUndefined(result)
389389
case _ => false
390390
}
391391

@@ -424,7 +424,7 @@ object ProtoTypes {
424424
case ValDef(_, tpt, _) if !tpt.isEmpty => typer.typedType(tpt).typeOpt
425425
case _ => WildcardType
426426
}
427-
targ = arg.withType(defn.FunctionOf(paramTypes, WildcardType))
427+
targ = arg.withType(defn.FunctionNOf(paramTypes, WildcardType))
428428
case Some(_) if !force =>
429429
targ = arg.withType(WildcardType)
430430
case _ =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ trait QuotesAndSplices {
122122
for arg <- typedArgs if arg.symbol.is(Mutable) do // TODO support these patterns. Possibly using scala.quoted.util.Var
123123
report.error("References to `var`s cannot be used in higher-order pattern", arg.srcPos)
124124
val argTypes = typedArgs.map(_.tpe.widenTermRefExpr)
125-
val patType = if tree.args.isEmpty then pt else defn.FunctionOf(argTypes, pt)
125+
val patType = if tree.args.isEmpty then pt else defn.FunctionNOf(argTypes, pt)
126126
val pat = typedPattern(tree.body, defn.QuotedExprClass.typeRef.appliedTo(patType))(using quotePatternSpliceContext)
127127
val baseType = pat.tpe.baseType(defn.QuotedExprClass)
128128
val argType = if baseType.exists then baseType.argTypesHi.head else defn.NothingType
@@ -148,7 +148,7 @@ trait QuotesAndSplices {
148148
if isInBraces then // ${x}(...) match an application
149149
val typedArgs = args.map(arg => typedExpr(arg))
150150
val argTypes = typedArgs.map(_.tpe.widenTermRefExpr)
151-
val splice1 = typedSplicePattern(splice, defn.FunctionOf(argTypes, pt))
151+
val splice1 = typedSplicePattern(splice, defn.FunctionNOf(argTypes, pt))
152152
untpd.cpy.Apply(tree)(splice1.select(nme.apply), typedArgs).withType(pt)
153153
else // $x(...) higher-order quasipattern
154154
if args.isEmpty then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
105105
case AppliedType(_, funArgs @ fun :: tupled :: Nil) =>
106106
def functionTypeEqual(baseFun: Type, actualArgs: List[Type],
107107
actualRet: Type, expected: Type) =
108-
expected =:= defn.FunctionOf(actualArgs, actualRet,
108+
expected =:= defn.FunctionNOf(actualArgs, actualRet,
109109
defn.isContextFunctionType(baseFun))
110110
val arity: Int =
111111
if defn.isFunctionNType(fun) then

0 commit comments

Comments
 (0)