Skip to content

Commit 44c1062

Browse files
committed
Disallow unsound &-type modifications
1 parent b6f89be commit 44c1062

2 files changed

Lines changed: 5 additions & 16 deletions

File tree

quicklens/src/main/scala-3/com/softwaremill/quicklens/QuicklensMacros.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ object QuicklensMacros {
157157
def poorMansLUB: TypeRepr = tpe match {
158158
case AndType(l, r) if l <:< r => l
159159
case AndType(l, r) if r <:< l => r
160+
case AndType(_, _) =>
161+
report.errorAndAbort(s"Implementation limitation: Cannot modify an & type with unrelated types.")
160162
case _ => tpe
161163
}
162164

quicklens/src/test/scala-3/com/softwaremill/quicklens/test/ModifyAndTypeTest.scala

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ object ModifyAndTypeTest {
99
case class A(a: Int) extends B
1010
trait B {
1111
def a: Int
12+
def b: Int = 0
1213
}
1314

1415
case class A1(a: Int)
@@ -26,6 +27,7 @@ class ModifyAndTypeTest extends AnyFlatSpec with Matchers {
2627

2728
val modified = ab.modify(_.a).setTo(1)
2829

30+
modified.b shouldBe 0
2931
modified.a shouldBe 1
3032
}
3133

@@ -34,22 +36,7 @@ class ModifyAndTypeTest extends AnyFlatSpec with Matchers {
3436

3537
val modified = ab.modify(_.a).setTo(1)
3638

37-
modified.a shouldBe 1
38-
}
39-
40-
it should "modify an & type object 2" in {
41-
val ab: B & A1 = new A1(0) with B
42-
43-
val modified = ab.modify(_.a).setTo(1)
44-
45-
modified.a shouldBe 1
46-
}
47-
48-
it should "modify an & type object 3" in {
49-
val ab: A1 & B = new A1(0) with B
50-
51-
val modified = ab.modify(_.a).setTo(1)
52-
39+
modified.b shouldBe 0
5340
modified.a shouldBe 1
5441
}
5542

0 commit comments

Comments
 (0)