Skip to content

Fix tuple casting #16113

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

Merged
merged 1 commit into from
Oct 3, 2022
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
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 @@ -3943,10 +3943,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)
}