Skip to content

Commit 6574b32

Browse files
author
EnzeXing
committed
Support implicit arguments before extractor method
1 parent 16dd58f commit 6574b32

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,9 +1298,13 @@ object Objects:
12981298
case select: Select =>
12991299
eval(select.qualifier, thisV, klass)
13001300

1301-
val implicitValues = evalArgs(implicits.map(Arg.apply), thisV, klass)
1302-
// TODO: implicit values may appear before and/or after the scrutinee parameter.
1303-
val unapplyRes = call(receiver, funRef.symbol, TraceValue(scrutinee, summon[Trace]) :: implicitValues, funRef.prefix, superType = NoType, needResolve = true)
1301+
def implicitValuesBeforeScrutinee(fun: Tree): Contextual[List[ArgInfo]] = fun match
1302+
case Apply(f, implicitArgs) =>
1303+
implicitValuesBeforeScrutinee(f) ++ evalArgs(implicitArgs.map(Arg.apply), thisV, klass)
1304+
case _ => List()
1305+
1306+
val implicitValuesAfterScrtinee = evalArgs(implicits.map(Arg.apply), thisV, klass)
1307+
val unapplyRes = call(receiver, funRef.symbol, TraceValue(scrutinee, summon[Trace]) :: (implicitValuesBeforeScrutinee(fun) ++ implicitValuesAfterScrtinee), funRef.prefix, superType = NoType, needResolve = true)
13041308

13051309
if fun.symbol.name == nme.unapplySeq then
13061310
var resultTp = unapplyResTp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo
2+
3+
object Bar {
4+
def unapply(using Foo)(pair: (Int, Int))(using Foo): Option[Int] =
5+
if pair._1 == 0 then Some(pair._1) else Some(pair._2)
6+
given Foo = new Foo
7+
val i1: Int = 0
8+
val i2: Int = (i1, i2) match // error
9+
case Bar(i) => i
10+
case _ => 0
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo
2+
3+
object Bar {
4+
def unapplySeq(using Foo)(using Foo)(pair: (Int, Int))(using Foo): Option[Seq[Int]] =
5+
if pair._1 == 0 then Some(Seq(pair._1)) else Some(Seq(pair._2))
6+
given Foo = new Foo
7+
val i1: Int = 0
8+
val i2: Int = (i1, i2) match // error
9+
case Bar(i) => i
10+
case _ => 0
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo
2+
3+
object Bar {
4+
def unapply(using Foo)(using Foo)(pair: (Int, Int))(using Foo): Option[Int] =
5+
if pair._1 == 0 then Some(pair._1) else Some(pair._2)
6+
given Foo = new Foo
7+
val i1: Int = 0
8+
val i2: Int = (i1, i1) match
9+
case Bar(i) => i
10+
case _ => 0
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Foo
2+
3+
object Bar {
4+
def unapplySeq(using Foo)(using Foo)(pair: (Int, Int))(using Foo): Option[Seq[Int]] =
5+
if pair._1 == 0 then Some(Seq(pair._1)) else Some(Seq(pair._2))
6+
given Foo = new Foo
7+
val i1: Int = 0
8+
val i2: Int = (i1, i1) match
9+
case Bar(i) => i
10+
case _ => 0
11+
}

0 commit comments

Comments
 (0)