@@ -182,6 +182,7 @@ object desugar {
182
182
tpt = TypeTree (defn.UnitType ),
183
183
rhs = setterRhs
184
184
).withMods((mods | Accessor ) &~ (CaseAccessor | GivenOrImplicit | Lazy ))
185
+ .dropEndMarker() // the end marker should only appear on the getter definition
185
186
Thicket (vdef1, setter)
186
187
}
187
188
else vdef1
@@ -883,6 +884,7 @@ object desugar {
883
884
val clsTmpl = cpy.Template (impl)(self = clsSelf, body = impl.body)
884
885
val cls = TypeDef (clsName, clsTmpl)
885
886
.withMods(mods.toTypeFlags & RetainedModuleClassFlags | ModuleClassCreationFlags )
887
+ .withEndMarker(copyFrom = mdef) // copy over the end marker position to the module class def
886
888
Thicket (modul, classDef(cls).withSpan(mdef.span))
887
889
}
888
890
}
@@ -1091,6 +1093,16 @@ object desugar {
1091
1093
case IdPattern (named, tpt) =>
1092
1094
derivedValDef(original, named, tpt, rhs, mods)
1093
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
+
1094
1106
def isTuplePattern (arity : Int ): Boolean = pat match {
1095
1107
case Tuple (pats) if pats.size == arity =>
1096
1108
pats.forall(isVarPattern)
@@ -1106,13 +1118,23 @@ object desugar {
1106
1118
// - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
1107
1119
val tupleOptimizable = forallResults(rhs, isMatchingTuple)
1108
1120
1121
+ val inAliasGenerator = original match
1122
+ case _ : GenAlias => true
1123
+ case _ => false
1124
+
1109
1125
val vars =
1110
1126
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 `_`
1116
1138
1117
1139
val ids = for ((named, _) <- vars) yield Ident (named.name)
1118
1140
val caseDef = CaseDef (pat, EmptyTree , makeTuple(ids))
@@ -1299,7 +1321,9 @@ object desugar {
1299
1321
if (nestedStats.isEmpty) pdef
1300
1322
else {
1301
1323
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 ))
1303
1327
cpy.PackageDef (pdef)(pdef.pid, topStats :+ grouped)
1304
1328
}
1305
1329
}
@@ -1796,16 +1820,21 @@ object desugar {
1796
1820
/** Returns list of all pattern variables, possibly with their types,
1797
1821
* without duplicates
1798
1822
*/
1799
- private def getVariables (tree : Tree )(using Context ): List [VarInfo ] = {
1823
+ private def getVariables (tree : Tree , shouldAddGiven : Context ?=> Bind => Boolean )(using Context ): List [VarInfo ] = {
1800
1824
val buf = ListBuffer [VarInfo ]()
1801
1825
def seenName (name : Name ) = buf exists (_._1.name == name)
1802
1826
def add (named : NameTree , t : Tree ): Unit =
1803
1827
if (! seenName(named.name) && named.name.isTermName) buf += ((named, t))
1804
1828
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)
1806
1834
collect(tree1)
1807
1835
case tree @ Bind (_, Typed (tree1, tpt)) =>
1808
- add(tree, tpt)
1836
+ if ! (tree.mods.is(Given ) && ! shouldAddGiven(tree)) then
1837
+ add(tree, tpt)
1809
1838
collect(tree1)
1810
1839
case tree @ Bind (_, tree1) =>
1811
1840
add(tree, TypeTree ())
@@ -1823,7 +1852,7 @@ object desugar {
1823
1852
case SeqLiteral (elems, _) =>
1824
1853
elems foreach collect
1825
1854
case Alternative (trees) =>
1826
- for (tree <- trees; (vble, _) <- getVariables(tree))
1855
+ for (tree <- trees; (vble, _) <- getVariables(tree, shouldAddGiven ))
1827
1856
report.error(IllegalVariableInPatternAlternative (), vble.srcPos)
1828
1857
case Annotated (arg, _) =>
1829
1858
collect(arg)
0 commit comments