Skip to content

Commit ab32f4c

Browse files
committed
Fix #1644: Disallow inner classes in value classes
1 parent 6e8933c commit ab32f4c

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ object Checking {
410410
/** Verify classes extending AnyVal meet the requirements */
411411
def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context) = {
412412
def checkValueClassMember(stat: Tree) = stat match {
413+
case _: TypeDef if stat.symbol.isClass =>
414+
ctx.error(s"value class may not define an inner class", stat.pos)
413415
case _: ValDef if !stat.symbol.is(ParamAccessor) =>
414416
ctx.error(s"value class may not define non-parameter field", stat.pos)
415417
case d: DefDef if d.symbol.isConstructor =>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class X1(val s: String) extends AnyVal {
2+
trait I2 { // error: value class may not define an inner class or trait
3+
val q: String
4+
def z = s + q
5+
}
6+
}
7+
8+
class X2(val s: String) extends AnyVal {
9+
private[this] class I2(val q: String) // error: value class may not define an inner class or trait
10+
11+
def y(i: Int) = {
12+
val i2 = new I2(i.toString)
13+
i2.q + s
14+
}
15+
}
16+
17+
class X3(val s: String) extends AnyVal {
18+
object I3 // error: value class may not define non-parameter field
19+
}

tests/untried/neg/valueclasses-impl-restrictions.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)