Skip to content

Commit

Permalink
Avoid spurious val binding in quote pattern (#19948)
Browse files Browse the repository at this point in the history
In quoted patterns, we do not want to generate the val bindings. We care
about the original structure if the pattern expression.

Fixes #19947
  • Loading branch information
nicolasstucki authored Mar 19, 2024
2 parents 01c6623 + 96afd7a commit 6586418
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3005,8 +3005,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)`
Expand All @@ -3023,7 +3023,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.QuotedExprPattern) then
val defs = new mutable.ListBuffer[Tree]
def lift(app: Tree): Tree = (app: @unchecked) match
case Apply(fn, args) =>
Expand Down
9 changes: 9 additions & 0 deletions tests/pos-macros/i19947/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/pos-macros/i19947/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test: Any = expandMacro

0 comments on commit 6586418

Please sign in to comment.