Skip to content

Commit

Permalink
Improve erased params logic (#18433)
Browse files Browse the repository at this point in the history
Part of #18305
  • Loading branch information
nicolasstucki committed Aug 31, 2023
2 parents 3df2ed3 + fa1f79d commit 4c77f62
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ object TypeErasure {
functionType(info.resultType)
case info: MethodType =>
assert(!info.resultType.isInstanceOf[MethodicType])
defn.FunctionType(n = info.erasedParams.count(_ == false))
defn.FunctionType(n = info.nonErasedParamCount)
}
erasure(functionType(applyInfo))
}
Expand Down
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3724,8 +3724,6 @@ object Types {

def companion: LambdaTypeCompanion[ThisName, PInfo, This]

def erasedParams(using Context) = List.fill(paramInfos.size)(false)

/** The type `[tparams := paramRefs] tp`, where `tparams` can be
* either a list of type parameter symbols or a list of lambda parameters
*
Expand Down Expand Up @@ -4017,13 +4015,18 @@ object Types {
final override def isImplicitMethod: Boolean =
companion.eq(ImplicitMethodType) || isContextualMethod
final override def hasErasedParams(using Context): Boolean =
erasedParams.contains(true)
paramInfos.exists(p => p.hasAnnotation(defn.ErasedParamAnnot))

final override def isContextualMethod: Boolean =
companion.eq(ContextualMethodType)

override def erasedParams(using Context): List[Boolean] =
def erasedParams(using Context): List[Boolean] =
paramInfos.map(p => p.hasAnnotation(defn.ErasedParamAnnot))

def nonErasedParamCount(using Context): Int =
paramInfos.count(p => !p.hasAnnotation(defn.ErasedParamAnnot))


protected def prefixString: String = companion.prefixString
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ class PlainPrinter(_ctx: Context) extends Printer {
"(" ~ toTextRef(tp) ~ " : " ~ toTextGlobal(tp.underlying) ~ ")"

protected def paramsText(lam: LambdaType): Text = {
val erasedParams = lam.erasedParams
def paramText(ref: ParamRef, erased: Boolean) =
def paramText(ref: ParamRef) =
val erased = ref.underlying.hasAnnotation(defn.ErasedParamAnnot)
keywordText("erased ").provided(erased) ~ ParamRefNameString(ref) ~ lambdaHash(lam) ~ toTextRHS(ref.underlying, isParameter = true)
Text(lam.paramRefs.lazyZip(erasedParams).map(paramText), ", ")
Text(lam.paramRefs.map(paramText), ", ")
}

protected def ParamRefNameString(name: Name): String = nameString(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ object ContextFunctionResults:
else
val defn.ContextFunctionType(params, resTpe, erasedParams) = tp: @unchecked
val rest = contextParamCount(resTpe, crCount - 1)
if erasedParams.contains(true) then erasedParams.count(_ == false) + rest else params.length + rest
// TODO use mt.nonErasedParamCount
if erasedParams.contains(true) then erasedParams.count(_ == false) + rest else params.length + rest // TODO use mt.nonErasedParamCount

def normalParamCount(tp: Type): Int = tp.widenExpr.stripPoly match
case mt @ MethodType(pnames) =>
val rest = normalParamCount(mt.resType)
if mt.hasErasedParams then
mt.erasedParams.count(_ == false) + rest
else pnames.length + rest
case mt @ MethodType(pnames) => mt.nonErasedParamCount + normalParamCount(mt.resType)
case _ => contextParamCount(tp, contextResultCount(sym))

normalParamCount(sym.info)
Expand Down

0 comments on commit 4c77f62

Please sign in to comment.