Skip to content

Commit 3f184c6

Browse files
oderskyWojciechMazur
authored andcommitted
Adapt refutability warning in Space engine to named tuples
Fixes #22899 [Cherry-picked 583f75f]
1 parent 842c13b commit 3f184c6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ object SpaceEngine {
279279
|| unappResult <:< ConstantType(Constant(true)) // only for unapply
280280
|| (unapp.symbol.is(Synthetic) && unapp.symbol.owner.linkedClass.is(Case)) // scala2 compatibility
281281
|| unapplySeqTypeElemTp(unappResult).exists // only for unapplySeq
282-
|| isProductMatch(unappResult, argLen)
282+
|| isProductMatch(unappResult.stripNamedTuple, argLen)
283283
|| extractorMemberType(unappResult, nme.isEmpty, NoSourcePosition) <:< ConstantType(Constant(false))
284284
|| unappResult.derivesFrom(defn.NonEmptyTupleClass)
285285
|| unapp.symbol == defn.TupleXXL_unapplySeq // Fixes TupleXXL.unapplySeq which returns Some but declares Option

tests/warn/i22899.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
case class CaseClass(a: Int)
2+
3+
object ProductMatch_CaseClass {
4+
def unapply(int: Int): CaseClass = CaseClass(int)
5+
}
6+
7+
object ProductMatch_NamedTuple {
8+
def unapply(int: Int): (a: Int) = (a = int)
9+
}
10+
11+
object NameBasedMatch_CaseClass {
12+
def unapply(int: Int): Some[CaseClass] = Some(CaseClass(int))
13+
}
14+
15+
object NameBasedMatch_NamedTuple {
16+
def unapply(int: Int): Some[(a: Int)] = Some((a = int))
17+
}
18+
19+
object Test {
20+
val ProductMatch_CaseClass(a = x1) = 1 // ok, was pattern's type (x1 : Int) is more specialized than the right hand side expression's type Int
21+
val ProductMatch_NamedTuple(a = x2) = 2 // ok, was pattern binding uses refutable extractor `org.test.ProductMatch_NamedTuple`
22+
val NameBasedMatch_CaseClass(a = x3) = 3 // ok, was pattern's type (x3 : Int) is more specialized than the right hand side expression's type Int
23+
val NameBasedMatch_NamedTuple(a = x4) = 4 // ok, was pattern's type (x4 : Int) is more specialized than the right hand side expression's type Int
24+
25+
val CaseClass(a = x5) = CaseClass(5) // ok, was pattern's type (x5 : Int) is more specialized than the right hand side expression's type Int
26+
val (a = x6) = (a = 6) // ok
27+
}

0 commit comments

Comments
 (0)