Skip to content

Commit 31381e1

Browse files
committed
Strip named tuple from scrutinee when testing refutability of a named pattern match
1 parent c56bcf5 commit 31381e1

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ object TypeOps:
387387
if tycon1.typeSymbol == tycon2.typeSymbol && (tycon1 =:= tycon2) =>
388388
mergeRefinedOrApplied(tp1, tp2) match
389389
case tp: AppliedType if tp.isUnreducibleWild =>
390-
// fall back to or-dominators rather tahn inferring a type that would
391-
// caue an unreducible type error later.
390+
// fall back to or-dominators rather than inferring a type that would
391+
// cause an unreducible type error later.
392392
approximateOr(tp1, tp2)
393393
case tp => tp
394394
case (tp1, tp2) =>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,16 @@ trait Checking {
963963
false
964964
}
965965

966-
def check(pat: Tree, pt: Type): Boolean =
966+
// Is scrutinee type `pt` a subtype of `pat.tpe`, after stripping named tuples
967+
// and accounting for large generic tuples?
968+
// Named tuples need to be stripped off, since names are dropped in patterns
969+
def conforms(pat: Tree, pt: Type): Boolean =
967970
pt.isTupleXXLExtract(pat.tpe) // See isTupleXXLExtract, fixes TupleXXL parameter type
968-
|| pt <:< pat.tpe
969-
|| fail(pat, pt, Reason.NonConforming)
971+
|| pt.stripNamedTuple <:< pat.tpe
972+
|| (pt.widen ne pt) && conforms(pat, pt.widen)
973+
974+
def check(pat: Tree, pt: Type): Boolean =
975+
conforms(pat, pt) || fail(pat, pt, Reason.NonConforming)
970976

971977
def recur(pat: Tree, pt: Type): Boolean =
972978
!sourceVersion.isAtLeast(`3.2`)

0 commit comments

Comments
 (0)