Skip to content

Improve erased params logic #18433

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 4 commits into from
Aug 31, 2023
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/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 @@ -3721,8 +3721,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 @@ -4014,13 +4012,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
Comment on lines +88 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two TODOs here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove that code soon with other parts of #18305. I'll remove both todos then.


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