Skip to content

Commit be65db4

Browse files
authored
Merge pull request #15182 from dotty-staging/mirrors-test-backcompat
test backwards compat of mirror changes
2 parents fd6b48f + 2a183f8 commit be65db4

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import scala.deriving.Mirror
2+
3+
package lib {
4+
object NewMirrors {
5+
val mFoo = summon[Mirror.Of[Foo]] // we can access the constructor of Foo here.
6+
val mFooObj = summon[Mirror.Of[Foo.type]]
7+
8+
object SubBar extends Bar(1) {
9+
val mBar = summon[deriving.Mirror.ProductOf[Bar]]
10+
val mBarObj = summon[deriving.Mirror.ProductOf[Bar.type]]
11+
}
12+
}
13+
}
14+
15+
package app {
16+
object Main:
17+
18+
def testFoo(): Unit = {
19+
val oldMirrorFoo: Mirror.ProductOf[lib.Foo] = lib.OldMirrors.mFoo
20+
val oldMirrorFooObj: Mirror.ProductOf[lib.Foo.type] = lib.OldMirrors.mFooObj
21+
22+
assert(oldMirrorFoo eq oldMirrorFooObj) // - not good as oldMirrorFoo is really the mirror for `Foo.type`
23+
assert(oldMirrorFooObj eq lib.Foo) // - object Foo is its own mirror
24+
25+
// 3.1 bug: mirror for Foo behaves as mirror for Foo.type
26+
assert(oldMirrorFooObj.fromProduct(EmptyTuple) == lib.Foo)
27+
28+
val newMirrorFoo: Mirror.ProductOf[lib.Foo] = lib.NewMirrors.mFoo
29+
val newMirrorFooObj: Mirror.ProductOf[lib.Foo.type] = lib.NewMirrors.mFooObj
30+
31+
assert(oldMirrorFooObj eq newMirrorFooObj) // mirror for Foo.type has not changed.
32+
33+
assert(newMirrorFoo ne lib.Foo) // anonymous mirror for Foo
34+
assert(newMirrorFoo.fromProduct(Tuple(23)).x == 23) // mirror for Foo behaves as expected
35+
}
36+
37+
def testBar(): Unit = {
38+
val oldMirrorBar: Mirror.ProductOf[lib.Bar] = lib.OldMirrors.SubBar.mBar
39+
val oldMirrorBarObj: Mirror.ProductOf[lib.Bar.type] = lib.OldMirrors.SubBar.mBarObj
40+
41+
assert(oldMirrorBar eq oldMirrorBarObj) // - not good as oldMirrorBar is really the mirror for `Bar.type`
42+
assert(oldMirrorBarObj eq lib.Bar) // - object Bar is its own mirror
43+
44+
// 3.1 bug: mirror for Bar behaves as mirror for Bar.type
45+
assert(oldMirrorBarObj.fromProduct(EmptyTuple) == lib.Bar)
46+
47+
val newMirrorBar: Mirror.ProductOf[lib.Bar] = lib.NewMirrors.SubBar.mBar
48+
val newMirrorBarObj: Mirror.ProductOf[lib.Bar.type] = lib.NewMirrors.SubBar.mBarObj
49+
50+
assert(oldMirrorBarObj eq newMirrorBarObj) // mirror for Bar.type has not changed.
51+
52+
assert(newMirrorBar ne lib.Bar) // anonymous mirror for Bar
53+
assert(newMirrorBar.fromProduct(Tuple(23)).x == 23) // mirror for Bar behaves as expected
54+
}
55+
56+
def main(args: Array[String]): Unit =
57+
testFoo()
58+
testBar()
59+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
lazy val lib = project.in(file("lib"))
2+
.settings(
3+
scalaVersion := "3.1.1"
4+
)
5+
6+
lazy val app = project.in(file("app"))
7+
.dependsOn(lib)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package lib
2+
3+
case class Foo private[lib] (x: Int)
4+
5+
// case object Foo is its own mirror, so the mirror for Foo will be anonymous.
6+
case object Foo
7+
8+
9+
case class Bar protected[lib] (x: Int)
10+
11+
// case object Bar is its own mirror, so the mirror for Bar will be anonymous.
12+
case object Bar
13+
14+
object OldMirrors {
15+
val mFoo = summon[deriving.Mirror.ProductOf[Foo]]
16+
val mFooObj = summon[deriving.Mirror.ProductOf[Foo.type]]
17+
18+
object SubBar extends Bar(1) {
19+
val mBar = summon[deriving.Mirror.ProductOf[Bar]]
20+
val mBarObj = summon[deriving.Mirror.ProductOf[Bar.type]]
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion")
10+
)
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> app/run

0 commit comments

Comments
 (0)