Skip to content

Commit b90a185

Browse files
committed
Fix isGenericArrayElement for higher-kinded types
A higher-kinded type `type A[T]` has to be treated just like an abstract type `type A`. Fixes #22888
1 parent 823782c commit b90a185

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/TypeErasure.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ object TypeErasure {
364364
case tp: MatchType =>
365365
val alts = tp.alternatives
366366
alts.nonEmpty && !fitsInJVMArray(alts.reduce(OrType(_, _, soft = true)))
367+
case tp @ AppliedType(tycon, _) if tycon.isLambdaSub =>
368+
!fitsInJVMArray(tp.translucentSuperType)
367369
case tp: TypeProxy =>
368370
isGenericArrayElement(tp.translucentSuperType, isScala2)
369371
case tp: AndType =>
@@ -781,11 +783,11 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
781783

782784
private def eraseArray(tp: Type)(using Context) = {
783785
val defn.ArrayOf(elemtp) = tp: @unchecked
784-
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
786+
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
785787
defn.ObjectType
786788
else if sourceLanguage.isScala2 && (elemtp.hiBound.isNullType || elemtp.hiBound.isNothingType) then
787789
JavaArrayType(defn.ObjectType)
788-
else
790+
else
789791
try erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp) match
790792
case _: WildcardType => WildcardType
791793
case elem => JavaArrayType(elem)

Diff for: tests/run/i22888.scala

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
trait Foo:
2+
type A[T]
3+
var arr: Array[A[Int]] = null
4+
5+
class Bar() extends Foo:
6+
type A[T] = Int
7+
8+
trait Foo2:
9+
type Dummy
10+
type A[T] <: Dummy
11+
var arr: Array[A[Int]] = null
12+
13+
class Bar2() extends Foo2:
14+
type Dummy = Any
15+
type A[T] = Int
16+
17+
trait Foo3:
18+
type A[T] <: Object
19+
var arr: Array[A[String]] = null
20+
21+
class Bar3() extends Foo3:
22+
type A[T] = String
23+
24+
object Test:
25+
def main(args: Array[String]) =
26+
val bar = new Bar()
27+
bar.arr = Array.ofDim[Int](1)
28+
bar.arr(0) = 123
29+
30+
val bar2 = new Bar2()
31+
bar2.arr = Array.ofDim[Int](1)
32+
bar2.arr(0) = 123
33+
34+
val bar3 = new Bar3()
35+
bar3.arr = Array.ofDim[String](1)
36+
bar3.arr(0) = "123"
37+

0 commit comments

Comments
 (0)