Skip to content

Commit f802abf

Browse files
authored
Merge pull request #3719 from dotty-staging/fix-#3634
Fix #3624: Lazily evaluate static module accessors
2 parents c307705 + 6d84f52 commit f802abf

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

compiler/src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
3636
class OffsetInfo(var defs: List[Tree], var ord:Int)
3737
val appendOffsetDefs = mutable.Map.empty[Symbol, OffsetInfo]
3838

39-
override def phaseName: String = "LazyVals"
39+
override def phaseName: String = "lazyVals"
4040

4141
/** List of names of phases that should have finished processing of tree
4242
* before this phase starts processing same tree */
@@ -61,7 +61,10 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
6161

6262
def transformLazyVal(tree: ValOrDefDef)(implicit ctx: Context): Tree = {
6363
val sym = tree.symbol
64-
if (!(sym is Flags.Lazy) || sym.owner.is(Flags.Trait) || (sym.isStatic && sym.is(Flags.Module))) tree
64+
if (!(sym is Flags.Lazy) ||
65+
sym.owner.is(Flags.Trait) || // val is accessor, lazy field will be implemented in subclass
66+
(sym.isStatic && sym.is(Flags.Module, butNot = Flags.Method))) // static module vals are implemented in the JVM by lazy loading
67+
tree
6568
else {
6669
val isField = sym.owner.isClass
6770
if (isField) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test extends App {
2+
3+
implicit def _1: Long = 1L
4+
implicit def _2: Int = 0
5+
6+
println(implicitly[AnyVal])
7+
}

tests/run/i3624.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait T {
2+
case object Foo
3+
}
4+
5+
object Bar extends T
6+
object Baz extends T
7+
8+
object Test {
9+
def main(args: Array[String]): Unit = {
10+
assert(Bar.Foo eq Bar.Foo)
11+
assert(Bar.Foo ne Baz.Foo)
12+
}
13+
}

0 commit comments

Comments
 (0)