Skip to content

Commit bb549ed

Browse files
dwijnandtgodzik
authored andcommitted
Fix ctx implicits under case unapplySeq
A case class with a varargs has a unapplySeq extractor instead of unapply. When we type an unapply, in typedUnapply, we first look for unapply methods before unapplySeq methods. But when searching for unapply, if a class method isn't found, then an extension method is looked for, which causes context implicits to be cached. The bindings from a pattern (such as from an unapply or unapplySeq extractor) are added to the context in indexPattern. But Context's `implicitCache` doesn't account for the scope changing. I opted for giving the body its own scope context, rather than making indexPattern reset the context implicits cache. [Cherry-picked 69f5f73]
1 parent 680a989 commit bb549ed

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19201920
/** Type a case. */
19211921
def typedCase(tree: untpd.CaseDef, sel: Tree, wideSelType: Type, pt: Type)(using Context): CaseDef = {
19221922
val originalCtx = ctx
1923-
val gadtCtx: Context = ctx.fresh.setFreshGADTBounds.setNewScope
1923+
val gadtCtx: Context = ctx.fresh.setFreshGADTBounds
19241924

19251925
def caseRest(pat: Tree)(using Context) = {
19261926
val pt1 = instantiateMatchTypeProto(pat, pt) match {
@@ -1950,7 +1950,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19501950
val pat1 = typedPattern(tree.pat, wideSelType)(using gadtCtx)
19511951
caseRest(pat1)(
19521952
using Nullables.caseContext(sel, pat1)(
1953-
using gadtCtx))
1953+
using gadtCtx.fresh.setNewScope))
19541954
}
19551955

19561956
def typedLabeled(tree: untpd.Labeled)(using Context): Labeled = {

Diff for: tests/pos/i21742.1.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
case class C(n: Int, ds: Double*)
2+
class Test:
3+
def m(using n: Int): Int = n + 1
4+
def t(): Unit =
5+
C(1, 2, 3, 4) match { case C(given Int, ds*) => m }

Diff for: tests/pos/i21742.2.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
case class C(n: Int, ds: Seq[Double])
2+
class Test:
3+
def m(using n: Int): Int = n + 1
4+
def t(): Unit =
5+
C(1, Seq(2, 3, 4)) match { case C(given Int, ds) => m }

0 commit comments

Comments
 (0)