Skip to content

Refine bind tuple pattern typing for named tuples #23380

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if !isFullyDefined(pt, ForceDegree.all) then
return errorTree(tree, em"expected type of $tree is not fully defined")
val body1 = typed(tree.body, pt)

// When we pattern match a named tuple, both the named tuple pattern and the
// regular tuple pattern are desugared to a regular tuple unapply.
// If the pattern (body) is a named tuple pattern, we give the binding
// a named tuple type using pt; otherwise we give it the regular tuple type.
// For example, in `case x @ (a = 1, b = 2)`, the type of `x` will be `(a: Int, b: Int)`;
// in `case x @ (a, b)`, the type of `x` will be `(Int, Int)`.
def isNamedTuplePattern =
ctx.mode.is(Mode.Pattern)
&& pt.dealias.isNamedTupleType
&& tree.body.match
case untpd.Tuple((_: NamedArg) :: _) => true
case _ => false

body1 match {
case UnApply(fn, Nil, arg :: Nil)
if fn.symbol.exists && (fn.symbol.owner.derivesFrom(defn.TypeTestClass) || fn.symbol.owner == defn.ClassTagClass) && !body1.tpe.isError =>
Expand All @@ -2799,8 +2813,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
body1.isInstanceOf[RefTree] && !isWildcardArg(body1)
|| body1.isInstanceOf[Literal]
val symTp =
if isStableIdentifierOrLiteral || pt.dealias.isNamedTupleType then pt
// need to combine tuple element types with expected named type
if isStableIdentifierOrLiteral || isNamedTuplePattern then pt
else if isWildcardStarArg(body1)
|| pt == defn.ImplicitScrutineeTypeRef
|| body1.tpe <:< pt // There is some strange interaction with gadt matching.
Expand Down
19 changes: 19 additions & 0 deletions tests/run/bind-tuple-pattern.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import annotation.experimental

def getNamedTuple: (x: Int, y: String) = (x = 42, y = "Hello")

@main def Test =
getNamedTuple match
case (x, y) => assert(x == 42 && y == "Hello")

getNamedTuple match
case t @ (x = a, y = b) =>
// t binds to a named tuple pattern
// t: (x: Int, y: String)
assert(a == t.x && b == t.y)

getNamedTuple match
case t @ (a, b) =>
// t binds to a regular tuple pattern
// t: (Int, String)
assert(t._1 == a && t._2 == b)
Loading