Skip to content

Commit

Permalink
Fix purity check for val inside of object
Browse files Browse the repository at this point in the history
Fixes #17317
  • Loading branch information
aherlihy committed Feb 2, 2024
1 parent 95266f2 commit ea3835e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,9 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
sym.owner.isPrimitiveValueClass
|| sym.owner == defn.StringClass
|| defn.pureMethods.contains(sym)

tree.tpe.isInstanceOf[ConstantType] && tree.symbol != NoSymbol && isKnownPureOp(tree.symbol) // A constant expression with pure arguments is pure.
|| fn.symbol.isStableMember && !fn.symbol.is(Lazy) // constructors of no-inits classes are stable
|| fn.symbol.isStableMember && fn.symbol.isConstructor // constructors of no-inits classes are stable

/** The purity level of this reference.
* @return
Expand Down
1 change: 1 addition & 0 deletions tests/run/i17317-b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
12 changes: 12 additions & 0 deletions tests/run/i17317-b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object foo {
object HelloGen {
println("hello world")
}
val Hello = HelloGen
}

import foo.Hello

object Test {
def main(args: Array[String]): Unit = Hello: Unit
}
1 change: 1 addition & 0 deletions tests/run/i17317.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
12 changes: 12 additions & 0 deletions tests/run/i17317.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package object foo {
object HelloGen {
println("hello world")
}
val Hello = HelloGen
}

import foo.Hello

object Test {
def main(args: Array[String]): Unit = Hello: Unit
}

0 comments on commit ea3835e

Please sign in to comment.