Skip to content

Commit eea671f

Browse files
authored
Merge pull request #348 from scala/backport-lts-3.3-22938
Backport "Fix isGenericArrayElement for higher-kinded types" to 3.3 LTS
2 parents 736ba44 + 0ac0542 commit eea671f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

compiler/src/dotty/tools/dotc/core/TypeErasure.scala

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

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

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)