@@ -86,7 +86,7 @@ class TestBCode extends DottyBytecodeTest {
86
86
/** Make sure that creating multidim arrays reduces to "multinewarray"
87
87
* instruction
88
88
*/
89
- @ Test def multidimArrays = {
89
+ @ Test def multidimArraysFromOfDim = {
90
90
val source = """
91
91
|object Arr {
92
92
| def arr = Array.ofDim[Int](2, 1)
@@ -98,12 +98,91 @@ class TestBCode extends DottyBytecodeTest {
98
98
99
99
val hadCorrectInstr =
100
100
instructionsFromMethod(method)
101
- .collect { case x @ NewArray (op, _, dims) if op == 197 && dims == 2 => x }
101
+ .collect {
102
+ case x @ NewArray (op, _, dims)
103
+ if op == Opcode .multianewarray && dims == 2 => x
104
+ }
102
105
.length > 0
103
106
104
107
assert(hadCorrectInstr,
105
108
" Did not contain \" multianewarray\" instruction in:\n " +
106
109
instructionsFromMethod(method).mkString(" \n " ))
107
110
}
108
111
}
112
+
113
+ @ Test def arraysFromOfDim = {
114
+ val source = """
115
+ |object Arr {
116
+ | def arr1 = Array.ofDim[Int](2)
117
+ | def arr2 = Array.ofDim[Unit](2)
118
+ | def arr3 = Array.ofDim[String](2)
119
+ | def arr4 = Array.ofDim[Map[String, String]](2)
120
+ |}""" .stripMargin
121
+ checkBCode(source) { dir =>
122
+ val moduleIn = dir.lookupName(" Arr$.class" , directory = false )
123
+ val moduleNode = loadClassNode(moduleIn.input)
124
+ val arr1 = getMethod(moduleNode, " arr1" )
125
+ val arr2 = getMethod(moduleNode, " arr2" )
126
+ val arr3 = getMethod(moduleNode, " arr3" )
127
+
128
+ val arr1CorrectInstr =
129
+ instructionsFromMethod(arr1)
130
+ .collect {
131
+ case x @ IntOp (op, oprnd)
132
+ if op == Opcode .newarray && oprnd == Opcode .int => x
133
+ }
134
+ .length > 0
135
+
136
+ assert(arr1CorrectInstr,
137
+ " Did not contain \" multianewarray\" instruction in:\n " +
138
+ instructionsFromMethod(arr1).mkString(" \n " ))
139
+
140
+ val arr2CorrectInstr =
141
+ instructionsFromMethod(arr2)
142
+ .collect {
143
+ case x @ TypeOp (op, oprnd)
144
+ if op == Opcode .anewarray && oprnd == Opcode .boxedUnit => x
145
+ }
146
+ .length > 0
147
+
148
+ assert(arr2CorrectInstr,
149
+ " arr2 bytecode did not contain correct `anewarray` instruction:\n " +
150
+ instructionsFromMethod(arr2)mkString(" \n " ))
151
+
152
+ val arr3CorrectInstr =
153
+ instructionsFromMethod(arr3)
154
+ .collect {
155
+ case x @ TypeOp (op, oprnd)
156
+ if op == Opcode .anewarray && oprnd == Opcode .javaString => x
157
+ }
158
+ .length > 0
159
+
160
+ assert(arr3CorrectInstr,
161
+ " arr3 bytecode did not contain correct `anewarray` instruction:\n " +
162
+ instructionsFromMethod(arr3).mkString(" \n " ))
163
+ }
164
+ }
165
+
166
+ @ Test def arraysFromDimAndFromNewEqual = {
167
+ val source = """
168
+ |object Arr {
169
+ | def arr1 = Array.ofDim[Int](2)
170
+ | def arr2 = new Array[Int](2)
171
+ |}""" .stripMargin
172
+
173
+ checkBCode(source) { dir =>
174
+ val moduleIn = dir.lookupName(" Arr$.class" , directory = false )
175
+ val moduleNode = loadClassNode(moduleIn.input)
176
+ val arr1 = getMethod(moduleNode, " arr1" )
177
+ val arr2 = getMethod(moduleNode, " arr2" )
178
+
179
+ // First two instructions of `arr1` fetch the static reference to `Array`
180
+ val instructions1 = instructionsFromMethod(arr1).drop(2 )
181
+ val instructions2 = instructionsFromMethod(arr2)
182
+
183
+ assert(instructions1 == instructions2,
184
+ " Creating arrays using `Array.ofDim[Int](2)` did not equal bytecode for `new Array[Int](2)`\n " +
185
+ diffInstructions(instructions1, instructions2))
186
+ }
187
+ }
109
188
}
0 commit comments