Skip to content

Commit e46d9ab

Browse files
committed
Drop NoInits flag for given aliases that might become lazy vals
The current LazyVals implementation contains initialization code for the OFFSET value. Therefore we cannot set a NoInits flag for traits containing alias givens since these might become lay vals. If the lazy val implementation changes we might be able to revert that commit.
1 parent 2e12f19 commit e46d9ab

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,6 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
243243
case y => y
244244
}
245245

246-
/** The largest subset of {NoInits, PureInterface} that a
247-
* trait or class enclosing this statement can have as flags.
248-
*/
249-
def defKind(tree: Tree)(implicit ctx: Context): FlagSet = unsplice(tree) match {
250-
case EmptyTree | _: Import => NoInitsInterface
251-
case tree: TypeDef => if (tree.isClassDef) NoInits else NoInitsInterface
252-
case tree: DefDef =>
253-
if (tree.unforcedRhs == EmptyTree &&
254-
tree.vparamss.forall(_.forall(_.rhs.isEmpty))) NoInitsInterface
255-
else NoInits
256-
case tree: ValDef => if (tree.unforcedRhs == EmptyTree) NoInitsInterface else EmptyFlags
257-
case _ => EmptyFlags
258-
}
259-
260246
/** The largest subset of {NoInits, PureInterface} that a
261247
* trait or class with these parents can have as flags.
262248
*/
@@ -266,12 +252,6 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
266252
case _ :: parents1 => parentsKind(parents1)
267253
}
268254

269-
/** The largest subset of {NoInits, PureInterface} that a
270-
* trait or class with this body can have as flags.
271-
*/
272-
def bodyKind(body: List[Tree])(implicit ctx: Context): FlagSet =
273-
(NoInitsInterface /: body)((fs, stat) => fs & defKind(stat))
274-
275255
/** Checks whether predicate `p` is true for all result parts of this expression,
276256
* where we zoom into Ifs, Matches, and Blocks.
277257
*/
@@ -342,6 +322,28 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped]
342322
case _ => false
343323
}
344324

325+
/** The largest subset of {NoInits, PureInterface} that a
326+
* trait or class enclosing this statement can have as flags.
327+
*/
328+
def defKind(tree: Tree)(implicit ctx: Context): FlagSet = unsplice(tree) match {
329+
case EmptyTree | _: Import => NoInitsInterface
330+
case tree: TypeDef => if (tree.isClassDef) NoInits else NoInitsInterface
331+
case tree: DefDef =>
332+
if (tree.unforcedRhs == EmptyTree &&
333+
tree.vparamss.forall(_.forall(_.rhs.isEmpty))) NoInitsInterface
334+
else if (tree.mods.is(Given) && tree.tparams.isEmpty && tree.vparamss.isEmpty)
335+
EmptyFlags // might become a lazy val: TODO: check whether we need to suppress NoInits once we have new lazy val impl
336+
else NoInits
337+
case tree: ValDef => if (tree.unforcedRhs == EmptyTree) NoInitsInterface else EmptyFlags
338+
case _ => EmptyFlags
339+
}
340+
341+
/** The largest subset of {NoInits, PureInterface} that a
342+
* trait or class with this body can have as flags.
343+
*/
344+
def bodyKind(body: List[Tree])(implicit ctx: Context): FlagSet =
345+
(NoInitsInterface /: body)((fs, stat) => fs & defKind(stat))
346+
345347
// todo: fill with other methods from TreeInfo that only apply to untpd.Tree's
346348
}
347349

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ class Namer { typer: Typer =>
11751175
if (isDerivedValueClass(cls)) cls.setFlag(Final)
11761176
cls.info = avoidPrivateLeaks(cls)
11771177
cls.baseClasses.foreach(_.invalidateBaseTypeCache()) // we might have looked before and found nothing
1178-
cls.setNoInitsFlags(parentsKind(parents), bodyKind(rest))
1178+
cls.setNoInitsFlags(parentsKind(parents), untpd.bodyKind(rest))
11791179
if (cls.isNoInitsClass) cls.primaryConstructor.setFlag(StableRealizable)
11801180
processExports(localCtx)
11811181
}

0 commit comments

Comments
 (0)