Skip to content

Commit e99c982

Browse files
author
EnzeXing
committed
Correct argument order in evalPattern and add test
1 parent 6574b32 commit e99c982

9 files changed

+83
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ object Objects:
13041304
case _ => List()
13051305

13061306
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)
1307+
val unapplyRes = call(receiver, funRef.symbol, implicitValuesBeforeScrutinee(fun) ++ (TraceValue(scrutinee, summon[Trace]) :: implicitValuesAfterScrtinee), funRef.prefix, superType = NoType, needResolve = true)
13081308

13091309
if fun.symbol.name == nme.unapplySeq then
13101310
var resultTp = unapplyResTp
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
class Foo
2-
31
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)
2+
class Foo {
3+
def m1(i: Int) = i+1
4+
def m2(i: Int) = i+2
5+
}
6+
def unapply(using f1: Foo)(i: Int): Option[Int] =
7+
if i == 0 then Some(f1.m1(i)) else Some(f1.m2(i))
8+
69
given Foo = new Foo
710
val i1: Int = 0
8-
val i2: Int = (i1, i2) match // error
11+
val i2: Int = i2 match // error
912
case Bar(i) => i
1013
case _ => 0
1114
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Bar {
2+
class Foo {
3+
def m1(i: Int) = i+1
4+
def m2(i: Int) = i+2
5+
}
6+
def unapply(using f1: Foo)(i: Int): Option[Int] =
7+
if i == 0 then Some(f1.m1(i1)) else Some(f1.m2(i2)) // error
8+
9+
given Foo = new Foo
10+
val i1: Int = 0
11+
val i2: Int = i1 match
12+
case Bar(i) => i
13+
case _ => 0
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object Bar {
2+
class Foo {
3+
def m1(i: Int) = i + i1
4+
def m2(i: Int) = i + i2 // error
5+
}
6+
def unapply(using f1: Foo)(i: Int): Option[Int] =
7+
if i == 0 then Some(f1.m1(i)) else Some(f1.m2(i))
8+
9+
given Foo = new Foo
10+
val i1: Int = 0
11+
val i2: Int = i1 match
12+
case Bar(i) => i
13+
case _ => 0
14+
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
class Foo
2-
31
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))
2+
class Foo {
3+
def m1(seq: Seq[Int]) = 1 +: seq
4+
def m2(seq: Seq[Int]) = 2 +: seq
5+
}
6+
def unapplySeq(using f1: Foo)(seqi: Seq[Int]): Option[Seq[Int]] =
7+
if seqi(0) == 0 then Some(f1.m1(seqi)) else Some(f1.m2(seqi))
8+
69
given Foo = new Foo
710
val i1: Int = 0
8-
val i2: Int = (i1, i2) match // error
11+
val i2: Int = Seq(i2) match // error
912
case Bar(i) => i
1013
case _ => 0
1114
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Bar {
2+
class Foo
3+
def unapplySeq(using f1: Foo)(using f2: Foo)(seqi: Seq[Int])(using Foo): Option[Seq[Int]] =
4+
Some(i1 +: seqi) // error
5+
given Foo = new Foo
6+
val i1: Int = Seq(0) match {
7+
case Bar(i) => i
8+
case _ => 0
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Bar {
2+
class Foo {
3+
def m(seq: Seq[Int]) = i1 +: seq // error
4+
}
5+
def unapplySeq(using f1: Foo)(seqi: Seq[Int])(using Foo): Option[Seq[Int]] =
6+
Some(f1.m(seqi))
7+
given Foo = new Foo
8+
val i1: Int = Seq(0) match {
9+
case Bar(i, _) => i
10+
case _ => 0
11+
}
12+
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
class Foo
2-
31
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)
2+
class Foo {
3+
def m1(i: Int) = i + i1
4+
def m2(i: Int) = i + 2
5+
}
6+
def unapply(using f1: Foo)(using f2: Foo)(i: Int)(using f3: Foo): Option[Int] =
7+
if i == 0 then Some(f1.m1(i1) + f3.m1(i1)) else Some(f2.m2(i) + f3.m2(i))
8+
69
given Foo = new Foo
710
val i1: Int = 0
8-
val i2: Int = (i1, i1) match
11+
val i2: Int = i1 match
912
case Bar(i) => i
1013
case _ => 0
1114
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
class Foo
2-
31
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))
2+
class Foo {
3+
def m1(seq: Seq[Int]) = 0 +: seq
4+
def m2(seq: Seq[Int]) = i1 +: seq
5+
}
6+
def unapplySeq(using f1: Foo)(using f2: Foo)(seqi: Seq[Int])(using f3: Foo): Option[Seq[Int]] =
7+
if seqi(0) == 0 then Some(f1.m1(seqi)) else Some(f2.m2(seqi))
8+
69
given Foo = new Foo
710
val i1: Int = 0
8-
val i2: Int = (i1, i1) match
11+
val i2: Int = Seq(i1) match
912
case Bar(i) => i
1013
case _ => 0
1114
}

0 commit comments

Comments
 (0)