Skip to content

Commit fdbc86b

Browse files
committed
Decouple erased parameters from ContextFunctionType.unapply
1 parent c1670ce commit fdbc86b

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11491149

11501150
def etaExpandCFT(using Context): Tree =
11511151
def expand(target: Tree, tp: Type)(using Context): Tree = tp match
1152-
case defn.ContextFunctionType(argTypes, resType, _) =>
1152+
case defn.ContextFunctionType(argTypes, resType) =>
11531153
val anonFun = newAnonFun(
11541154
ctx.owner,
11551155
MethodType.companion(isContextual = true)(argTypes, resType),

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,15 @@ class Definitions {
17461746
isFunctionNType(tp)
17471747
|| tp.derivesFrom(defn.PolyFunctionClass) // TODO check for refinement?
17481748

1749+
def erasedFunctionParams(tp: Type)(using Context): List[Boolean] =
1750+
tp match
1751+
case PolyFunctionOf(mt: MethodType) =>
1752+
mt.erasedParams
1753+
case tp =>
1754+
val arity = functionArity(tp)
1755+
if arity < 0 then Nil
1756+
else List.fill(arity) { false }
1757+
17491758
private def withSpecMethods(cls: ClassSymbol, bases: List[Name], paramTypes: Set[TypeRef]) =
17501759
if !ctx.settings.Yscala2Stdlib.value then
17511760
for base <- bases; tp <- paramTypes do
@@ -1863,17 +1872,17 @@ class Definitions {
18631872
* types `As`, the result type `B` and a whether the type is an erased context function.
18641873
*/
18651874
object ContextFunctionType:
1866-
def unapply(tp: Type)(using Context): Option[(List[Type], Type, List[Boolean])] =
1875+
def unapply(tp: Type)(using Context): Option[(List[Type], Type)] =
18671876
if ctx.erasedTypes then
18681877
atPhase(erasurePhase)(unapply(tp))
18691878
else
18701879
asContextFunctionType(tp) match
18711880
case PolyFunctionOf(mt: MethodType) =>
1872-
Some((mt.paramInfos, mt.resType, mt.erasedParams))
1881+
Some((mt.paramInfos, mt.resType))
18731882
case tp1 if tp1.exists =>
18741883
val args = tp1.functionArgInfos
18751884
val erasedParams = List.fill(functionArity(tp1)) { false }
1876-
Some((args.init, args.last, erasedParams))
1885+
Some((args.init, args.last))
18771886
case _ => None
18781887

18791888
/** A whitelist of Scala-2 classes that are known to be pure */

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ class Bridges(root: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
129129
assert(ctx.typer.isInstanceOf[Erasure.Typer])
130130
ctx.typer.typed(untpd.cpy.Apply(ref)(ref, args), member.info.finalResultType)
131131
else
132-
val defn.ContextFunctionType(argTypes, resType, erasedParams) = tp: @unchecked
132+
val defn.ContextFunctionType(argTypes, resType) = tp: @unchecked
133+
val erasedParams = defn.erasedFunctionParams(tp)
133134
val anonFun = newAnonFun(ctx.owner,
134135
MethodType(
135136
argTypes.zip(erasedParams.padTo(argTypes.length, false))

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ContextFunctionResults:
2020
*/
2121
def annotateContextResults(mdef: DefDef)(using Context): Unit =
2222
def contextResultCount(rhs: Tree, tp: Type): Int = tp match
23-
case defn.ContextFunctionType(_, resTpe, _) =>
23+
case defn.ContextFunctionType(_, resTpe) =>
2424
rhs match
2525
case closureDef(meth) => 1 + contextResultCount(meth.rhs, resTpe)
2626
case _ => 0
@@ -58,7 +58,8 @@ object ContextFunctionResults:
5858
*/
5959
def contextResultsAreErased(sym: Symbol)(using Context): Boolean =
6060
def allErased(tp: Type): Boolean = tp.dealias match
61-
case defn.ContextFunctionType(_, resTpe, erasedParams) => !erasedParams.contains(false) && allErased(resTpe)
61+
case ft @ defn.ContextFunctionType(_, resTpe) =>
62+
!defn.erasedFunctionParams(ft).contains(false) && allErased(resTpe)
6263
case _ => true
6364
contextResultCount(sym) > 0 && allErased(sym.info.finalResultType)
6465

@@ -72,7 +73,7 @@ object ContextFunctionResults:
7273
integrateContextResults(rt, crCount)
7374
case tp: MethodOrPoly =>
7475
tp.derivedLambdaType(resType = integrateContextResults(tp.resType, crCount))
75-
case defn.ContextFunctionType(argTypes, resType, erasedParams) =>
76+
case defn.ContextFunctionType(argTypes, resType) =>
7677
MethodType(argTypes, integrateContextResults(resType, crCount - 1))
7778

7879
/** The total number of parameters of method `sym`, not counting
@@ -83,7 +84,8 @@ object ContextFunctionResults:
8384
def contextParamCount(tp: Type, crCount: Int): Int =
8485
if crCount == 0 then 0
8586
else
86-
val defn.ContextFunctionType(params, resTpe, erasedParams) = tp: @unchecked
87+
val defn.ContextFunctionType(params, resTpe) = tp: @unchecked
88+
val erasedParams = defn.erasedFunctionParams(tp)
8789
val rest = contextParamCount(resTpe, crCount - 1)
8890
if erasedParams.contains(true) then erasedParams.count(_ == false) + rest else params.length + rest
8991

@@ -103,7 +105,7 @@ object ContextFunctionResults:
103105
def recur(tp: Type, n: Int): Type =
104106
if n == 0 then tp
105107
else tp match
106-
case defn.ContextFunctionType(_, resTpe, _) => recur(resTpe, n - 1)
108+
case defn.ContextFunctionType(_, resTpe) => recur(resTpe, n - 1)
107109
recur(meth.info.finalResultType, depth)
108110

109111
/** Should selection `tree` be eliminated since it refers to an `apply`
@@ -118,7 +120,7 @@ object ContextFunctionResults:
118120
case Select(qual, name) =>
119121
if name == nme.apply then
120122
qual.tpe match
121-
case defn.ContextFunctionType(_, _, _) =>
123+
case defn.ContextFunctionType(_, _) =>
122124
integrateSelect(qual, n + 1)
123125
case _ if defn.isContextFunctionClass(tree.symbol.maybeOwner) => // for TermRefs
124126
integrateSelect(qual, n + 1)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ object ErrorReporting {
167167
val normPt = normalize(pt, pt)
168168

169169
def contextFunctionCount(tp: Type): Int = tp.stripped match
170-
case defn.ContextFunctionType(_, restp, _) => 1 + contextFunctionCount(restp)
170+
case defn.ContextFunctionType(_, restp) => 1 + contextFunctionCount(restp)
171171
case _ => 0
172172
def strippedTpCount = contextFunctionCount(tree.tpe) - contextFunctionCount(normTp)
173173
def strippedPtCount = contextFunctionCount(pt) - contextFunctionCount(normPt)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ class Namer { typer: Typer =>
18931893
val originalTp = defaultParamType
18941894
val approxTp = wildApprox(originalTp)
18951895
approxTp.stripPoly match
1896-
case atp @ defn.ContextFunctionType(_, resType, _)
1896+
case atp @ defn.ContextFunctionType(_, resType)
18971897
if !defn.isNonRefinedFunction(atp) // in this case `resType` is lying, gives us only the non-dependent upper bound
18981898
|| resType.existsPart(_.isInstanceOf[WildcardType], StopAt.Static, forceLazy = false) =>
18991899
originalTp

0 commit comments

Comments
 (0)