diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 836218f302bc..09572d47bca2 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -698,13 +698,24 @@ 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 + // The methods defined in `Any` and `AnyRef` are trivial and don't affect initialization. + if meth.owner == defn.AnyClass || meth.owner == defn.ObjectClass then + value + 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 _ => // 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..1d59a39e9b74 --- /dev/null +++ b/tests/init-global/pos/i18624.scala @@ -0,0 +1,8 @@ +def h(a: Int): Unit = { + +} + +object X { + h.notify() + println(h.getClass()) +}