Skip to content

Commit ace6e93

Browse files
Update PatternMatcher logic for name based pattern matching
1 parent 54b8b51 commit ace6e93

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
@@ -797,15 +797,11 @@ class Definitions {
797797
}
798798

799799
def isProductSubType(tp: Type)(implicit ctx: Context) =
800-
(tp derivesFrom ProductType.symbol) && tp.baseClasses.exists(isProductClass)
800+
(tp.derivesFrom(ProductType.symbol) && tp.baseClasses.exists(isProductClass)) ||
801+
tp.derivesFrom(NameBasedPatternType.symbol)
801802

802803
def productArity(tp: Type)(implicit ctx: Context) =
803-
if (tp derivesFrom ProductType.symbol)
804-
tp.baseClasses.find(isProductClass) match {
805-
case Some(prod) => prod.typeParams.length
806-
case None => -1
807-
}
808-
else -1
804+
if (isProductSubType(tp)) typer.Applications.productSelectorTypes(tp).size else -1
809805

810806
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ImplicitFunctionN ? */
811807
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)