@@ -20,7 +20,7 @@ import dotty.tools.dotc.typer.Implicits._
20
20
import dotty .tools .dotc .typer .Inferencing ._
21
21
import dotty .tools .dotc .util .Spans ._
22
22
import dotty .tools .dotc .util .Stats .record
23
-
23
+ import dotty . tools . dotc . reporting . IllegalVariableInPatternAlternative
24
24
import scala .collection .mutable
25
25
26
26
@@ -243,6 +243,17 @@ trait QuotesAndSplices {
243
243
res
244
244
}
245
245
246
+ def checkAlternativeBinds (pat0 : Tree ): Unit =
247
+ def rec (pat : Tree ): Unit =
248
+ pat match
249
+ case Typed (pat, _) => rec(pat)
250
+ case UnApply (_, _, pats) => pats.foreach(rec)
251
+ case pat : Bind =>
252
+ report.error(IllegalVariableInPatternAlternative (pat.symbol.name), pat.withSpan(pat.nameSpan))
253
+ rec(pat.body)
254
+ case _ =>
255
+ if ctx.mode.is(Mode .InPatternAlternative ) then rec(pat0)
256
+
246
257
val patBuf = new mutable.ListBuffer [Tree ]
247
258
val freshTypePatBuf = new mutable.ListBuffer [Tree ]
248
259
val freshTypeBindingsBuff = new mutable.ListBuffer [Tree ]
@@ -254,6 +265,7 @@ trait QuotesAndSplices {
254
265
val newSplice = ref(defn.QuotedRuntime_exprSplice ).appliedToType(tpt1.tpe).appliedTo(Typed (pat, exprTpt))
255
266
transform(newSplice)
256
267
case Apply (TypeApply (fn, targs), Apply (sp, pat :: Nil ) :: args :: Nil ) if fn.symbol == defn.QuotedRuntimePatterns_patternHigherOrderHole =>
268
+ checkAlternativeBinds(pat)
257
269
args match // TODO support these patterns. Possibly using scala.quoted.util.Var
258
270
case SeqLiteral (args, _) =>
259
271
for arg <- args; if arg.symbol.is(Mutable ) do
@@ -266,6 +278,7 @@ trait QuotesAndSplices {
266
278
patBuf += pat1
267
279
}
268
280
case Apply (fn, pat :: Nil ) if fn.symbol.isExprSplice =>
281
+ checkAlternativeBinds(pat)
269
282
try ref(defn.QuotedRuntimePatterns_patternHole .termRef).appliedToType(tree.tpe).withSpan(tree.span)
270
283
finally {
271
284
val patType = pat.tpe.widen
@@ -321,7 +334,9 @@ trait QuotesAndSplices {
321
334
}
322
335
323
336
private def transformTypeBindingTypeDef (nameOfSyntheticGiven : TermName , tdef : TypeDef , buff : mutable.Builder [Tree , List [Tree ]])(using Context ): Tree = {
324
- if (variance == - 1 )
337
+ if ctx.mode.is(Mode .InPatternAlternative ) then
338
+ report.error(IllegalVariableInPatternAlternative (tdef.symbol.name), tdef.srcPos)
339
+ if variance == - 1 then
325
340
tdef.symbol.addAnnotation(Annotation (New (ref(defn.QuotedRuntimePatterns_fromAboveAnnot .typeRef)).withSpan(tdef.span)))
326
341
val bindingType = getBinding(tdef.symbol).symbol.typeRef
327
342
val bindingTypeTpe = AppliedType (defn.QuotedTypeClass .typeRef, bindingType :: Nil )
0 commit comments