Skip to content

Fix errors in the global initialization checker when compiling bootstrapped dotty #23429

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,15 @@ class Objects(using Context @constructorOnly):
case env: EnvRef =>
env.initVal(x, value)
case ref: Ref =>
ref.initVal(x, value) // TODO: This is possible for match statement in class body. Report warning?
ref.initVal(x, value) // This is possible for match statement in class body.

def setLocalVar(x: Symbol, value: Value)(using scope: Scope, ctx: Context, heap: Heap.MutableData, envMap: EnvMap.EnvMapMutableData): Unit =
assert(x.is(Flags.Mutable, butNot = Flags.Param), "Only local mutable variable allowed")
scope match
case env: EnvRef =>
env.initVar(x, value)
case ref: Ref =>
ref.initVar(x, value) // TODO: This is possible for match statement in class body. Report warning?
ref.initVar(x, value) // This is possible for match statement in class body.

/**
* Resolve the environment by searching for a given symbol.
Expand Down Expand Up @@ -986,15 +986,7 @@ class Objects(using Context @constructorOnly):
// Assume such method is pure. Check return type, only try to analyze body if return type is not safe
val target = resolve(v.typeSymbol.asClass, meth)
val targetType = target.denot.info
assert(targetType.isInstanceOf[ExprType] || targetType.isInstanceOf[MethodType],
"Unexpected type! Receiver = " + v.show + ", meth = " + target + ", type = " + targetType)
val returnType =
if targetType.isInstanceOf[ExprType] then
// corresponds to parameterless method like `def meth: ExprType[T]`
// See pos/toDouble.scala
targetType.asInstanceOf[ExprType].resType
else
targetType.asInstanceOf[MethodType].resType
val returnType = targetType.finalResultType
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice simplification 👍

val typeSymbol = SafeValue.getSafeTypeSymbol(returnType)
if typeSymbol.isDefined then
// since method is pure and return type is safe, no need to analyze method body
Expand Down Expand Up @@ -1326,7 +1318,8 @@ class Objects(using Context @constructorOnly):
report.warning("[Internal error] top-level class should have `Package` as outer, class = " + klass.show + ", outer = " + outer.show + ", " + Trace.show, Trace.position)
(Bottom, Env.NoEnv)
else
val outerCls = klass.owner.enclosingClass.asClass
// enclosingClass is specially handled for java static terms, so use `lexicallyEnclosingClass` here
val outerCls = klass.owner.lexicallyEnclosingClass.asClass
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch and comment 👍

// When `klass` is directly nested in `outerCls`, `outerCls`.enclosingMethod returns its primary constructor
if klass.owner.enclosingMethod == outerCls.primaryConstructor then
(outer, Env.NoEnv)
Expand Down Expand Up @@ -1813,6 +1806,7 @@ class Objects(using Context @constructorOnly):
val toSeqResTp = resultTp.memberInfo(selectors.last).finalResultType
evalSeqPatterns(toSeqRes, toSeqResTp, elemTp, seqPats)
end if
// TODO: refactor the code of product sequence match, avoid passing NoType to parameter elemTp in evalSeqPatterns

else
// distribute unapply to patterns
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Trace.*
object Util:
/** Exception used for errors encountered when reading TASTy. */
case class TastyTreeException(msg: String) extends RuntimeException(msg)

/** Utility definition used for better error-reporting of argument errors */
case class TraceValue[T](value: T, trace: Trace)

Expand Down Expand Up @@ -96,7 +96,7 @@ object Util:
else sym.matchingMember(cls.appliedRef)

extension (sym: Symbol)
def hasSource(using Context): Boolean = !sym.defTree.isEmpty
def hasSource(using Context): Boolean = !sym.is(Flags.JavaDefined) && !sym.defTree.isEmpty

def isStaticObject(using Context) =
sym.is(Flags.Module, butNot = Flags.Package) && sym.isStatic
Expand Down
17 changes: 17 additions & 0 deletions tests/init-global/pos/enum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
enum FileExtension(val toLowerCase: String):
case Tasty extends FileExtension("tasty")
case Betasty extends FileExtension("betasty")
case Class extends FileExtension("class")
case Jar extends FileExtension("jar")
case Scala extends FileExtension("scala")
case ScalaScript extends FileExtension("sc")
case Java extends FileExtension("java")
case Zip extends FileExtension("zip")
case Inc extends FileExtension("inc")
case Empty extends FileExtension("")

/** Fallback extension */
case External(override val toLowerCase: String) extends FileExtension(toLowerCase)

object O:
val a = FileExtension.Empty
Loading