Skip to content

Commit 7e49461

Browse files
committed
VCXPrototype: remove bound on type variable
This is needed so that Array[T] erases to Object, see the note in the code.
1 parent 7613a01 commit 7e49461

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

src/dotty/runtime/vc/VCPrototype.scala

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ abstract class VCFloatCasePrototype(underlying: Float) extends VCFloatPrototype(
2929
}
3030
}
3131

32-
abstract class VCFloatCompanion[T <: VCFloatPrototype] extends ClassTag[T] {
32+
// NOTE for all VCXCompanion: The type parameter T should be bounded like this:
33+
// abstract class VCXCompanion[T <: VCXPrototype] extends ClassTag[T]
34+
// But this affects erasure: it means that Array[T] is erased to [VCIntPrototype;
35+
// instead of Object, but we really need it to erase to Object if we want
36+
// VCXArray to be a valid array. We work around this by adding casts where
37+
// we need to assume that T is a subtype of VCXPrototype.
38+
39+
abstract class VCFloatCompanion[T /*<: VCFloatPrototype*/] extends ClassTag[T] {
3340
def box(underlying: Float): T
34-
final def unbox(boxed: T) = boxed.underlying
41+
final def unbox(boxed: T) = boxed.asInstanceOf[VCFloatPrototype].underlying
3542

3643
implicit def classTag: this.type = this
3744
override def newArray(len: Int): Array[T] =
38-
new VCFloatArray(this, len).asInstanceOf[Array[T]]
45+
new VCFloatArray(this.asInstanceOf[VCFloatCompanion[VCFloatPrototype]], len).asInstanceOf[Array[T]]
3946

4047

4148
final def _1$extension(underlying: Float) = underlying
@@ -80,13 +87,13 @@ abstract class VCObjectCasePrototype(underlying: Object) extends VCObjectPrototy
8087
}
8188
}
8289

