Skip to content

Commit 6e110f5

Browse files
EnzeXingolhotak
authored andcommitted
Fix errors when compiling bootstrapped dotty
1 parent d109a39 commit 6e110f5

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ class Objects(using Context @constructorOnly):
151151
def hasVar(sym: Symbol)(using Heap.MutableData): Boolean = Heap.containsVal(this, sym)
152152

153153
def initVal(field: Symbol, value: Value)(using Context, Heap.MutableData) = log("Initialize " + field.show + " = " + value + " for " + this, printer) {
154-
assert(!field.is(Flags.Mutable), "Field is mutable: " + field.show)
154+
assert(field.is(Flags.Param) || !field.is(Flags.Mutable), "Field is mutable: " + field.show)
155155
Heap.writeJoinVal(this, field, value)
156156
}
157157

158158
def initVar(field: Symbol, value: Value)(using Context, Heap.MutableData) = log("Initialize " + field.show + " = " + value + " for " + this, printer) {
159-
assert(field.is(Flags.Mutable), "Field is not mutable: " + field.show)
159+
assert(field.is(Flags.Mutable, butNot = Flags.Param), "Field is not mutable: " + field.show)
160160
Heap.writeJoinVal(this, field, value)
161161
}
162162

@@ -421,12 +421,12 @@ class Objects(using Context @constructorOnly):
421421
def hasVar(sym: Symbol)(using EnvMap.EnvMapMutableData): Boolean = EnvMap.containsVal(this, sym)
422422

423423
def initVal(field: Symbol, value: Value)(using Context, EnvMap.EnvMapMutableData) = log("Initialize " + field.show + " = " + value + " for " + this, printer) {
424-
assert(!field.is(Flags.Mutable), "Field is mutable: " + field.show)
424+
assert(field.is(Flags.Param) || !field.is(Flags.Mutable), "Field is mutable: " + field.show)
425425
EnvMap.writeJoinVal(this, field, value)
426426
}
427427

428428
def initVar(field: Symbol, value: Value)(using Context, EnvMap.EnvMapMutableData) = log("Initialize " + field.show + " = " + value + " for " + this, printer) {
429-
assert(field.is(Flags.Mutable), "Field is not mutable: " + field.show)
429+
assert(field.is(Flags.Mutable, butNot = Flags.Param), "Field is not mutable: " + field.show)
430430
EnvMap.writeJoinVal(this, field, value)
431431
}
432432

@@ -527,20 +527,20 @@ class Objects(using Context @constructorOnly):
527527
_of(Map.empty, byNameParam, thisV, outerEnv)
528528

529529
def setLocalVal(x: Symbol, value: Value)(using scope: Scope, ctx: Context, heap: Heap.MutableData, envMap: EnvMap.EnvMapMutableData): Unit =
530-
assert(!x.isOneOf(Flags.Param | Flags.Mutable), "Only local immutable variable allowed")
530+
assert(x.is(Flags.Param) || !x.is(Flags.Mutable), "Only local immutable variable allowed")
531531
scope match
532532
case env: EnvRef =>
533533
env.initVal(x, value)
534534
case ref: Ref =>
535-
ref.initVal(x, value) // TODO: This is possible for match statement in class body. Report warning?
535+
ref.initVal(x, value) // This is possible for match statement in class body.
536536

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

545545
/**
546546
* Resolve the environment by searching for a given symbol.
@@ -986,15 +986,7 @@ class Objects(using Context @constructorOnly):
986986
// Assume such method is pure. Check return type, only try to analyze body if return type is not safe
987987
val target = resolve(v.typeSymbol.asClass, meth)
988988
val targetType = target.denot.info
989-
assert(targetType.isInstanceOf[ExprType] || targetType.isInstanceOf[MethodType],
990-
"Unexpected type! Receiver = " + v.show + ", meth = " + target + ", type = " + targetType)
991-
val returnType =
992-
if targetType.isInstanceOf[ExprType] then
993-
// corresponds to parameterless method like `def meth: ExprType[T]`
994-
// See pos/toDouble.scala
995-
targetType.asInstanceOf[ExprType].resType
996-
else
997-
targetType.asInstanceOf[MethodType].resType
989+
val returnType = targetType.finalResultType
998990
val typeSymbol = SafeValue.getSafeTypeSymbol(returnType)
999991
if typeSymbol.isDefined then
1000992
// since method is pure and return type is safe, no need to analyze method body
@@ -1326,7 +1318,8 @@ class Objects(using Context @constructorOnly):
13261318
report.warning("[Internal error] top-level class should have `Package` as outer, class = " + klass.show + ", outer = " + outer.show + ", " + Trace.show, Trace.position)
13271319
(Bottom, Env.NoEnv)
13281320
else
1329-
val outerCls = klass.owner.enclosingClass.asClass
1321+
// enclosingClass is specially handled for java static terms, so use `lexicallyEnclosingClass` here
1322+
val outerCls = klass.owner.lexicallyEnclosingClass.asClass
13301323
// When `klass` is directly nested in `outerCls`, `outerCls`.enclosingMethod returns its primary constructor
13311324
if klass.owner.enclosingMethod == outerCls.primaryConstructor then
13321325
(outer, Env.NoEnv)

tests/init-global/pos/enum.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
enum FileExtension(val toLowerCase: String):
2+
case Tasty extends FileExtension("tasty")
3+
case Betasty extends FileExtension("betasty")
4+
case Class extends FileExtension("class")
5+
case Jar extends FileExtension("jar")
6+
case Scala extends FileExtension("scala")
7+
case ScalaScript extends FileExtension("sc")
8+
case Java extends FileExtension("java")
9+
case Zip extends FileExtension("zip")
10+
case Inc extends FileExtension("inc")
11+
case Empty extends FileExtension("")
12+
13+
/** Fallback extension */
14+
case External(override val toLowerCase: String) extends FileExtension(toLowerCase)
15+
16+
object O:
17+
val a = FileExtension.Empty

0 commit comments

Comments
 (0)