Skip to content

Commit 5a5ea06

Browse files
authored
Fix purity check for val inside of object (#19598)
Fixes #17317
2 parents 9bf429d + ea3835e commit 5a5ea06

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,9 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
578578
sym.owner.isPrimitiveValueClass
579579
|| sym.owner == defn.StringClass
580580
|| defn.pureMethods.contains(sym)
581+
581582
tree.tpe.isInstanceOf[ConstantType] && tree.symbol != NoSymbol && isKnownPureOp(tree.symbol) // A constant expression with pure arguments is pure.
582-
|| fn.symbol.isStableMember && !fn.symbol.is(Lazy) // constructors of no-inits classes are stable
583+
|| fn.symbol.isStableMember && fn.symbol.isConstructor // constructors of no-inits classes are stable
583584

584585
/** The purity level of this reference.
585586
* @return

tests/run/i17317-b.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

tests/run/i17317-b.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object foo {
2+
object HelloGen {
3+
println("hello world")
4+
}
5+
val Hello = HelloGen
6+
}
7+
8+
import foo.Hello
9+
10+
object Test {
11+
def main(args: Array[String]): Unit = Hello: Unit
12+
}

tests/run/i17317.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world

tests/run/i17317.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package object foo {
2+
object HelloGen {
3+
println("hello world")
4+
}
5+
val Hello = HelloGen
6+
}
7+
8+
import foo.Hello
9+
10+
object Test {
11+
def main(args: Array[String]): Unit = Hello: Unit
12+
}

0 commit comments

Comments
 (0)