Skip to content

Commit c11d852

Browse files
committed
Workaroud #1856: recursively calling a lazy val works differently in Dotty
1 parent e063066 commit c11d852

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,17 @@ class Definitions {
319319
def staticsMethodRef(name: PreName) = ScalaStaticsModule.requiredMethodRef(name)
320320
def staticsMethod(name: PreName) = ScalaStaticsModule.requiredMethod(name)
321321

322-
lazy val DottyPredefModuleRef = ctx.requiredModuleRef("dotty.DottyPredef")
322+
// Dotty deviation: we cannot use a lazy val here because lazy vals in dotty
323+
// will return "null" when called recursively, see #1856.
324+
def DottyPredefModuleRef = {
325+
if (_DottyPredefModuleRef == null) {
326+
_DottyPredefModuleRef = ctx.requiredModuleRef("dotty.DottyPredef")
327+
assert(_DottyPredefModuleRef != null)
328+
}
329+
_DottyPredefModuleRef
330+
}
331+
private[this] var _DottyPredefModuleRef: TermRef = _
332+
323333
def DottyPredefModule(implicit ctx: Context) = DottyPredefModuleRef.symbol
324334

325335
def Predef_eqAny(implicit ctx: Context) = DottyPredefModule.requiredMethod(nme.eqAny)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ object ImportInfo {
3030
class ImportInfo(symf: => Symbol, val selectors: List[untpd.Tree],
3131
symNameOpt: Option[TermName], val isRootImport: Boolean = false)(implicit ctx: Context) {
3232

33-
lazy val sym = symf
33+
// Dotty deviation: we cannot use a lazy val here for the same reason
34+
// that we cannot use one for `DottyPredefModuleRef`.
35+
def sym = {
36+
if (_sym == null) {
37+
_sym = symf
38+
assert(_sym != null)
39+
}
40+
_sym
41+
}
42+
private[this] var _sym: Symbol = _
3443

3544
/** The (TermRef) type of the qualifier of the import clause */
3645
def site(implicit ctx: Context): Type = {

0 commit comments

Comments
 (0)