Skip to content

Commit 42579af

Browse files
committed
Use correct context for val RHS
1 parent 096e7fa commit 42579af

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Deriving.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ trait Deriving { this: Typer =>
5555
// derived instance at the summoning site.
5656

5757
val (lazyOrMethod, rhsType) = info match {
58-
case ExprType(rhsType) => (Lazy, rhsType)
58+
case ExprType(rhsType) => (Final | Lazy | StableRealizable, rhsType)
5959
case _ => (Method, info)
6060
}
6161

@@ -177,7 +177,7 @@ trait Deriving { this: Typer =>
177177
typed(rhs, resultType)
178178
}
179179

180-
def typeclassValInstance(sym: Symbol): Tree = {
180+
def typeclassValInstance(sym: Symbol)(implicit ctx: Context): Tree = {
181181
val rhsType = sym.info
182182
val module = untpd.ref(companionRef(rhsType)).withSpan(sym.span)
183183
val rhs = untpd.Select(module, nme.derived)
@@ -193,11 +193,12 @@ trait Deriving { this: Typer =>
193193
}
194194

195195
def syntheticDef(sym: Symbol): Tree = {
196+
val localCtx = ctx.fresh.setOwner(sym).setNewScope
196197
sym.info match {
197198
case _: MethodicType =>
198-
tpd.polyDefDef(sym.asTerm, typeclassDefInstance(sym)(ctx.fresh.setOwner(sym).setNewScope))
199+
tpd.polyDefDef(sym.asTerm, typeclassDefInstance(sym)(localCtx))
199200
case _ =>
200-
typeclassValInstance(sym)
201+
typeclassValInstance(sym)(localCtx)
201202
}
202203
}
203204

tests/run/lazy-derives.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import scala.deriving._
22

33
object Test extends App {
4-
case class Mono(i: Int) derives Foo, Bar
5-
case class Poly[T](t: T) derives Foo, Bar, Baz
4+
case class Mono(i: Int) derives Foo, Bar, Inline
5+
case class Poly[T](t: T) derives Foo, Bar, Baz, Inline
66

77
trait Quux[T]
88
object Quux {
@@ -26,13 +26,21 @@ object Test extends App {
2626
def derived[F[_]] given (m: Mirror { type MirroredType = F }): Baz[F] = new Baz[F] {}
2727
}
2828

29+
trait Inline[T]
30+
object Inline {
31+
given as Inline[Int] {}
32+
inline def derived[T] given (m: Mirror.Of[T]): Inline[T] = new Inline[T] {}
33+
}
34+
2935
// Unfortunately these tests doesn't distinguish a lazy val
3036
// from a cached def
3137
assert(the[Foo[Mono]] eq the[Foo[Mono]])
3238
assert(the[Bar[Mono]] eq the[Bar[Mono]])
3339
assert(the[Baz[Poly]] eq the[Baz[Poly]])
40+
assert(the[Inline[Mono]] eq the[Inline[Mono]])
3441

3542
// These have term arguments so should be distinct
3643
assert(the[Foo[Poly[Int]]] ne the[Foo[Poly[Int]]])
3744
assert(the[Bar[Poly[Int]]] ne the[Bar[Poly[Int]]])
45+
assert(the[Inline[Poly[Int]]] ne the[Inline[Poly[Int]]])
3846
}

0 commit comments

Comments
 (0)