Skip to content

Backport "Remove erasure logic from defn.ContextFunctionType" to LTS #20607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {

def etaExpandCFT(using Context): Tree =
def expand(target: Tree, tp: Type)(using Context): Tree = tp match
case defn.ContextFunctionType(argTypes, resType, _) =>
case defn.ContextFunctionType(argTypes, resType) =>
val anonFun = newAnonFun(
ctx.owner,
MethodType.companion(isContextual = true)(argTypes, resType),
Expand Down
20 changes: 8 additions & 12 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1881,18 +1881,14 @@ class Definitions {
* types `As`, the result type `B` and a whether the type is an erased context function.
*/
object ContextFunctionType:
def unapply(tp: Type)(using Context): Option[(List[Type], Type, List[Boolean])] =
if ctx.erasedTypes then
atPhase(erasurePhase)(unapply(tp))
else
asContextFunctionType(tp) match
case ErasedFunctionOf(mt) =>
Some((mt.paramInfos, mt.resType, mt.erasedParams))
case tp1 if tp1.exists =>
val args = tp1.functionArgInfos
val erasedParams = erasedFunctionParameters(tp1)
Some((args.init, args.last, erasedParams))
case _ => None
def unapply(tp: Type)(using Context): Option[(List[Type], Type)] =
asContextFunctionType(tp) match
case PolyFunctionOf(mt: MethodType) =>
Some((mt.paramInfos, mt.resType))
case tp1 if tp1.exists =>
val args = tp1.functionArgInfos
Some((args.init, args.last))
case _ => None

/* Returns a list of erased booleans marking whether parameters are erased, for a function type. */
def erasedFunctionParameters(tp: Type)(using Context): List[Boolean] = tp.dealias match {
Expand Down
17 changes: 8 additions & 9 deletions compiler/src/dotty/tools/dotc/transform/Bridges.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,24 @@ class Bridges(root: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
assert(ctx.typer.isInstanceOf[Erasure.Typer])
ctx.typer.typed(untpd.cpy.Apply(ref)(ref, args), member.info.finalResultType)
else
val defn.ContextFunctionType(argTypes, resType, erasedParams) = tp: @unchecked
val anonFun = newAnonFun(ctx.owner,
MethodType(
argTypes.zip(erasedParams.padTo(argTypes.length, false))
.flatMap((t, e) => if e then None else Some(t)),
resType),
coord = ctx.owner.coord)
val mtWithoutErasedParams = atPhase(erasurePhase) {
val defn.ContextFunctionType(argTypes, resType) = tp.dealias: @unchecked
val paramInfos = argTypes.filterNot(_.hasAnnotation(defn.ErasedParamAnnot))
MethodType(paramInfos, resType)
}
val anonFun = newAnonFun(ctx.owner, mtWithoutErasedParams, coord = ctx.owner.coord)
anonFun.info = transformInfo(anonFun, anonFun.info)

def lambdaBody(refss: List[List[Tree]]) =
val refs :: Nil = refss: @unchecked
val expandedRefs = refs.map(_.withSpan(ctx.owner.span.endPos)) match
case (bunchedParam @ Ident(nme.ALLARGS)) :: Nil =>
argTypes.indices.toList.map(n =>
mtWithoutErasedParams.paramInfos.indices.toList.map(n =>
bunchedParam
.select(nme.primitive.arrayApply)
.appliedTo(Literal(Constant(n))))
case refs1 => refs1
expand(args ::: expandedRefs, resType, n - 1)(using ctx.withOwner(anonFun))
expand(args ::: expandedRefs, mtWithoutErasedParams.resType, n - 1)(using ctx.withOwner(anonFun))

val unadapted = Closure(anonFun, lambdaBody)
cpy.Block(unadapted)(unadapted.stats,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ContextFunctionResults:
*/
def annotateContextResults(mdef: DefDef)(using Context): Unit =
def contextResultCount(rhs: Tree, tp: Type): Int = tp match
case defn.ContextFunctionType(_, resTpe, _) =>
case defn.ContextFunctionType(_, resTpe) =>
rhs match
case closureDef(meth) => 1 + contextResultCount(meth.rhs, resTpe)
case _ => 0
Expand Down Expand Up @@ -58,7 +58,8 @@ object ContextFunctionResults:
*/
def contextResultsAreErased(sym: Symbol)(using Context): Boolean =
def allErased(tp: Type): Boolean = tp.dealias match
case defn.ContextFunctionType(_, resTpe, erasedParams) => !erasedParams.contains(false) && allErased(resTpe)
case defn.ContextFunctionType(argTpes, resTpe) =>
argTpes.forall(_.hasAnnotation(defn.ErasedParamAnnot)) && allErased(resTpe)
case _ => true
contextResultCount(sym) > 0 && allErased(sym.info.finalResultType)

Expand All @@ -72,7 +73,7 @@ object ContextFunctionResults:
integrateContextResults(rt, crCount)
case tp: MethodOrPoly =>
tp.derivedLambdaType(resType = integrateContextResults(tp.resType, crCount))
case defn.ContextFunctionType(argTypes, resType, erasedParams) =>
case defn.ContextFunctionType(argTypes, resType) =>
MethodType(argTypes, integrateContextResults(resType, crCount - 1))

/** The total number of parameters of method `sym`, not counting
Expand All @@ -83,9 +84,10 @@ object ContextFunctionResults:
def contextParamCount(tp: Type, crCount: Int): Int =
if crCount == 0 then 0
else
val defn.ContextFunctionType(params, resTpe, erasedParams) = tp: @unchecked
val defn.ContextFunctionType(params, resTpe) = tp: @unchecked
val rest = contextParamCount(resTpe, crCount - 1)
if erasedParams.contains(true) then erasedParams.count(_ == false) + rest else params.length + rest
val nonErasedParams = params.count(!_.hasAnnotation(defn.ErasedParamAnnot))
nonErasedParams + rest

def normalParamCount(tp: Type): Int = tp.widenExpr.stripPoly match
case mt @ MethodType(pnames) =>
Expand All @@ -103,7 +105,7 @@ object ContextFunctionResults:
def recur(tp: Type, n: Int): Type =
if n == 0 then tp
else tp match
case defn.ContextFunctionType(_, resTpe, _) => recur(resTpe, n - 1)
case defn.ContextFunctionType(_, resTpe) => recur(resTpe, n - 1)
recur(meth.info.finalResultType, depth)

/** Should selection `tree` be eliminated since it refers to an `apply`
Expand All @@ -118,7 +120,7 @@ object ContextFunctionResults:
case Select(qual, name) =>
if name == nme.apply then
qual.tpe match
case defn.ContextFunctionType(_, _, _) =>
case defn.ContextFunctionType(_, _) =>
integrateSelect(qual, n + 1)
case _ if defn.isContextFunctionClass(tree.symbol.maybeOwner) => // for TermRefs
integrateSelect(qual, n + 1)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ object ErrorReporting {
val normPt = normalize(pt, pt)

def contextFunctionCount(tp: Type): Int = tp.stripped match
case defn.ContextFunctionType(_, restp, _) => 1 + contextFunctionCount(restp)
case defn.ContextFunctionType(_, restp) => 1 + contextFunctionCount(restp)
case _ => 0
def strippedTpCount = contextFunctionCount(tree.tpe) - contextFunctionCount(normTp)
def strippedPtCount = contextFunctionCount(pt) - contextFunctionCount(normPt)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ class Namer { typer: Typer =>
val originalTp = defaultParamType
val approxTp = wildApprox(originalTp)
approxTp.stripPoly match
case atp @ defn.ContextFunctionType(_, resType, _)
case atp @ defn.ContextFunctionType(_, resType)
if !defn.isNonRefinedFunction(atp) // in this case `resType` is lying, gives us only the non-dependent upper bound
|| resType.existsPart(_.isInstanceOf[WildcardType], StopAt.Static, forceLazy = false) =>
originalTp
Expand Down
Loading