Skip to content

Commit 64bddf8

Browse files
Update PatternMatcher logic for name based pattern matching
1 parent e132720 commit 64bddf8

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,15 +844,11 @@ class Definitions {
844844
}
845845

846846
def isProductSubType(tp: Type)(implicit ctx: Context) =
847-
(tp derivesFrom ProductType.symbol) && tp.baseClasses.exists(isProductClass)
847+
(tp.derivesFrom(ProductType.symbol) && tp.baseClasses.exists(isProductClass)) ||
848+
tp.derivesFrom(NameBasedPatternType.symbol)
848849

849850
def productArity(tp: Type)(implicit ctx: Context) =
850-
if (tp derivesFrom ProductType.symbol)
851-
tp.baseClasses.find(isProductClass) match {
852-
case Some(prod) => prod.typeParams.length
853-
case None => -1
854-
}
855-
else -1
851+
if (isProductSubType(tp)) typer.Applications.productSelectorTypes(tp).size else -1
856852

857853
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN ? */
858854
def isFunctionType(tp: Type)(implicit ctx: Context) = {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ object Applications {
5252
* This is the case of `tp` is a subtype of the Product<numArgs> class.
5353
*/
5454
def isProductMatch(tp: Type, numArgs: Int)(implicit ctx: Context) =
55-
0 <= numArgs && numArgs <= Definitions.MaxTupleArity &&
56-
tp.derivesFrom(defn.ProductNType(numArgs).typeSymbol)
55+
0 <= numArgs && defn.isProductSubType(tp) &&
56+
productSelectorTypes(tp).size == numArgs
5757

5858
/** Does `tp` fit the "get match" conditions as an unapply result type?
5959
* This is the case of `tp` has a `get` member as well as a

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
761761

762762
/** Is `formal` a product type which is elementwise compatible with `params`? */
763763
def ptIsCorrectProduct(formal: Type) = {
764-
val pclass = defn.ProductNType(params.length).symbol
765764
isFullyDefined(formal, ForceDegree.noBottom) &&
766-
formal.derivesFrom(pclass) &&
767-
formal.baseArgTypes(pclass).corresponds(params) {
765+
defn.isProductSubType(formal) &&
766+
Applications.productSelectorTypes(formal).corresponds(params) {
768767
(argType, param) =>
769768
param.tpt.isEmpty || argType <:< typedAheadType(param.tpt).tpe
770769
}

0 commit comments

Comments
 (0)