Skip to content

Commit c484dee

Browse files
committed
fix #10997: check accessible from sealed parent
1 parent a42fe92 commit c484dee

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,18 @@ object SymUtils:
9191
* It must satisfy the following conditions:
9292
* - it has at least one child class or object
9393
* - none of its children are anonymous classes
94-
* - all of its children are addressable through a path from its companion object
94+
* - all of its children are addressable through a path from the parent class
9595
* - all of its children are generic products or singletons
9696
*/
9797
def whyNotGenericSum(using Context): String =
9898
if (!self.is(Sealed))
9999
s"it is not a sealed ${self.kindString}"
100100
else {
101101
val children = self.children
102-
val companion = self.linkedClass
103102
def problem(child: Symbol) = {
104103

105104
def isAccessible(sym: Symbol): Boolean =
106-
companion.isContainedIn(sym) || sym.is(Module) && isAccessible(sym.owner)
105+
self.isContainedIn(sym) || sym.is(Module) && isAccessible(sym.owner)
107106

108107
if (child == self) "it has anonymous or inaccessible subclasses"
109108
else if (!isAccessible(child.owner)) i"its child $child is not accessible"

tests/neg/i10997.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sealed trait Parent
2+
3+
def Wrapper = {
4+
5+
case class Foo(x: Int, y: Int, s: String) extends Parent
6+
case class Bar(x: Int, y: Int) extends Parent
7+
8+
println(summon[deriving.Mirror.Of[Parent]]) // error
9+
}

tests/pos/i10997.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Test {
2+
3+
sealed trait Parent
4+
case class Foo(x: Int, y: Int, s: String) extends Parent
5+
case class Bar(x: Int, y: Int) extends Parent
6+
7+
println(summon[deriving.Mirror.Of[Parent]])
8+
}

tests/run/deriving-constructor-order.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Test extends App {
77
case _: T => ()
88
}
99

10-
sealed trait Base1
10+
sealed trait Base1 // Base1 MUST NOT have a companion here!
1111
case class Foo() extends Base1
1212
case object Bar extends Base1
1313
case class Qux(i: Int) extends Base1

tests/run/deriving.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ object T
44
case class A(x: Int, y: Int) extends T
55
case object B extends T
66

7-
sealed trait U
7+
sealed trait U // U MUST NOT have a companion here!
88
case class C() extends U
99

1010
object Test extends App {

0 commit comments

Comments
 (0)