83-
abstract class VCObjectCompanion[T <: VCObjectPrototype] extends ClassTag[T] {
90+
abstract class VCObjectCompanion[T /*<: VCObjectPrototype*/] extends ClassTag[T] {
8491
def box(underlying: Object): T
85-
final def unbox(boxed: T) = boxed.underlying
92+
final def unbox(boxed: T) = boxed.asInstanceOf[VCObjectPrototype].underlying
8693

8794
implicit def classTag: this.type = this
8895
override def newArray(len: Int): Array[T] =
89-
new VCObjectArray(this, len).asInstanceOf[Array[T]]
96+
new VCObjectArray(this.asInstanceOf[VCObjectCompanion[VCObjectPrototype]], len).asInstanceOf[Array[T]]
9097

9198

9299
final def _1$extension(underlying: Object) = underlying
@@ -133,13 +140,13 @@ abstract class VCShortCasePrototype(underlying: Short) extends VCShortPrototype(
133140
}
134141
}
135142

136-
abstract class VCShortCompanion[T <: VCShortPrototype] extends ClassTag[T] {
143+
abstract class VCShortCompanion[T /*<: VCShortPrototype*/] extends ClassTag[T] {
137144
def box(underlying: Short): T
138-
final def unbox(boxed: T) = boxed.underlying
145+
final def unbox(boxed: T) = boxed.asInstanceOf[VCShortPrototype].underlying
139146

140147
implicit def classTag: this.type = this
141148
override def newArray(len: Int): Array[T] =
142-
new VCShortArray(this, len).asInstanceOf[Array[T]]
149+
new VCShortArray(this.asInstanceOf[VCShortCompanion[VCShortPrototype]], len).asInstanceOf[Array[T]]
143150

144151

145152
final def _1$extension(underlying: Short) = underlying
@@ -187,13 +194,13 @@ abstract class VCLongCasePrototype(underlying: Long) extends VCLongPrototype(und
187194
}
188195
}
189196

190-
abstract class VCLongCompanion[T <: VCLongPrototype] extends ClassTag[T] {
197+
abstract class VCLongCompanion[T /*<: VCLongPrototype*/] extends ClassTag[T] {
191198
def box(underlying: Long): T
192-
final def unbox(boxed: T) = boxed.underlying
199+
final def unbox(boxed: T) = boxed.asInstanceOf[VCLongPrototype].underlying
193200

194201
implicit def classTag: this.type = this
195202
override def newArray(len: Int): Array[T] =
196-
new VCLongArray(this, len).asInstanceOf[Array[T]]
203+
new VCLongArray(this.asInstanceOf[VCLongCompanion[VCLongPrototype]], len).asInstanceOf[Array[T]]
197204

198205

199206
final def _1$extension(underlying: Long) = underlying
@@ -240,13 +247,13 @@ abstract class VCIntCasePrototype(underlying: Int) extends VCIntPrototype(underl
240247
}
241248
}
242249

243-
abstract class VCIntCompanion[T <: VCIntPrototype] extends ClassTag[T] {
250+
abstract class VCIntCompanion[T /*<: VCIntPrototype*/] extends ClassTag[T] {
244251
def box(underlying: Int): T
245-
final def unbox(boxed: T) = boxed.underlying
252+
final def unbox(boxed: T) = boxed.asInstanceOf[VCIntPrototype].underlying
246253

247254
implicit def classTag: this.type = this
248255
override def newArray(len: Int): Array[T] =
249-
new VCIntArray(this, len).asInstanceOf[Array[T]]
256+
new VCIntArray(this.asInstanceOf[VCIntCompanion[VCIntPrototype]], len).asInstanceOf[Array[T]]
250257

251258

252259
final def _1$extension(underlying: Int) = underlying
@@ -291,13 +298,13 @@ abstract class VCDoubleCasePrototype(underlying: Double) extends VCDoublePrototy
291298
}
292299
}
293300

294-
abstract class VCDoubleCompanion[T <: VCDoublePrototype] extends ClassTag[T] {
301+
abstract class VCDoubleCompanion[T /*<: VCDoublePrototype*/] extends ClassTag[T] {
295302
def box(underlying: Double): T
296-
final def unbox(boxed: T) = boxed.underlying
303+
final def unbox(boxed: T) = boxed.asInstanceOf[VCDoublePrototype].underlying
297304

298305
implicit def classTag: this.type = this
299306
override def newArray(len: Int): Array[T] =
300-
new VCDoubleArray(this, len).asInstanceOf[Array[T]]
307+
new VCDoubleArray(this.asInstanceOf[VCDoubleCompanion[VCDoublePrototype]], len).asInstanceOf[Array[T]]
301308

302309

303310
final def _1$extension(underlying: Double) = underlying
@@ -342,13 +349,13 @@ abstract class VCBooleanCasePrototype(underlying: Boolean) extends VCBooleanProt
342349
}
343350
}
344351

345-
abstract class VCBooleanCompanion[T <: VCBooleanPrototype] extends ClassTag[T] {
352+
abstract class VCBooleanCompanion[T /*<: VCBooleanPrototype*/] extends ClassTag[T] {
346353
def box(underlying: Boolean): T
347-
final def unbox(boxed: T) = boxed.underlying
354+
final def unbox(boxed: T) = boxed.asInstanceOf[VCBooleanPrototype].underlying
348355

349356
implicit def classTag: this.type = this
350357
override def newArray(len: Int): Array[T] =
351-
new VCBooleanArray(this, len).asInstanceOf[Array[T]]
358+
new VCBooleanArray(this.asInstanceOf[VCBooleanCompanion[VCBooleanPrototype]], len).asInstanceOf[Array[T]]
352359

353360

354361
final def _1$extension(underlying: Boolean) = underlying
@@ -397,13 +404,13 @@ abstract class VCCharCasePrototype(underlying: Char) extends VCCharPrototype(und
397404
// subclasses are expected to implement equals, productPrefix, and canEqual
398405
}
399406

400-
abstract class VCCharCompanion[T <: VCCharPrototype] extends ClassTag[T] {
407+
abstract class VCCharCompanion[T /*<: VCCharPrototype*/] extends ClassTag[T] {
401408
def box(underlying: Char): T
402-
final def unbox(boxed: T) = boxed.underlying
409+
final def unbox(boxed: T) = boxed.asInstanceOf[VCCharPrototype].underlying
403410

404411
implicit def classTag: this.type = this
405412
override def newArray(len: Int): Array[T] =
406-
new VCCharArray(this, len).asInstanceOf[Array[T]]
413+
new VCCharArray(this.asInstanceOf[VCCharCompanion[VCCharPrototype]], len).asInstanceOf[Array[T]]
407414

408415

409416
final def _1$extension(underlying: Char) = underlying
@@ -448,13 +455,13 @@ abstract class VCByteCasePrototype(underlying: Byte) extends VCBytePrototype(und
448455
}
449456
}
450457

451-
abstract class VCByteCompanion[T <: VCBytePrototype] extends ClassTag[T] {
458+
abstract class VCByteCompanion[T /*<: VCBytePrototype*/] extends ClassTag[T] {
452459
def box(underlying: Byte): T
453-
final def unbox(boxed: T) = boxed.underlying
460+
final def unbox(boxed: T) = boxed.asInstanceOf[VCBytePrototype].underlying
454461

455462
implicit def classTag: this.type = this
456463
override def newArray(len: Int): Array[T] =
457-
new VCByteArray(this, len).asInstanceOf[Array[T]]
464+
new VCByteArray(this.asInstanceOf[VCByteCompanion[VCBytePrototype]], len).asInstanceOf[Array[T]]
458465

459466

460467
final def _1$extension(underlying: Byte) = underlying

0 commit comments

Comments
 (0)