File tree Expand file tree Collapse file tree 6 files changed +91
-4
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 6 files changed +91
-4
lines changed Original file line number Diff line number Diff line change @@ -328,6 +328,9 @@ object TypeErasure {
328328 isGenericArrayElement(tp.alias, isScala2)
329329 case tp : TypeBounds =>
330330 ! fitsInJVMArray(tp.hi)
331+ case tp : MatchType =>
332+ val alts = tp.alternatives
333+ alts.nonEmpty && ! fitsInJVMArray(alts.reduce(OrType (_, _, soft = true )))
331334 case tp : TypeProxy =>
332335 isGenericArrayElement(tp.translucentSuperType, isScala2)
333336 case tp : AndType =>
Original file line number Diff line number Diff line change @@ -40,11 +40,28 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
4040 val classTag = ref(defn.ClassTagModule )
4141 val tag =
4242 if defn.SpecialClassTagClasses .contains(sym) then
43- classTag.select(sym.name.toTermName)
43+ classTag.select(sym.name.toTermName).withSpan(span)
4444 else
45- val clsOfType = escapeJavaArray(erasure(tp))
46- classTag.select(nme.apply).appliedToType(tp).appliedTo(clsOf(clsOfType))
47- withNoErrors(tag.withSpan(span))
45+ def clsOfType (tp : Type ): Type =
46+ val tp1 = tp.dealias
47+ if tp1.isMatch then
48+ val matchTp = tp1.underlyingIterator.collect {
49+ case mt : MatchType => mt
50+ }.next
51+ matchTp.alternatives.map(clsOfType) match
52+ case ct1 :: cts if cts.forall(ct1 == _) => ct1
53+ case _ => NoType
54+ else
55+ escapeJavaArray(erasure(tp))
56+ val ctype = clsOfType(tp)
57+ if ctype.exists then
58+ classTag.select(nme.apply)
59+ .appliedToType(tp)
60+ .appliedTo(clsOf(ctype))
61+ .withSpan(span)
62+ else
63+ EmptyTree
64+ withNoErrors(tag)
4865 case tp => EmptyTreeNoError
4966 else EmptyTreeNoError
5067 case _ => EmptyTreeNoError
Original file line number Diff line number Diff line change 1+ -- Error: tests/neg/i15618.scala:17:44 ---------------------------------------------------------------------------------
2+ 17 | def toArray: Array[ScalaType[T]] = Array() // error
3+ | ^
4+ | No ClassTag available for ScalaType[T]
5+ |
6+ | where: T is a type in class Tensor with bounds <: DType
7+ |
8+ |
9+ | Note: a match type could not be fully reduced:
10+ |
11+ | trying to reduce ScalaType[T]
12+ | failed since selector T
13+ | does not match case Float16 => Float
14+ | and cannot be shown to be disjoint from it either.
15+ | Therefore, reduction cannot advance to the remaining cases
16+ |
17+ | case Float32 => Float
18+ | case Int32 => Int
Original file line number Diff line number Diff line change 1+ sealed abstract class DType
2+ sealed class Float16 extends DType
3+ sealed class Float32 extends DType
4+ sealed class Int32 extends DType
5+
6+ object Float16 extends Float16
7+ object Float32 extends Float32
8+ object Int32 extends Int32
9+
10+ type ScalaType [U <: DType ] <: Int | Float = U match
11+ case Float16 => Float
12+ case Float32 => Float
13+ case Int32 => Int
14+
15+ class Tensor [T <: DType ](dtype : T ):
16+ def toSeq : Seq [ScalaType [T ]] = Seq ()
17+ def toArray : Array [ScalaType [T ]] = Array () // error
18+
19+ @ main
20+ def Test =
21+ val t = Tensor (Float32 ) // Tensor[Float32]
22+ println(t.toSeq.headOption) // works, Seq[Float]
23+ println(t.toArray.headOption) // ClassCastException
Original file line number Diff line number Diff line change 1+ None
2+ None
Original file line number Diff line number Diff line change 1+ sealed abstract class DType
2+ sealed class Float16 extends DType
3+ sealed class Float32 extends DType
4+ sealed class Int32 extends DType
5+
6+ object Float16 extends Float16
7+ object Float32 extends Float32
8+ object Int32 extends Int32
9+
10+ type ScalaType [U <: DType ] <: Int | Float = U match
11+ case Float16 => Float
12+ case Float32 => Float
13+ case Int32 => Int
14+
15+ abstract class Tensor [T <: DType ]:
16+ def toArray : Array [ScalaType [T ]]
17+
18+ object FloatTensor extends Tensor [Float16 ]:
19+ def toArray : Array [Float ] = Array (1 , 2 , 3 )
20+
21+ @ main
22+ def Test =
23+ val t = FloatTensor : Tensor [Float16 ] // Tensor[Float32]
24+ println(t.toArray.headOption) // was ClassCastException
You can’t perform that action at this time.
0 commit comments