Skip to content
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
3 changes: 3 additions & 0 deletions compiler/test/dotc/pos-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ i7087.scala

# Opaque type
i5720.scala

# Tuples
toexproftuple.scala
8 changes: 7 additions & 1 deletion library/src-bootstrapped/scala/quoted/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ package object quoted {
'{ Tuple.fromIArray(IArray(${seq.toExprOfSeq}: _*)) }
}
}
}

/** Given a tuple of the form `(Expr[A1], ..., Expr[An])`, outputs a tuple `Expr[(A1, ..., An)]`. */
def (tup: T) toExprOfTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type] given (ctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
import ctx.tasty._
tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[_]]].toExprOfTuple
.cast[Tuple.InverseMap[T, Expr]]
}
}
}
13 changes: 13 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ object Tuple {
case _ => Tuple
}

/** Converts a tuple `(F[T1], ..., F[Tn])` to `(T1, ... Tn)` */
type InverseMap[X <: Tuple, F[_]] <: Tuple = X match {
case F[x] *: t => x *: InverseMap[t, F]
case Unit => Unit
}

/** Implicit evidence. IsMappedBy[F][X] is present in the implicit scope iff
* X is a tuple for which each element's type is constructed via `F`. E.g.
* (F[A1], ..., F[An]), but not `(F[A1], B2, ..., F[An])` where B2 does not
* have the shape of `F[A]`.
*/
type IsMappedBy[F[_]] = [X <: Tuple] =>> X =:= Map[InverseMap[X, F], F]

/** Convert an array into a tuple of unknown arity and types */
def fromArray[T](xs: Array[T]): Tuple = {
val xs2 = xs match {
Expand Down
11 changes: 11 additions & 0 deletions tests/neg/toexproftuple.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted._, scala.deriving._
import given scala.quoted._

inline def mcr: Any = ${mcrImpl}
def mcrImpl given (ctx: QuoteContext): Expr[Any] = {
val tpl: (Expr[1], Expr[2], Expr[3]) = ('{1}, '{2}, '{3})
'{val res: (1, 3, 3) = ${tpl.toExprOfTuple}; res} // error

val tpl2: (Expr[1], 2, Expr[3]) = ('{1}, 2, '{3})
'{val res = ${tpl2.toExprOfTuple}; res} // error
}
5 changes: 5 additions & 0 deletions tests/neg/tuple-isMappedBy-2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.Tuple.IsMappedBy

object Test2 {
def test0[F[_], T: IsMappedBy[F]]: Unit = () // error: Type argument T does not conform to upper bound Tuple
}
13 changes: 13 additions & 0 deletions tests/neg/tuple-isMappedBy.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.Tuple.IsMappedBy

object Test1 {

def test[F[_], T <: Tuple: IsMappedBy[F]]: Unit = ()

test[List, (List[Int], Char)] // error
test[List, (List[Int], Seq[Char])] // error
test[Seq, (List[Int], Seq[Char])] // error
test[List, Tuple] // error
test[List, Nothing] // error

}
8 changes: 8 additions & 0 deletions tests/pos/toexproftuple.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._, scala.deriving._
import given scala.quoted._

inline def mcr: Any = ${mcrImpl}
def mcrImpl given (ctx: QuoteContext): Expr[Any] = {
val tpl: (Expr[1], Expr[2], Expr[3]) = ('{1}, '{2}, '{3})
'{val res: (1, 2, 3) = ${tpl.toExprOfTuple}; res}
}
27 changes: 27 additions & 0 deletions tests/pos/tuple-isMappedBy.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import scala.Tuple.IsMappedBy

object Test {

def test[F[_], T <: Tuple: IsMappedBy[F]]: Unit = ()

test[[X] =>> X, Unit]
test[[X] =>> X, (Int, Long)]

test[List, Unit]
test[List, (List[Int], List[Long])]

trait A[+X]
trait B[-X]
trait C[X]

test[A, Unit]
test[A, (A[Int], A[Long])]

test[B, Unit]
test[B, (B[Int], B[Long])]

test[C, Unit]
test[C, (C[Int], C[Long])]

}