Skip to content

Commit 89e8dba

Browse files
Remove erasure logic from defn.ContextFunctionType (#18466)
2 parents a4660af + 7c0a848 commit 89e8dba

File tree

6 files changed

+28
-32
lines changed

6 files changed

+28
-32
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11521152

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

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -1878,18 +1878,14 @@ class Definitions {
18781878
* types `As`, the result type `B` and a whether the type is an erased context function.
18791879
*/
18801880
object ContextFunctionType:
1881-
def unapply(tp: Type)(using Context): Option[(List[Type], Type, List[Boolean])] =
1882-
if ctx.erasedTypes then
1883-
atPhase(erasurePhase)(unapply(tp))
1884-
else
1885-
asContextFunctionType(tp) match
1886-
case PolyFunctionOf(mt: MethodType) =>
1887-
Some((mt.paramInfos, mt.resType, mt.erasedParams))
1888-
case tp1 if tp1.exists =>
1889-
val args = tp1.functionArgInfos
1890-
val erasedParams = List.fill(functionArity(tp1)) { false }
1891-
Some((args.init, args.last, erasedParams))
1892-
case _ => None
1881+
def unapply(tp: Type)(using Context): Option[(List[Type], Type)] =
1882+
asContextFunctionType(tp) match
1883+
case PolyFunctionOf(mt: MethodType) =>
1884+
Some((mt.paramInfos, mt.resType))
1885+
case tp1 if tp1.exists =>
1886+
val args = tp1.functionArgInfos
1887+
Some((args.init, args.last))
1888+
case _ => None
18931889

18941890
/** A whitelist of Scala-2 classes that are known to be pure */
18951891
def isAssuredNoInits(sym: Symbol): Boolean =

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,24 @@ 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
133-
val anonFun = newAnonFun(ctx.owner,
134-
MethodType(
135-
argTypes.zip(erasedParams.padTo(argTypes.length, false))
136-
.flatMap((t, e) => if e then None else Some(t)),
137-
resType),
138-
coord = ctx.owner.coord)
132+
val mtWithoutErasedParams = atPhase(erasurePhase) {
133+
val defn.ContextFunctionType(argTypes, resType) = tp.dealias: @unchecked
134+
val paramInfos = argTypes.filterNot(_.hasAnnotation(defn.ErasedParamAnnot))
135+
MethodType(paramInfos, resType)
136+
}
137+
val anonFun = newAnonFun(ctx.owner, mtWithoutErasedParams, coord = ctx.owner.coord)
139138
anonFun.info = transformInfo(anonFun, anonFun.info)
140139

141140
def lambdaBody(refss: List[List[Tree]]) =
142141
val refs :: Nil = refss: @unchecked
143142
val expandedRefs = refs.map(_.withSpan(ctx.owner.span.endPos)) match
144143
case (bunchedParam @ Ident(nme.ALLARGS)) :: Nil =>
145-
argTypes.indices.toList.map(n =>
144+
mtWithoutErasedParams.paramInfos.indices.toList.map(n =>
146145
bunchedParam
147146
.select(nme.primitive.arrayApply)
148147
.appliedTo(Literal(Constant(n))))
149148
case refs1 => refs1
150-
expand(args ::: expandedRefs, resType, n - 1)(using ctx.withOwner(anonFun))
149+
expand(args ::: expandedRefs, mtWithoutErasedParams.resType, n - 1)(using ctx.withOwner(anonFun))
151150

152151
val unadapted = Closure(anonFun, lambdaBody)
153152
cpy.Block(unadapted)(unadapted.stats,

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

+9-8
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 defn.ContextFunctionType(argTpes, resTpe) =>
62+
argTpes.forall(_.hasAnnotation(defn.ErasedParamAnnot)) && 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,10 +84,10 @@ 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
8788
val rest = contextParamCount(resTpe, crCount - 1)
88-
// TODO use mt.nonErasedParamCount
89-
if erasedParams.contains(true) then erasedParams.count(_ == false) + rest else params.length + rest // TODO use mt.nonErasedParamCount
89+
val nonErasedParams = params.count(!_.hasAnnotation(defn.ErasedParamAnnot))
90+
nonErasedParams + rest
9091

9192
def normalParamCount(tp: Type): Int = tp.widenExpr.stripPoly match
9293
case mt @ MethodType(pnames) => mt.nonErasedParamCount + normalParamCount(mt.resType)
@@ -100,7 +101,7 @@ object ContextFunctionResults:
100101
def recur(tp: Type, n: Int): Type =
101102
if n == 0 then tp
102103
else tp match
103-
case defn.ContextFunctionType(_, resTpe, _) => recur(resTpe, n - 1)
104+
case defn.ContextFunctionType(_, resTpe) => recur(resTpe, n - 1)
104105
recur(meth.info.finalResultType, depth)
105106

106107
/** Should selection `tree` be eliminated since it refers to an `apply`
@@ -115,7 +116,7 @@ object ContextFunctionResults:
115116
case Select(qual, name) =>
116117
if name == nme.apply then
117118
qual.tpe match
118-
case defn.ContextFunctionType(_, _, _) =>
119+
case defn.ContextFunctionType(_, _) =>
119120
integrateSelect(qual, n + 1)
120121
case _ if defn.isContextFunctionClass(tree.symbol.maybeOwner) => // for TermRefs
121122
integrateSelect(qual, n + 1)

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

+1-1
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

+1-1
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)