@@ -1093,6 +1093,16 @@ object desugar {
1093
1093
case IdPattern (named, tpt) =>
1094
1094
derivedValDef(original, named, tpt, rhs, mods)
1095
1095
case _ =>
1096
+
1097
+ def filterWildcardGivenBinding (givenPat : Bind ): Boolean =
1098
+ givenPat.name != nme.WILDCARD
1099
+
1100
+ def errorOnGivenBinding (bind : Bind )(using Context ): Boolean =
1101
+ report.error(
1102
+ em """ ${hl(" given" )} patterns are not allowed in a ${hl(" val" )} definition,
1103
+ |please bind to an identifier and use an alias given. """ .stripMargin, bind)
1104
+ false
1105
+
1096
1106
def isTuplePattern (arity : Int ): Boolean = pat match {
1097
1107
case Tuple (pats) if pats.size == arity =>
1098
1108
pats.forall(isVarPattern)
@@ -1108,13 +1118,23 @@ object desugar {
1108
1118
// - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
1109
1119
val tupleOptimizable = forallResults(rhs, isMatchingTuple)
1110
1120
1121
+ val inAliasGenerator = original match
1122
+ case _ : GenAlias => true
1123
+ case _ => false
1124
+
1111
1125
val vars =
1112
1126
if (tupleOptimizable) // include `_`
1113
- pat match {
1114
- case Tuple (pats) =>
1115
- pats.map { case id : Ident => id -> TypeTree () }
1116
- }
1117
- else getVariables(pat) // no `_`
1127
+ pat match
1128
+ case Tuple (pats) => pats.map { case id : Ident => id -> TypeTree () }
1129
+ else
1130
+ getVariables(
1131
+ tree = pat,
1132
+ shouldAddGiven =
1133
+ if inAliasGenerator then
1134
+ filterWildcardGivenBinding
1135
+ else
1136
+ errorOnGivenBinding
1137
+ ) // no `_`
1118
1138
1119
1139
val ids = for ((named, _) <- vars) yield Ident (named.name)
1120
1140
val caseDef = CaseDef (pat, EmptyTree , makeTuple(ids))
@@ -1800,16 +1820,21 @@ object desugar {
1800
1820
/** Returns list of all pattern variables, possibly with their types,
1801
1821
* without duplicates
1802
1822
*/
1803
- private def getVariables (tree : Tree )(using Context ): List [VarInfo ] = {
1823
+ private def getVariables (tree : Tree , shouldAddGiven : Context ?=> Bind => Boolean )(using Context ): List [VarInfo ] = {
1804
1824
val buf = ListBuffer [VarInfo ]()
1805
1825
def seenName (name : Name ) = buf exists (_._1.name == name)
1806
1826
def add (named : NameTree , t : Tree ): Unit =
1807
1827
if (! seenName(named.name) && named.name.isTermName) buf += ((named, t))
1808
1828
def collect (tree : Tree ): Unit = tree match {
1809
- case Bind (nme.WILDCARD , tree1) =>
1829
+ case tree @ Bind (nme.WILDCARD , tree1) =>
1830
+ if tree.mods.is(Given ) then
1831
+ val Typed (_, tpt) = tree1 : @ unchecked
1832
+ if shouldAddGiven(tree) then
1833
+ add(tree, tpt)
1810
1834
collect(tree1)
1811
1835
case tree @ Bind (_, Typed (tree1, tpt)) =>
1812
- add(tree, tpt)
1836
+ if ! (tree.mods.is(Given ) && ! shouldAddGiven(tree)) then
1837
+ add(tree, tpt)
1813
1838
collect(tree1)
1814
1839
case tree @ Bind (_, tree1) =>
1815
1840
add(tree, TypeTree ())
@@ -1827,7 +1852,7 @@ object desugar {
1827
1852
case SeqLiteral (elems, _) =>
1828
1853
elems foreach collect
1829
1854
case Alternative (trees) =>
1830
- for (tree <- trees; (vble, _) <- getVariables(tree))
1855
+ for (tree <- trees; (vble, _) <- getVariables(tree, shouldAddGiven ))
1831
1856
report.error(IllegalVariableInPatternAlternative (), vble.srcPos)
1832
1857
case Annotated (arg, _) =>
1833
1858
collect(arg)
0 commit comments