Skip to content

Commit 9b63df1

Browse files
Update PatternMatcher logic for name based pattern matching
1 parent 63ac7e2 commit 9b63df1

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
@@ -771,15 +771,11 @@ class Definitions {
771771
}
772772

773773
def isProductSubType(tp: Type)(implicit ctx: Context) =
774-
(tp derivesFrom ProductType.symbol) && tp.baseClasses.exists(isProductClass)
774+
(tp.derivesFrom(ProductType.symbol) && tp.baseClasses.exists(isProductClass)) ||
775+
tp.derivesFrom(NameBasedPatternType.symbol)
775776

776777
def productArity(tp: Type)(implicit ctx: Context) =
777-
if (tp derivesFrom ProductType.symbol)
778-
tp.baseClasses.find(isProductClass) match {
779-
case Some(prod) => prod.typeParams.length
780-
case None => -1
781-
}
782-
else -1
778+
if (isProductSubType(tp)) typer.Applications.productSelectorTypes(tp).size else -1
783779

784780
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN ? */
785781
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
@@ -746,10 +746,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
746746

747747
/** Is `formal` a product type which is elementwise compatible with `params`? */
748748
def ptIsCorrectProduct(formal: Type) = {
749-
val pclass = defn.ProductNType(params.length).symbol
750749
isFullyDefined(formal, ForceDegree.noBottom) &&
751-
formal.derivesFrom(pclass) &&
752-
formal.baseArgTypes(pclass).corresponds(params) {
750+
defn.isProductSubType(formal) &&
751+
Applications.productSelectorTypes(formal).corresponds(params) {
753752
(argType, param) =>
754753
param.tpt.isEmpty || argType <:< typedAheadType(param.tpt).tpe
755754
}

0 commit comments

Comments
 (0)