Skip to content

Commit 6586418

Browse files
Avoid spurious val binding in quote pattern (#19948)
In quoted patterns, we do not want to generate the val bindings. We care about the original structure if the pattern expression. Fixes #19947
2 parents 01c6623 + 96afd7a commit 6586418

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -3005,8 +3005,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30053005
/** Translate infix operation expression `l op r` to
30063006
*
30073007
* l.op(r) if `op` is left-associative
3008-
* { val x = l; r.op(x) } if `op` is right-associative call-by-value and `l` is impure
3009-
* r.op(l) if `op` is right-associative call-by-name or `l` is pure
3008+
* { val x = l; r.op(x) } if `op` is right-associative call-by-value and `l` is impure, and not in a quote pattern
3009+
* r.op(l) if `op` is right-associative call-by-name, or `l` is pure, or in a quote pattern
30103010
*
30113011
* Translate infix type `l op r` to `op[l, r]`
30123012
* Translate infix pattern `l op r` to `op(l, r)`
@@ -3023,7 +3023,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
30233023
typedUnApply(cpy.Apply(tree)(op, l :: r :: Nil), pt)
30243024
else {
30253025
val app = typedApply(desugar.binop(l, op, r), pt)
3026-
if op.name.isRightAssocOperatorName then
3026+
if op.name.isRightAssocOperatorName && !ctx.mode.is(Mode.QuotedExprPattern) then
30273027
val defs = new mutable.ListBuffer[Tree]
30283028
def lift(app: Tree): Tree = (app: @unchecked) match
30293029
case Apply(fn, args) =>

tests/pos-macros/i19947/Macro_1.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted.*
2+
3+
inline def expandMacro(inline from: Tuple): Any =
4+
${ expandMacroImpl }
5+
6+
def expandMacroImpl(using Quotes): Expr[?] =
7+
'{ 1 *: EmptyTuple } match
8+
case '{ ($hd: Int) *: ($tl: Tuple) } => '{ ??? }
9+
case x => throw new MatchError(x.show)

tests/pos-macros/i19947/Test_2.scala

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test: Any = expandMacro

0 commit comments

Comments
 (0)