Skip to content

Fix #3624: Lazily evaluate static module accessors #3719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
class OffsetInfo(var defs: List[Tree], var ord:Int)
val appendOffsetDefs = mutable.Map.empty[Symbol, OffsetInfo]

override def phaseName: String = "LazyVals"
override def phaseName: String = "lazyVals"

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

def transformLazyVal(tree: ValOrDefDef)(implicit ctx: Context): Tree = {
val sym = tree.symbol
if (!(sym is Flags.Lazy) || sym.owner.is(Flags.Trait) || (sym.isStatic && sym.is(Flags.Module))) tree
if (!(sym is Flags.Lazy) ||
sym.owner.is(Flags.Trait) ||
(sym.isStatic && sym.is(Flags.Module, butNot = Flags.Method)))
tree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment here explaining why this is OK in these cases?

else {
val isField = sym.owner.isClass
if (isField) {
Expand Down
11 changes: 11 additions & 0 deletions tests/run/i3634.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait T {
case object Foo
}

object Bar extends T

object Test {
def main(args: Array[String]): Unit = {
assert(Bar.Foo == Bar.Foo) // false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use eq instead of == to be extra sure that nothing funny is going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a object Baz extends T and assert(Bar.Foo ne Baz.Foo)

}
}