From e5fc8c819f82c4404ca21b16a48bc19e9a8a80a1 Mon Sep 17 00:00:00 2001 From: David Hua Date: Tue, 7 Nov 2023 01:53:39 +0100 Subject: [PATCH 1/4] Fix i18624 and add test case for it --- .../dotty/tools/dotc/transform/init/Objects.scala | 14 +++++++++++--- tests/init-global/pos/i18624.scala | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/init-global/pos/i18624.scala diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 836218f302bc..b28fe6ce1a76 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -698,13 +698,21 @@ object Objects: case Fun(code, thisV, klass, env) => // meth == NoSymbol for poly functions - if meth.name.toString == "tupled" then + if meth.name == nme.tupled then value // a call like `fun.tupled` else code match case ddef: DefDef => - given Env.Data = Env.of(ddef, args.map(_.value), env) - extendTrace(code) { eval(ddef.rhs, thisV, klass, cacheResult = true) } + if meth.name == nme.apply then + given Env.Data = Env.of(ddef, args.map(_.value), env) + extendTrace(code) { eval(ddef.rhs, thisV, klass, cacheResult = true) } + else + meth.owner.asType.name match + case tpnme.Any | tpnme.AnyRef => + value + case _ => + Cold + end if case _ => // by-name closure diff --git a/tests/init-global/pos/i18624.scala b/tests/init-global/pos/i18624.scala new file mode 100644 index 000000000000..f2562d2da61b --- /dev/null +++ b/tests/init-global/pos/i18624.scala @@ -0,0 +1,7 @@ +def h(a: Int): Unit = { + +} + +object X { + println(h.getClass()) +} From 1ab7ddbcb69127e65b4f090a0494e6699fec3225 Mon Sep 17 00:00:00 2001 From: q-ata <24601033+q-ata@users.noreply.github.com> Date: Mon, 13 Nov 2023 01:29:37 +0100 Subject: [PATCH 2/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Lhoták --- compiler/src/dotty/tools/dotc/transform/init/Objects.scala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index b28fe6ce1a76..d265cb25216d 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -708,9 +708,12 @@ object Objects: extendTrace(code) { eval(ddef.rhs, thisV, klass, cacheResult = true) } else meth.owner.asType.name match + // The methods defined in `Any` and `AnyRef` are trivial and don't affect initialization. case tpnme.Any | tpnme.AnyRef => value case _ => + // In future, we will have Tasty for stdlib classes and can abstractly interpret that Tasty. + // For now, return `Cold` to ensure soundness and trigger a warning. Cold end if From 2cf07b38da17a7af83545727722974010a80bffd Mon Sep 17 00:00:00 2001 From: David Hua Date: Mon, 13 Nov 2023 01:57:32 +0100 Subject: [PATCH 3/4] Compare owner instead of owner name --- compiler/src/dotty/tools/dotc/transform/init/Objects.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index d265cb25216d..09572d47bca2 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -707,14 +707,14 @@ object Objects: given Env.Data = Env.of(ddef, args.map(_.value), env) extendTrace(code) { eval(ddef.rhs, thisV, klass, cacheResult = true) } else - meth.owner.asType.name match // The methods defined in `Any` and `AnyRef` are trivial and don't affect initialization. - case tpnme.Any | tpnme.AnyRef => + if meth.owner == defn.AnyClass || meth.owner == defn.ObjectClass then value - case _ => + else // In future, we will have Tasty for stdlib classes and can abstractly interpret that Tasty. // For now, return `Cold` to ensure soundness and trigger a warning. Cold + end if end if case _ => From 2e2b2236e50eb172067a6ccb1a5377d0bd39a6dd Mon Sep 17 00:00:00 2001 From: David Hua Date: Mon, 13 Nov 2023 02:00:19 +0100 Subject: [PATCH 4/4] Trigger the AnyRef case in test --- tests/init-global/pos/i18624.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/init-global/pos/i18624.scala b/tests/init-global/pos/i18624.scala index f2562d2da61b..1d59a39e9b74 100644 --- a/tests/init-global/pos/i18624.scala +++ b/tests/init-global/pos/i18624.scala @@ -3,5 +3,6 @@ def h(a: Int): Unit = { } object X { + h.notify() println(h.getClass()) }