Skip to content

Commit

Permalink
Make Array.apply an intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Sep 11, 2023
1 parent 1be790c commit 8bfc293
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/init-global/neg/mutable-array.scala
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8bfc293

Please sign in to comment.