Skip to content

Commit 39ccc7d

Browse files
committed
Fix #10131: load enum types more lazily
1 parent 1ab76c1 commit 39ccc7d

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class ClassfileParser(
518518
}
519519
// sigToType
520520

521-
def parseAnnotArg(skip: Boolean = false)(using ctx: Context, in: DataReader): Option[untpd.Tree] = {
521+
def parseAnnotArg(skip: Boolean = false)(using ctx: Context, in: DataReader): Option[untpd.Tree | (Context => untpd.Tree)] = {
522522

523523
// If we encounter an empty array literal, we need the type of the corresponding
524524
// parameter to properly type it, but that would require forcing the annotation
@@ -545,20 +545,26 @@ class ClassfileParser(
545545
case CLASS_TAG =>
546546
if (skip) None else Some(lit(Constant(pool.getType(index))))
547547
case ENUM_TAG =>
548-
val enumClassTp = pool.getType(index)
548+
val sig = pool.getExternalName(index).value
549549
val enumCaseName = pool.getName(in.nextChar)
550550
if (skip)
551551
None
552552
else {
553-
val enumModuleClass = enumClassTp.classSymbol.companionModule
554-
Some(Select(ref(enumModuleClass), enumCaseName.name))
553+
val res = (ctx: Context) => {
554+
implicit val ctx2 = ctx
555+
val enumClassTp = sigToType(sig)
556+
val enumModuleClass = enumClassTp.classSymbol.companionModule
557+
Select(ref(enumModuleClass), enumCaseName.name)
558+
}
559+
Some(res)
555560
}
556561
case ARRAY_TAG =>
557562
val arr = new ArrayBuffer[Tree]()
558563
var hasError = false
559564
for (i <- 0 until index)
560565
parseAnnotArg(skip) match {
561-
case Some(c) => arr += c
566+
case Some(c: untpd.Tree) => arr += c
567+
case Some(fun: (Context => untpd.Tree) @unchecked) => arr += fun(ctx)
562568
case None => hasError = true
563569
}
564570
if (hasError) None
@@ -572,7 +578,13 @@ class ClassfileParser(
572578
}
573579
}
574580

575-
class ClassfileAnnotation(annotType: Type, args: List[untpd.Tree]) extends LazyAnnotation {
581+
class ClassfileAnnotation(annotType: Type, lazyArgs: List[untpd.Tree | (Context => untpd.Tree)]) extends LazyAnnotation {
582+
val args: Context ?=> List[untpd.Tree] = (using ctx: Context) =>
583+
lazyArgs.map {
584+
case tree: untpd.Tree => tree
585+
case fun: (Context => untpd.Tree) @unchecked => fun(ctx)
586+
}
587+
576588
protected var mySym: Symbol | (Context ?=> Symbol) =
577589
(using ctx: Context) => annotType.classSymbol
578590

@@ -598,13 +610,19 @@ class ClassfileParser(
598610
case _ =>
599611

600612
val nargs = in.nextChar
601-
val argbuf = new ListBuffer[untpd.Tree]
613+
val argbuf = new ListBuffer[untpd.Tree | (Context => untpd.Tree)]
602614
var hasError = false
603615
for (i <- 0 until nargs) {
604616
val name = pool.getName(in.nextChar)
605617
parseAnnotArg(skip) match {
606-
case Some(arg) => argbuf += untpd.NamedArg(name.name, arg)
607-
case None => hasError = !skip
618+
case Some(fun: (Context => untpd.Tree) @unchecked) =>
619+
argbuf += ((ctx: Context) => untpd.NamedArg(name.name, fun(ctx)))
620+
621+
case Some(arg: untpd.Tree) =>
622+
argbuf += untpd.NamedArg(name.name, arg)
623+
624+
case None =>
625+
hasError = !skip
608626
}
609627
}
610628
if (hasError || skip) None

0 commit comments

Comments
 (0)