Skip to content

Commit 2cb5dd6

Browse files
authored
Merge pull request #6248 from dotty-staging/fix-#6214
Fix #6214: Set correct owner for splices in quote patterns
2 parents ae0df96 + ade87ab commit 2cb5dd6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1988,8 +1988,10 @@ class Typer extends Namer
19881988
case expr =>
19891989
if (ctx.mode.is(Mode.QuotedPattern) && level == 1) {
19901990
fullyDefinedType(pt, "quoted pattern selector", tree.span)
1991+
def spliceOwner(ctx: Context): Symbol =
1992+
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
19911993
val pat = typedPattern(expr, defn.QuotedExprType.appliedTo(pt))(
1992-
spliceContext.retractMode(Mode.QuotedPattern))
1994+
spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
19931995
Splice(pat)
19941996
}
19951997
else

tests/pos/i6214.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
object Test {
3+
def res(x: quoted.Expr[Int]) given tasty.Reflection: quoted.Expr[Int] = x match {
4+
case '{ val a: Int = $y; 1} => y // owner of `y` is `res`
5+
case _ => '{ val b: Int = ${val c = 2; c.toExpr}; 1} // owner of `c` is `b`, but that seems to be OK
6+
}
7+
}

tests/pos/i6214b.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
def res(x: quoted.Expr[Int]) given tasty.Reflection: quoted.Expr[Int] = x match {
3+
case '{ val a: Int = ${ Foo('{ val b: Int = $y; b }) }; a } => y // owner of y is res
4+
}
5+
object Foo {
6+
def unapply(x: quoted.Expr[Int]): Option[quoted.Expr[Int]] = Some(x)
7+
}
8+
}

0 commit comments

Comments
 (0)