forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix two problems related to match types as array elements
1. The erasure of an array of matchtypes should sometimes be Object instead of Object[] 2. Classtags of matchtypes can be created only if all alternatives produce the same classtag. About 1: If a matchtype with alternative types A_1, ... A_n is an array element, it should be treated in the same way as the type ? <: A_1 | ... | A_n. It's an _unknown_ subtype of A_1 | ... | A_n. That can cause the erasure of the underlying array to be Object. Fixes scala#15618
- Loading branch information
1 parent
e13cf1d
commit 28823b7
Showing
6 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- Error: tests/neg/i15618.scala:17:44 --------------------------------------------------------------------------------- | ||
17 | def toArray: Array[ScalaType[T]] = Array() // error | ||
| ^ | ||
| No ClassTag available for ScalaType[T] | ||
| | ||
| where: T is a type in class Tensor with bounds <: DType | ||
| | ||
| | ||
| Note: a match type could not be fully reduced: | ||
| | ||
| trying to reduce ScalaType[T] | ||
| failed since selector T | ||
| does not match case Float16 => Float | ||
| and cannot be shown to be disjoint from it either. | ||
| Therefore, reduction cannot advance to the remaining cases | ||
| | ||
| case Float32 => Float | ||
| case Int32 => Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
sealed abstract class DType | ||
sealed class Float16 extends DType | ||
sealed class Float32 extends DType | ||
sealed class Int32 extends DType | ||
|
||
object Float16 extends Float16 | ||
object Float32 extends Float32 | ||
object Int32 extends Int32 | ||
|
||
type ScalaType[U <: DType] <: Int | Float = U match | ||
case Float16 => Float | ||
case Float32 => Float | ||
case Int32 => Int | ||
|
||
class Tensor[T <: DType](dtype: T): | ||
def toSeq: Seq[ScalaType[T]] = Seq() | ||
def toArray: Array[ScalaType[T]] = Array() // error | ||
|
||
@main | ||
def Test = | ||
val t = Tensor(Float32) // Tensor[Float32] | ||
println(t.toSeq.headOption) // works, Seq[Float] | ||
println(t.toArray.headOption) // ClassCastException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
None | ||
None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
sealed abstract class DType | ||
sealed class Float16 extends DType | ||
sealed class Float32 extends DType | ||
sealed class Int32 extends DType | ||
|
||
object Float16 extends Float16 | ||
object Float32 extends Float32 | ||
object Int32 extends Int32 | ||
|
||
type ScalaType[U <: DType] <: Int | Float = U match | ||
case Float16 => Float | ||
case Float32 => Float | ||
case Int32 => Int | ||
|
||
abstract class Tensor[T <: DType]: | ||
def toArray: Array[ScalaType[T]] | ||
|
||
object FloatTensor extends Tensor[Float16]: | ||
def toArray: Array[Float] = Array(1, 2, 3) | ||
|
||
@main | ||
def Test = | ||
val t = FloatTensor: Tensor[Float16] // Tensor[Float32] | ||
println(t.toArray.headOption) // was ClassCastException |