@@ -182,6 +182,7 @@ object desugar {
182182 tpt = TypeTree (defn.UnitType ),
183183 rhs = setterRhs
184184 ).withMods((mods | Accessor ) &~ (CaseAccessor | GivenOrImplicit | Lazy ))
185+ .dropEndMarker() // the end marker should only appear on the getter definition
185186 Thicket (vdef1, setter)
186187 }
187188 else vdef1
@@ -883,6 +884,7 @@ object desugar {
883884 val clsTmpl = cpy.Template (impl)(self = clsSelf, body = impl.body)
884885 val cls = TypeDef (clsName, clsTmpl)
885886 .withMods(mods.toTypeFlags & RetainedModuleClassFlags | ModuleClassCreationFlags )
887+ .withEndMarker(copyFrom = mdef) // copy over the end marker position to the module class def
886888 Thicket (modul, classDef(cls).withSpan(mdef.span))
887889 }
888890 }
@@ -1091,6 +1093,16 @@ object desugar {
10911093 case IdPattern (named, tpt) =>
10921094 derivedValDef(original, named, tpt, rhs, mods)
10931095 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+
10941106 def isTuplePattern (arity : Int ): Boolean = pat match {
10951107 case Tuple (pats) if pats.size == arity =>
10961108 pats.forall(isVarPattern)
@@ -1106,13 +1118,23 @@ object desugar {
11061118 // - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
11071119 val tupleOptimizable = forallResults(rhs, isMatchingTuple)
11081120
1121+ val inAliasGenerator = original match
1122+ case _ : GenAlias => true
1123+ case _ => false
1124+
11091125 val vars =
11101126 if (tupleOptimizable) // include `_`
1111- pat match {
1112- case Tuple (pats) =>
1113- pats.map { case id : Ident => id -> TypeTree () }
1114- }
1115- 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 `_`
11161138
11171139 val ids = for ((named, _) <- vars) yield Ident (named.name)
11181140 val caseDef = CaseDef (pat, EmptyTree , makeTuple(ids))
@@ -1299,7 +1321,9 @@ object desugar {
12991321 if (nestedStats.isEmpty) pdef
13001322 else {
13011323 val name = packageObjectName(ctx.source)
1302- val grouped = ModuleDef (name, Template (emptyConstructor, Nil , Nil , EmptyValDef , nestedStats))
1324+ val grouped =
1325+ ModuleDef (name, Template (emptyConstructor, Nil , Nil , EmptyValDef , nestedStats))
1326+ .withMods(Modifiers (Synthetic ))
13031327 cpy.PackageDef (pdef)(pdef.pid, topStats :+ grouped)
13041328 }
13051329 }
@@ -1796,16 +1820,21 @@ object desugar {
17961820 /** Returns list of all pattern variables, possibly with their types,
17971821 * without duplicates
17981822 */
1799- private def getVariables (tree : Tree )(using Context ): List [VarInfo ] = {
1823+ private def getVariables (tree : Tree , shouldAddGiven : Context ?=> Bind => Boolean )(using Context ): List [VarInfo ] = {
18001824 val buf = ListBuffer [VarInfo ]()
18011825 def seenName (name : Name ) = buf exists (_._1.name == name)
18021826 def add (named : NameTree , t : Tree ): Unit =
18031827 if (! seenName(named.name) && named.name.isTermName) buf += ((named, t))
18041828 def collect (tree : Tree ): Unit = tree match {
1805- 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)
18061834 collect(tree1)
18071835 case tree @ Bind (_, Typed (tree1, tpt)) =>
1808- add(tree, tpt)
1836+ if ! (tree.mods.is(Given ) && ! shouldAddGiven(tree)) then
1837+ add(tree, tpt)
18091838 collect(tree1)
18101839 case tree @ Bind (_, tree1) =>
18111840 add(tree, TypeTree ())
@@ -1823,7 +1852,7 @@ object desugar {
18231852 case SeqLiteral (elems, _) =>
18241853 elems foreach collect
18251854 case Alternative (trees) =>
1826- for (tree <- trees; (vble, _) <- getVariables(tree))
1855+ for (tree <- trees; (vble, _) <- getVariables(tree, shouldAddGiven ))
18271856 report.error(IllegalVariableInPatternAlternative (), vble.srcPos)
18281857 case Annotated (arg, _) =>
18291858 collect(arg)
0 commit comments