diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 3d01b6a93b08..c29fa3fb2c81 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -616,7 +616,7 @@ object Objects: * @param superType The type of the super in a super call. NoType for non-super calls. * @param needResolve Whether the target of the call needs resolution? */ - def call(value: Value, meth: Symbol, args: List[ArgInfo], receiver: Type, superType: Type, needResolve: Boolean = true): Contextual[Value] = log("call " + meth.show + ", args = " + args.map(_.value.show), printer, (_: Value).show) { + def call(value: Value, meth: Symbol, args: List[ArgInfo], receiver: Type, superType: Type, needResolve: Boolean = true): Contextual[Value] = log("call " + meth.show + ", this = " + value.show + ", args = " + args.map(_.value.show), printer, (_: Value).show) { value match case Cold => report.warning("Using cold alias. Calling trace:\n" + Trace.show, Trace.position) @@ -658,7 +658,11 @@ object Objects: resolve(ref.klass, meth) if target.isOneOf(Flags.Method) then - if target.hasSource then + if target.owner == defn.ArrayModuleClass && target.name == nme.apply then + val arr = OfArray(State.currentObject, summon[Regions.Data]) + Heap.write(arr.addr, args.map(_.value).join) + arr + else if target.hasSource then val cls = target.owner.enclosingClass.asClass val ddef = target.defTree.asInstanceOf[DefDef] val meth = ddef.symbol diff --git a/tests/init-global/neg/mutable-array.scala b/tests/init-global/neg/mutable-array.scala new file mode 100644 index 000000000000..3cbc23a5e127 --- /dev/null +++ b/tests/init-global/neg/mutable-array.scala @@ -0,0 +1,8 @@ +object A: + class Box(var value: Int) + val box: Box = new Box(0) + +object B: + val boxes: Array[A.Box] = Array(A.box) + val box: A.Box = boxes(0) + val x: Int = box.value // error