Skip to content

Commit

Permalink
Harden NamedTuple handling against ill-formed NamedTuples
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Dec 5, 2023
1 parent fea5fe1 commit 97757f7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/TypeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class TypeUtils {
case defn.NamedTuple(nmes, vals) =>
val names = nmes.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil).map:
case ConstantType(Constant(str: String)) => str.toTermName
case t => throw TypeError(em"Malformed NamedTuple: names must be string types, but $t was found.")
val values = vals.tupleElementTypesUpTo(bound, normalize).getOrElse(Nil)
names.zip(values)
case t =>
Expand Down
7 changes: 7 additions & 0 deletions tests/neg/named-tuples-3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import language.experimental.namedTuples

def f: NamedTuple.NamedTuple[(Int, Any), (Int, String)] = ??? // error

type Person = (name: Int, age: String)

val p: Person = f

0 comments on commit 97757f7

Please sign in to comment.