From e160de736a0e223d7b4acdc7b1d4710632c74deb Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 5 Jul 2024 12:24:01 +0100 Subject: [PATCH] Fix SeqFactoryClass#unapplySeq Previously `defn.ListType.appliedTo(elemTp) <:< pat.tpe` failed when pat.tpe is something like ParamClause, an alias. --- .../src/dotty/tools/dotc/transform/patmat/Space.scala | 2 +- tests/warn/i20132.list-Seq.scala | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/warn/i20132.list-Seq.scala diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index 39bf0d9bfc2a..9d60336c02d7 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -352,7 +352,7 @@ object SpaceEngine { val funRef = fun1.tpe.asInstanceOf[TermRef] if (fun.symbol.name == nme.unapplySeq) val (arity, elemTp, resultTp) = unapplySeqInfo(fun.tpe.widen.finalResultType, fun.srcPos) - if (fun.symbol.owner == defn.SeqFactoryClass && defn.ListType.appliedTo(elemTp) <:< pat.tpe) + if fun.symbol.owner == defn.SeqFactoryClass && pat.tpe.hasClassSymbol(defn.ListClass) then // The exhaustivity and reachability logic already handles decomposing sum types (into its subclasses) // and product types (into its components). To get better counter-examples for patterns that are of type // List (or a super-type of list, like LinearSeq) we project them into spaces that use `::` and Nil. diff --git a/tests/warn/i20132.list-Seq.scala b/tests/warn/i20132.list-Seq.scala new file mode 100644 index 000000000000..95d6e962d547 --- /dev/null +++ b/tests/warn/i20132.list-Seq.scala @@ -0,0 +1,10 @@ +class D1 +class D2 + +class Test1: + type Ds = List[D1] | List[D2] + def m1(dss: List[Ds]) = + dss.flatMap: + case Seq(d) => Some(1) + case Seq(head, tail*) => Some(2) + case Seq() => None