Skip to content

Fix isGenericArrayElement for higher-kinded types #22938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ object TypeErasure {
case tp: MatchType =>
val alts = tp.alternatives
alts.nonEmpty && !fitsInJVMArray(alts.reduce(OrType(_, _, soft = true)))
case tp @ AppliedType(tycon, _) if tycon.isLambdaSub =>
!fitsInJVMArray(tp.translucentSuperType)
case tp: TypeProxy =>
isGenericArrayElement(tp.translucentSuperType, isScala2)
case tp: AndType =>
Expand Down Expand Up @@ -781,11 +783,11 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst

private def eraseArray(tp: Type)(using Context) = {
val defn.ArrayOf(elemtp) = tp: @unchecked
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
if isGenericArrayElement(elemtp, isScala2 = sourceLanguage.isScala2) then
defn.ObjectType
else if sourceLanguage.isScala2 && (elemtp.hiBound.isNullType || elemtp.hiBound.isNothingType) then
JavaArrayType(defn.ObjectType)
else
else
try erasureFn(sourceLanguage, semiEraseVCs = false, isConstructor, isSymbol, inSigName)(elemtp) match
case _: WildcardType => WildcardType
case elem => JavaArrayType(elem)
Expand Down
37 changes: 37 additions & 0 deletions tests/run/i22888.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
trait Foo:
type A[T]
var arr: Array[A[Int]] = null

class Bar() extends Foo:
type A[T] = Int

trait Foo2:
type Dummy
type A[T] <: Dummy
var arr: Array[A[Int]] = null

class Bar2() extends Foo2:
type Dummy = Any
type A[T] = Int

trait Foo3:
type A[T] <: Object
var arr: Array[A[String]] = null

class Bar3() extends Foo3:
type A[T] = String

object Test:
def main(args: Array[String]) =
val bar = new Bar()
bar.arr = Array.ofDim[Int](1)
bar.arr(0) = 123

val bar2 = new Bar2()
bar2.arr = Array.ofDim[Int](1)
bar2.arr(0) = 123

val bar3 = new Bar3()
bar3.arr = Array.ofDim[String](1)
bar3.arr(0) = "123"

Loading