From 484b8086ef1d5f66219d5865b1f3f5bf1712aa89 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 14 Mar 2024 17:20:51 +0100 Subject: [PATCH] Avoid spurious val binding in quote pattern Fixes #19947 [Cherry-picked 96afd7a4dfd177f8db99d5216d95278759cc37a3] --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 6 +++--- tests/pos-macros/i19947/Macro_1.scala | 9 +++++++++ tests/pos-macros/i19947/Test_2.scala | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 tests/pos-macros/i19947/Macro_1.scala create mode 100644 tests/pos-macros/i19947/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9539422d94cf..7637bb3d5e2a 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2935,8 +2935,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer /** Translate infix operation expression `l op r` to * * l.op(r) if `op` is left-associative - * { val x = l; r.op(x) } if `op` is right-associative call-by-value and `l` is impure - * r.op(l) if `op` is right-associative call-by-name or `l` is pure + * { val x = l; r.op(x) } if `op` is right-associative call-by-value and `l` is impure, and not in a quote pattern + * r.op(l) if `op` is right-associative call-by-name, or `l` is pure, or in a quote pattern * * Translate infix type `l op r` to `op[l, r]` * Translate infix pattern `l op r` to `op(l, r)` @@ -2953,7 +2953,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer typedUnApply(cpy.Apply(tree)(op, l :: r :: Nil), pt) else { val app = typedApply(desugar.binop(l, op, r), pt) - if op.name.isRightAssocOperatorName then + if op.name.isRightAssocOperatorName && !ctx.mode.is(Mode.QuotedPattern) then val defs = new mutable.ListBuffer[Tree] def lift(app: Tree): Tree = (app: @unchecked) match case Apply(fn, args) => diff --git a/tests/pos-macros/i19947/Macro_1.scala b/tests/pos-macros/i19947/Macro_1.scala new file mode 100644 index 000000000000..dc9ae46eb55f --- /dev/null +++ b/tests/pos-macros/i19947/Macro_1.scala @@ -0,0 +1,9 @@ +import scala.quoted.* + +inline def expandMacro(inline from: Tuple): Any = + ${ expandMacroImpl } + +def expandMacroImpl(using Quotes): Expr[?] = + '{ 1 *: EmptyTuple } match + case '{ ($hd: Int) *: ($tl: Tuple) } => '{ ??? } + case x => throw new MatchError(x.show) diff --git a/tests/pos-macros/i19947/Test_2.scala b/tests/pos-macros/i19947/Test_2.scala new file mode 100644 index 000000000000..85c0dc845dc4 --- /dev/null +++ b/tests/pos-macros/i19947/Test_2.scala @@ -0,0 +1 @@ +def test: Any = expandMacro