Skip to content

Commit e00c826

Browse files
committed
Fix Ycheck: allow assigning fields in static constructors.
1 parent 74b118a commit e00c826

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ object NameOps {
6363
(if (name.isTermName) n.toTermName else n.toTypeName).asInstanceOf[N]
6464

6565
def isConstructorName = name == CONSTRUCTOR || name == TRAIT_CONSTRUCTOR
66+
def isStaticConstructorName = name == STATIC_CONSTRUCTOR
6667
def isExceptionResultName = name startsWith EXCEPTION_RESULT_PREFIX
6768
def isImplClassName = name endsWith IMPL_CLASS_SUFFIX
6869
def isLocalDummyName = name startsWith LOCALDUMMY_PREFIX

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ object SymDenotations {
594594
final def isPrimaryConstructor(implicit ctx: Context) =
595595
isConstructor && owner.primaryConstructor == symbol
596596

597+
/** Does this symbol denote the primary constructor of its enclosing class? */
598+
final def isStaticConstructor(implicit ctx: Context) =
599+
name.isStaticConstructorName
600+
597601
/** Is this a subclass of the given class `base`? */
598602
def isSubClass(base: Symbol)(implicit ctx: Context) = false
599603

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
476476
def canAssign(sym: Symbol) = // allow assignments from the primary constructor to class fields
477477
sym.is(Mutable, butNot = Accessor) ||
478478
ctx.owner.isPrimaryConstructor && !sym.is(Method) && sym.owner == ctx.owner.owner ||
479-
ctx.owner.name.isTraitSetterName
479+
ctx.owner.name.isTraitSetterName || ctx.owner.isStaticConstructor
480480
lhsCore.tpe match {
481481
case ref: TermRef if canAssign(ref.symbol) =>
482482
assignType(cpy.Assign(tree)(lhs1, typed(tree.rhs, ref.info)))

0 commit comments

Comments
 (0)