Skip to content
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

Fix check whether classtag can be generated for match types #16708

Merged
merged 1 commit into from
Jan 23, 2023
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
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,9 @@ object TypeErasure {
case _: ClassInfo => true
case _ => false
}
case tp: TypeParamRef => false
case tp: TypeBounds => false
case _: TypeParamRef => false
case _: TypeBounds => false
case _: MatchType => false
case tp: TypeProxy => hasStableErasure(tp.translucentSuperType)
case tp: AndType => hasStableErasure(tp.tp1) && hasStableErasure(tp.tp2)
case tp: OrType => hasStableErasure(tp.tp1) && hasStableErasure(tp.tp2)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ object Types {
* @param relaxedCheck if true type `Null` becomes a subtype of non-primitive value types in TypeComparer.
* @param matchLoosely if true the types `=> T` and `()T` are seen as overriding each other.
* @param checkClassInfo if true we check that ClassInfos are within bounds of abstract types
*
*
* @param isSubType a function used for checking subtype relationships.
*/
final def overrides(that: Type, relaxedCheck: Boolean, matchLoosely: => Boolean, checkClassInfo: Boolean = true,
Expand Down
9 changes: 1 addition & 8 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
if defn.SpecialClassTagClasses.contains(sym) then
classTagModul.select(sym.name.toTermName).withSpan(span)
else
def clsOfType(tp: Type): Type = tp.dealias.underlyingMatchType match
case matchTp: MatchType =>
matchTp.alternatives.map(clsOfType) match
case ct1 :: cts if cts.forall(ct1 == _) => ct1
case _ => NoType
case _ =>
escapeJavaArray(erasure(tp))
val ctype = clsOfType(tp)
val ctype = escapeJavaArray(erasure(tp))
if ctype.exists then
classTagModul.select(nme.apply)
.appliedToType(tp)
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i15618.check
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
|
| case Float32 => Float
| case Int32 => Int
-- [E172] Type Error: tests/neg/i15618.scala:21:33 ---------------------------------------------------------------------
21 | def toArray: Array[T] = Array() // error
| ^
| No ClassTag available for T
|
| where: T is a type in class Tensor2 with bounds <: Int | Float
8 changes: 8 additions & 0 deletions tests/neg/i15618.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ class Tensor[T <: DType](dtype: T):
def toSeq: Seq[ScalaType[T]] = Seq()
def toArray: Array[ScalaType[T]] = Array() // error

class Tensor2[T <: Int | Float](dtype: T):
def toSeq: Seq[T] = Seq()
def toArray: Array[T] = Array() // error

@main
def Test =
val t = Tensor(Float32) // Tensor[Float32]
println(t.toSeq.headOption) // works, Seq[Float]
println(t.toArray.headOption) // ClassCastException

val t2 = Tensor2(0.0f) // Tensor2[Float]
println(t.toSeq.headOption)
println(t.toArray.headOption)
17 changes: 17 additions & 0 deletions tests/pos/i16706.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.deriving.Mirror
import scala.reflect.ClassTag

type TupleUnionLub[T <: Tuple, Lub, Acc <: Lub] <: Lub = T match {
case (h & Lub) *: t => TupleUnionLub[t, Lub, Acc | h]
case EmptyTuple => Acc
}

transparent inline given derived[A](
using m: Mirror.SumOf[A],
idClassTag: ClassTag[TupleUnionLub[m.MirroredElemTypes, A, Nothing]]
): Unit = ()

sealed trait Foo
case class FooA(a: Int) extends Foo

val instance = derived[Foo] // error
11 changes: 11 additions & 0 deletions tests/pos/i16707.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.deriving.Mirror
import scala.reflect.ClassTag

transparent inline given derived[A](
using m: Mirror.ProductOf[A],
idClassTag: ClassTag[Tuple.Union[m.MirroredElemTypes]]
): Unit = ???

case class Foo(a: Int)

val instance = derived[Foo] // error