Skip to content

Commit 257bf52

Browse files
committed
Check that value classes are static
1 parent b0084da commit 257bf52

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,14 @@ object RefChecks {
655655
}
656656

657657
/** Verify classes extending AnyVal meet the requirements */
658-
private def checkAnyValSubclass(clazz: Symbol)(implicit ctx: Context) =
658+
private def checkDerivedValueClass(clazz: Symbol)(implicit ctx: Context) =
659659
if (isDerivedValueClass(clazz)) {
660660
if (clazz.is(Trait))
661661
ctx.error("Only classes (not traits) are allowed to extend AnyVal", clazz.pos)
662-
else if (clazz.is(Abstract))
662+
if (clazz.is(Abstract))
663663
ctx.error("`abstract' modifier cannot be used with value classes", clazz.pos)
664+
if (!clazz.isStatic)
665+
ctx.error("value class cannot be an inner class", clazz.pos)
664666
}
665667

666668
type LevelAndIndex = immutable.Map[Symbol, (LevelInfo, Int)]
@@ -708,7 +710,7 @@ import RefChecks._
708710
* - only one overloaded alternative defines default arguments
709711
* - applyDynamic methods are not overloaded
710712
* - all overrides conform to rules laid down by `checkAllOverrides`.
711-
* - any value classes conform to rules laid down by `checkAnyValSubClass`.
713+
* - any value classes conform to rules laid down by `checkDerivedValueClass`.
712714
* - this(...) constructor calls do not forward reference other definitions in their block (not even lazy vals).
713715
* - no forward reference in a local block jumps over a non-lazy val definition.
714716
* - a class and its companion object do not both define a class or module with the same name.
@@ -778,7 +780,7 @@ class RefChecks extends MiniPhase { thisTransformer =>
778780
checkParents(cls)
779781
checkCompanionNameClashes(cls)
780782
checkAllOverrides(cls)
781-
checkAnyValSubclass(cls)
783+
checkDerivedValueClass(cls)
782784
tree
783785
}
784786

test/dotc/tests.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class tests extends CompilerTest {
133133
@Test def neg_i0248_inherit_refined = compileFile(negDir, "i0248-inherit-refined", xerrors = 4)
134134
@Test def neg_i0281 = compileFile(negDir, "i0281-null-primitive-conforms", xerrors = 3)
135135
@Test def neg_i583 = compileFile(negDir, "i0583-skolemize", xerrors = 2)
136+
@Test def neg_finalSealed = compileFile(negDir, "final-sealed", xerrors = 2)
137+
@Test def neg_i705 = compileFile(negDir, "i705-inner-value-class", xerrors = 3)
136138
@Test def neg_moduleSubtyping = compileFile(negDir, "moduleSubtyping", xerrors = 4)
137139
@Test def neg_escapingRefs = compileFile(negDir, "escapingRefs", xerrors = 2)
138140
@Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Foo {
2+
class B(val a: Int) extends AnyVal // error
3+
}
4+
5+
object Test {
6+
class B(val a: Int) extends AnyVal // ok
7+
def f = {
8+
class C(val a: Int) extends AnyVal // error
9+
new C(1)
10+
}
11+
class B1(val b: Int) extends B(b)
12+
// class D extends B( { class E(val a: Int) extends AnyVal; new E(1) } ) // error
13+
}
14+
15+

0 commit comments

Comments
 (0)