Skip to content

Commit 195a872

Browse files
authored
Backport "Fix tuple casting" (#16197)
Backports #16113
2 parents efab3af + 235f08b commit 195a872

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -3961,10 +3961,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39613961
else tree
39623962
else if tree.tpe.derivesFrom(defn.PairClass) && !defn.isTupleNType(tree.tpe.widenDealias) then
39633963
// If this is a generic tuple we need to cast it to make the TupleN/ members accessible.
3964-
// This only works for generic tuples of know size up to 22.
3965-
defn.tupleTypes(tree.tpe.widenTermRefExpr, Definitions.MaxTupleArity) match
3966-
case Some(elems) => tree.cast(defn.tupleType(elems))
3967-
case None => tree
3964+
// This works only for generic tuples of known size up to 22.
3965+
defn.tupleTypes(tree.tpe.widenTermRefExpr) match
3966+
case Some(elems) if elems.length <= Definitions.MaxTupleArity =>
3967+
tree.cast(defn.tupleType(elems))
3968+
case _ => tree
39683969
else tree // other adaptations for selections are handled in typedSelect
39693970
case _ if ctx.mode.is(Mode.ImplicitsEnabled) && tree.tpe.isValueType =>
39703971
if pt.isRef(defn.AnyValClass, skipRefined = false)

tests/pos/i16104.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait JsonVal
2+
3+
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")
4+
val tree: JsonVal = ???
5+
6+
def Case2 = {
7+
sealed trait Write[T]
8+
object WriteOf:
9+
final inline def tuple[T <: Tuple]: Write[T] = ???
10+
11+
given EntryToJson[T]: scala.Conversion[T, JsonStructureEntry[T]] = ???
12+
class JsonStructureEntry[T](t: T):
13+
def writeAs[X >: T](using Write[X]): util.Try[JsonVal] = ???
14+
15+
value
16+
.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])
17+
.fold(_ => ???, _ == tree)
18+
}

0 commit comments

Comments
 (0)