@@ -1093,6 +1093,16 @@ object desugar {
10931093 case IdPattern (named, tpt) =>
10941094 derivedValDef(original, named, tpt, rhs, mods)
10951095 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+
10961106 def isTuplePattern (arity : Int ): Boolean = pat match {
10971107 case Tuple (pats) if pats.size == arity =>
10981108 pats.forall(isVarPattern)
@@ -1108,13 +1118,23 @@ object desugar {
11081118 // - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
11091119 val tupleOptimizable = forallResults(rhs, isMatchingTuple)
11101120
1121+ val inAliasGenerator = original match
1122+ case _ : GenAlias => true
1123+ case _ => false
1124+
11111125 val vars =
11121126 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 `_`
11181138
11191139 val ids = for ((named, _) <- vars) yield Ident (named.name)
11201140 val caseDef = CaseDef (pat, EmptyTree , makeTuple(ids))
@@ -1800,16 +1820,21 @@ object desugar {
18001820 /** Returns list of all pattern variables, possibly with their types,
18011821 * without duplicates
18021822 */
1803- private def getVariables (tree : Tree )(using Context ): List [VarInfo ] = {
1823+ private def getVariables (tree : Tree , shouldAddGiven : Context ?=> Bind => Boolean )(using Context ): List [VarInfo ] = {
18041824 val buf = ListBuffer [VarInfo ]()
18051825 def seenName (name : Name ) = buf exists (_._1.name == name)
18061826 def add (named : NameTree , t : Tree ): Unit =
18071827 if (! seenName(named.name) && named.name.isTermName) buf += ((named, t))
18081828 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)
18101834 collect(tree1)
18111835 case tree @ Bind (_, Typed (tree1, tpt)) =>
1812- add(tree, tpt)
1836+ if ! (tree.mods.is(Given ) && ! shouldAddGiven(tree)) then
1837+ add(tree, tpt)
18131838 collect(tree1)
18141839 case tree @ Bind (_, tree1) =>
18151840 add(tree, TypeTree ())
@@ -1827,7 +1852,7 @@ object desugar {
18271852 case SeqLiteral (elems, _) =>
18281853 elems foreach collect
18291854 case Alternative (trees) =>
1830- for (tree <- trees; (vble, _) <- getVariables(tree))
1855+ for (tree <- trees; (vble, _) <- getVariables(tree, shouldAddGiven ))
18311856 report.error(IllegalVariableInPatternAlternative (), vble.srcPos)
18321857 case Annotated (arg, _) =>
18331858 collect(arg)
0 commit comments