Skip to content

Commit

Permalink
Backport "Fix tuple casting" (scala#16197)
Browse files Browse the repository at this point in the history
Backports scala#16113
  • Loading branch information
Kordyjan authored Oct 17, 2022
2 parents efab3af + 235f08b commit 195a872
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3961,10 +3961,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
else tree
else if tree.tpe.derivesFrom(defn.PairClass) && !defn.isTupleNType(tree.tpe.widenDealias) then
// If this is a generic tuple we need to cast it to make the TupleN/ members accessible.
// This only works for generic tuples of know size up to 22.
defn.tupleTypes(tree.tpe.widenTermRefExpr, Definitions.MaxTupleArity) match
case Some(elems) => tree.cast(defn.tupleType(elems))
case None => tree
// This works only for generic tuples of known size up to 22.
defn.tupleTypes(tree.tpe.widenTermRefExpr) match
case Some(elems) if elems.length <= Definitions.MaxTupleArity =>
tree.cast(defn.tupleType(elems))
case _ => tree
else tree // other adaptations for selections are handled in typedSelect
case _ if ctx.mode.is(Mode.ImplicitsEnabled) && tree.tpe.isValueType =>
if pt.isRef(defn.AnyValClass, skipRefined = false)
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i16104.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
trait JsonVal

val value = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "k", "l", "m", "n", "o", "p", "q")
val tree: JsonVal = ???

def Case2 = {
sealed trait Write[T]
object WriteOf:
final inline def tuple[T <: Tuple]: Write[T] = ???

given EntryToJson[T]: scala.Conversion[T, JsonStructureEntry[T]] = ???
class JsonStructureEntry[T](t: T):
def writeAs[X >: T](using Write[X]): util.Try[JsonVal] = ???

value
.writeAs(using WriteOf.tuple[String *: String *: String *: String *: String *: String *: String *: String *: String *: String *: Int *: Int *: Int *: Int *: Int *: Int *: Int *: Int *: Int *: Int *: String *: String *: String *: String *: String *: String *: String *: EmptyTuple])
.fold(_ => ???, _ == tree)
}

0 comments on commit 195a872

Please sign in to comment.