Skip to content

Commit e570bf7

Browse files
committed
Use erasedParams instead of continuously unwrapping
1 parent 4aa4db4 commit e570bf7

File tree

13 files changed

+23
-37
lines changed

13 files changed

+23
-37
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,14 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
270270
else makeSym(origInfo)
271271
end valueParam
272272

273-
val erasedParams = if tp.isErasedMethod
274-
then tp.companion.asInstanceOf[ErasedMethodCompanion].isErased
275-
else List.fill(tp.paramNames.size) { false }
276-
277273
val (vparams: List[TermSymbol], remaining1) =
278274
if tp.paramNames.isEmpty then (Nil, remaining)
279275
else remaining match
280276
case vparams :: remaining1 =>
281277
assert(vparams.hasSameLengthAs(tp.paramNames) && vparams.head.isTerm)
282278
(vparams.asInstanceOf[List[TermSymbol]], remaining1)
283279
case nil =>
284-
(tp.paramNames.lazyZip(tp.paramInfos).lazyZip(erasedParams).map(valueParam), Nil)
280+
(tp.paramNames.lazyZip(tp.paramInfos).lazyZip(tp.erasedParams).map(valueParam), Nil)
285281
val (rtp, paramss) = recur(tp.instantiate(vparams.map(_.termRef)), remaining1)
286282
(rtp, vparams :: paramss)
287283
case _ =>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,8 +1826,7 @@ class Definitions {
18261826

18271827
/* Returns a list of erased booleans marking whether parameters are erased, for a function type. */
18281828
def erasedFunctionParameters(tp: Type)(using Context): List[Boolean] = tp.dealias match {
1829-
case RefinedType(parent, nme.apply, mt) if defn.isErasedFunctionType(parent) =>
1830-
mt.asInstanceOf[MethodType].companion.asInstanceOf[ErasedMethodCompanion].isErased
1829+
case RefinedType(parent, nme.apply, mt: MethodType) => mt.erasedParams
18311830
case tp if isFunctionType(tp) => List.fill(functionArity(tp)) { false }
18321831
case _ => List()
18331832
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,9 +2119,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
21192119
}
21202120
// Check if methods are erased, then the erased parameters match
21212121
val erasedValid = if tp1.isErasedMethod && tp2.isErasedMethod then
2122-
val comp1 = tp1.companion.asInstanceOf[ErasedMethodCompanion]
2123-
val comp2 = tp2.companion.asInstanceOf[ErasedMethodCompanion]
2124-
comp1.isErased == comp2.isErased
2122+
tp1.erasedParams == tp2.erasedParams
21252123
else !tp1.isErasedMethod && !tp2.isErasedMethod
21262124

21272125
erasedValid && loop(tp1.paramInfos, tp2.paramInfos)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,8 @@ object TypeErasure {
539539

540540
def eraseErasedFunctionApply(applyInfo: Type)(using Context): Type =
541541
val erasedFn = applyInfo.asInstanceOf[MethodType]
542-
val companion = erasedFn.companion.asInstanceOf[ErasedMethodCompanion]
543542
val fnType = defn.FunctionType(
544-
n = companion.isErased.count(_ == false),
543+
n = erasedFn.erasedParams.count(_ == false),
545544
isErased = false,
546545
isContextual = erasedFn.isContextualMethod,
547546
)
@@ -653,7 +652,7 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
653652
val (names, formals0) = if (tp.isErasedMethod)
654653
tp.paramNames
655654
.zip(tp.paramInfos)
656-
.zip(tp.companion.asInstanceOf[ErasedMethodCompanion].isErased)
655+
.zip(tp.erasedParams)
657656
.flatMap((p, e) => if e then None else Some(p))
658657
.unzip
659658
else (tp.paramNames, tp.paramInfos)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ object Types {
18421842
val isContextual = mt.isContextualMethod && !ctx.erasedTypes
18431843
val isErased =
18441844
(if mt.isErasedMethod && !ctx.erasedTypes then
1845-
mt.companion.asInstanceOf[ErasedMethodCompanion].isErased
1845+
mt.erasedParams
18461846
else List.fill(mt.paramInfos.size) { false }) dropRight dropLast
18471847
val result1 = mt.nonDependentResultApprox match {
18481848
case res: MethodType => res.toFunctionType(isJava)
@@ -3621,6 +3621,8 @@ object Types {
36213621

36223622
def companion: LambdaTypeCompanion[ThisName, PInfo, This]
36233623

3624+
def erasedParams = List.fill(paramInfos.size)(false)
3625+
36243626
/** The type `[tparams := paramRefs] tp`, where `tparams` can be
36253627
* either a list of type parameter symbols or a list of lambda parameters
36263628
*
@@ -3700,7 +3702,7 @@ object Types {
37003702
case tp: MethodType =>
37013703
val params = if (isErasedMethod)
37023704
tp.paramInfos
3703-
.zip(tp.companion.asInstanceOf[ErasedMethodCompanion].isErased)
3705+
.zip(tp.erasedParams)
37043706
.flatMap((p, e) => if e then None else Some(p))
37053707
else tp.paramInfos
37063708
resultSignature.prependTermParams(params, sourceLanguage)
@@ -3918,9 +3920,9 @@ object Types {
39183920
companion.eq(ContextualMethodType) ||
39193921
companion.isInstanceOf[ErasedContextualMethodType]
39203922

3921-
def erasedParams: List[Boolean] = companion match
3923+
override def erasedParams: List[Boolean] = companion match
39223924
case c: ErasedMethodCompanion => c.isErased
3923-
case _ => paramInfos.map(_ => false)
3925+
case _ => super.erasedParams
39243926

39253927
def withoutErased(using Context): MethodType =
39263928
val newCompanion = companion match {

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,7 @@ class TreePickler(pickler: TastyPickler) {
301301
writeByte(tag)
302302
if tag == ERASEDMETHODtype then
303303
withLength {
304-
tpe.companion.asInstanceOf[ErasedMethodCompanion]
305-
.isErased
306-
.foreach { isErased => writeByte(if isErased then 1 else 0) }
304+
tpe.erasedParams.foreach { isErased => writeByte(if isErased then 1 else 0) }
307305
}
308306
withLength {
309307
pickleType(tpe.resultType, richTypes = true)

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
296296
"(" ~ toTextRef(tp) ~ " : " ~ toTextGlobal(tp.underlying) ~ ")"
297297

298298
protected def paramsText(lam: LambdaType): Text = {
299-
val companion = lam.companion
300-
val isErased = if companion.isInstanceOf[ErasedMethodCompanion] then
301-
companion.asInstanceOf[ErasedMethodCompanion].isErased
302-
else List.fill(lam.paramNames.length)(false)
299+
val isErased = lam.erasedParams
303300
def paramText(name: Name, tp: Type, erased: Boolean) =
304301
keywordText("erased ").provided(erased) ~ toText(name) ~ lambdaHash(lam) ~ toTextRHS(tp, isParameter = true)
305302
Text(lam.paramNames.lazyZip(lam.paramInfos).lazyZip(isErased).map(paramText), ", ")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ object ContextFunctionResults:
9393
case mt @ MethodType(pnames) =>
9494
val rest = normalParamCount(mt.resType)
9595
if mt.isErasedMethod then
96-
mt.companion.asInstanceOf[ErasedMethodCompanion].isErased.count(_ == false) + rest
96+
mt.erasedParams.count(_ == false) + rest
9797
else pnames.length + rest
9898
case _ => contextParamCount(tp, contextResultCount(sym))
9999

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,10 +828,10 @@ object Erasure {
828828
val origFun = fun.asInstanceOf[tpd.Tree]
829829
val origFunType = origFun.tpe.widen(using preErasureCtx)
830830
val erasedCompanion = if origFunType.isErasedMethod then
831-
Some(origFunType.asInstanceOf[MethodType].companion.asInstanceOf[ErasedMethodCompanion])
831+
Some(origFunType.asInstanceOf[MethodType].erasedParams)
832832
else None
833-
val ownArgs = erasedCompanion.map { companion =>
834-
args.zip(companion.isErased).flatMap((a, e) => if e then None else Some(a))
833+
val ownArgs = erasedCompanion.map { isErased =>
834+
args.zip(isErased).flatMap((a, e) => if e then None else Some(a))
835835
}.getOrElse(args)
836836
val fun1 = typedExpr(fun, AnyFunctionProto)
837837
fun1.tpe.widen match

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ object GenericSignatures {
312312
// erased method parameters do not make it to the bytecode.
313313
def effectiveParamInfoss(t: Type)(using Context): List[List[Type]] = t match {
314314
case t: MethodType if t.isErasedMethod =>
315-
t.paramInfos.zip(t.companion.asInstanceOf[ErasedMethodCompanion].isErased)
315+
t.paramInfos.zip(t.erasedParams)
316316
.flatMap((i, e) => if e then None else Some(i))
317317
:: effectiveParamInfoss(t.resType)
318318
case t: MethodType => t.paramInfos :: effectiveParamInfoss(t.resType)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
299299
if (methType.isErasedMethod)
300300
tpd.cpy.Apply(tree)(
301301
tree.fun,
302-
tree.args.zip(methType.companion.asInstanceOf[ErasedMethodCompanion].isErased).map((arg, isErased) =>
302+
tree.args.zip(methType.erasedParams).map((arg, isErased) =>
303303
if !isErased then arg
304304
else
305305
if methType.isResultDependent then

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
4242
override def transformApply(tree: Apply)(using Context): Tree =
4343
if !tree.fun.tpe.widen.isErasedMethod then tree
4444
else
45-
val comp = tree.fun.tpe.widen.asInstanceOf[MethodType].companion.asInstanceOf[ErasedMethodCompanion]
46-
cpy.Apply(tree)(tree.fun, tree.args.zip(comp.isErased).map((a, e) => if e then trivialErasedTree(a) else a))
45+
val erasedParams = tree.fun.tpe.widen.asInstanceOf[MethodType].erasedParams
46+
cpy.Apply(tree)(tree.fun, tree.args.zip(erasedParams).map((a, e) => if e then trivialErasedTree(a) else a))
4747

4848
override def transformValDef(tree: ValDef)(using Context): Tree =
4949
checkErasedInExperimental(tree.symbol)

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12191219
}
12201220

12211221
val pt1 = pt.strippedDealias.normalized
1222-
// println(s"\tpt = ${pt1.show}\n\tdropped refinement = ${pt1.dropDependentRefinement.dealias.show}\n\tarity = $defaultArity")
12231222
if (pt1 ne pt1.dropDependentRefinement)
12241223
&& defn.isContextFunctionType(pt1.nonPrivateMember(nme.apply).info.finalResultType)
12251224
&& pt1.nonPrivateMember(nme.apply).info.asInstanceOf[MethodType].isResultDependent
@@ -3031,10 +3030,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30313030
else formals.map(untpd.TypeTree)
30323031
}
30333032

3034-
val erasedParams = pt match {
3035-
case RefinedType(parent, nme.apply, mt) if defn.isErasedFunctionType(parent) =>
3036-
val companion = mt.asInstanceOf[MethodType].companion.asInstanceOf[ErasedMethodCompanion]
3037-
companion.isErased
3033+
val erasedParams = pt.dealias match {
3034+
case RefinedType(parent, nme.apply, mt: MethodType) => mt.erasedParams
30383035
case _ => paramTypes.map(_ => false)
30393036
}
30403037

0 commit comments

Comments
 (0)