From ea3835e4db798913cc20911ca2c4521e0228e3d5 Mon Sep 17 00:00:00 2001 From: aherlihy Date: Fri, 2 Feb 2024 17:28:49 +0100 Subject: [PATCH] Fix purity check for val inside of object Fixes #17317 --- compiler/src/dotty/tools/dotc/ast/TreeInfo.scala | 3 ++- tests/run/i17317-b.check | 1 + tests/run/i17317-b.scala | 12 ++++++++++++ tests/run/i17317.check | 1 + tests/run/i17317.scala | 12 ++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/run/i17317-b.check create mode 100644 tests/run/i17317-b.scala create mode 100644 tests/run/i17317.check create mode 100644 tests/run/i17317.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index 5ded0e1262e4..28d3ef6daaef 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -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 diff --git a/tests/run/i17317-b.check b/tests/run/i17317-b.check new file mode 100644 index 000000000000..3b18e512dba7 --- /dev/null +++ b/tests/run/i17317-b.check @@ -0,0 +1 @@ +hello world diff --git a/tests/run/i17317-b.scala b/tests/run/i17317-b.scala new file mode 100644 index 000000000000..b1606da6df20 --- /dev/null +++ b/tests/run/i17317-b.scala @@ -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 +} diff --git a/tests/run/i17317.check b/tests/run/i17317.check new file mode 100644 index 000000000000..3b18e512dba7 --- /dev/null +++ b/tests/run/i17317.check @@ -0,0 +1 @@ +hello world diff --git a/tests/run/i17317.scala b/tests/run/i17317.scala new file mode 100644 index 000000000000..41ea627c20b9 --- /dev/null +++ b/tests/run/i17317.scala @@ -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 +}