Skip to content

Value class related checks #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ErrorReporting._
import tpd.ListOfTreeDecorator
import config.Printers._
import Annotations._
import transform.ValueClasses._
import language.implicitConversions

trait NamerContextOps { this: Context =>
Expand Down Expand Up @@ -580,6 +581,7 @@ class Namer { typer: Typer =>
index(rest)(inClassContext(selfInfo))
denot.info = ClassInfo(cls.owner.thisType, cls, parentRefs, decls, selfInfo)
addAnnotations(denot)
if (isDerivedValueClass(cls)) cls.setFlag(Final)
cls.setApplicableFlags(
(NoInitsInterface /: impl.body)((fs, stat) => fs & defKind(stat)))
}
Expand Down
35 changes: 28 additions & 7 deletions src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ object RefChecks {
}
}

/** Check that self type of this class conforms to self types of parents */
private def checkSelfType(clazz: Symbol)(implicit ctx: Context): Unit = clazz.info match {
/** Check that final and sealed restrictions on class parents
* and that self type of this class conforms to self types of parents.
*/
private def checkParents(clazz: Symbol)(implicit ctx: Context): Unit = clazz.info match {
case cinfo: ClassInfo =>
for (parent <- cinfo.classParents) {
val pclazz = parent.classSymbol
if (pclazz.is(Final))
ctx.error(d"cannot extend final $pclazz", clazz.pos)
if (pclazz.is(Sealed) && pclazz.associatedFile != clazz.associatedFile)
ctx.error(d"cannot extend sealed $pclazz in different compilation unit", clazz.pos)
val pself = parent.givenSelfType.asSeenFrom(clazz.thisType, parent.classSymbol)
if (pself.exists && !(cinfo.selfType <:< pself))
ctx.error(d"illegal inheritance: self type ${cinfo.selfType} of $clazz does not conform to self type $pself of parent ${parent.classSymbol}", clazz.pos)
Expand Down Expand Up @@ -648,13 +655,27 @@ object RefChecks {
}

/** Verify classes extending AnyVal meet the requirements */
private def checkAnyValSubclass(clazz: Symbol)(implicit ctx: Context) =
private def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context) = {
def checkValueClassMember(stat: Tree) = stat match {
case _: ValDef if !stat.symbol.is(ParamAccessor) =>
ctx.error(s"value class may not define non-parameter field", stat.pos)
case _: DefDef if stat.symbol.isConstructor =>
ctx.error(s"value class may not define secondary constructor", stat.pos)
case _: MemberDef | _: Import | EmptyTree =>
// ok
case _ =>
ctx.error(s"value class may not contain initialization statements", stat.pos)
}
if (isDerivedValueClass(clazz)) {
if (clazz.is(Trait))
ctx.error("Only classes (not traits) are allowed to extend AnyVal", clazz.pos)
else if (clazz.is(Abstract))
if (clazz.is(Abstract))
ctx.error("`abstract' modifier cannot be used with value classes", clazz.pos)
if (!clazz.isStatic)
ctx.error("value class cannot be an inner class", clazz.pos)
stats.foreach(checkValueClassMember)
}
}

type LevelAndIndex = immutable.Map[Symbol, (LevelInfo, Int)]

Expand Down Expand Up @@ -701,7 +722,7 @@ import RefChecks._
* - only one overloaded alternative defines default arguments
* - applyDynamic methods are not overloaded
* - all overrides conform to rules laid down by `checkAllOverrides`.
* - any value classes conform to rules laid down by `checkAnyValSubClass`.
* - any value classes conform to rules laid down by `checkDerivedValueClass`.
* - this(...) constructor calls do not forward reference other definitions in their block (not even lazy vals).
* - no forward reference in a local block jumps over a non-lazy val definition.
* - a class and its companion object do not both define a class or module with the same name.
Expand Down Expand Up @@ -768,10 +789,10 @@ class RefChecks extends MiniPhase { thisTransformer =>
override def transformTemplate(tree: Template)(implicit ctx: Context, info: TransformerInfo) = {
val cls = ctx.owner
checkOverloadedRestrictions(cls)
checkSelfType(cls)
checkParents(cls)
checkCompanionNameClashes(cls)
checkAllOverrides(cls)
checkAnyValSubclass(cls)
checkDerivedValueClass(cls, tree.body)
tree
}

Expand Down
2 changes: 2 additions & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class tests extends CompilerTest {
@Test def neg_i0248_inherit_refined = compileFile(negDir, "i0248-inherit-refined", xerrors = 4)
@Test def neg_i0281 = compileFile(negDir, "i0281-null-primitive-conforms", xerrors = 3)
@Test def neg_i583 = compileFile(negDir, "i0583-skolemize", xerrors = 2)
@Test def neg_finalSealed = compileFile(negDir, "final-sealed", xerrors = 2)
@Test def neg_i705 = compileFile(negDir, "i705-inner-value-class", xerrors = 7)
@Test def neg_moduleSubtyping = compileFile(negDir, "moduleSubtyping", xerrors = 4)
@Test def neg_escapingRefs = compileFile(negDir, "escapingRefs", xerrors = 2)
@Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/final-sealed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
final class A
class B extends A
class C extends Option[Int]

22 changes: 22 additions & 0 deletions tests/neg/i705-inner-value-class.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Foo {
class B(val a: Int) extends AnyVal // error
}

class VCwithBadMembers(val a: Int) extends AnyVal {
def this() = this(1) // error
var x = 0 // error
val y = 2 // error
println("hi") // error
}

object Test {
class B(val a: Int) extends AnyVal // ok
def f = {
class C(val a: Int) extends AnyVal // error
new C(1)
}
class B1(val b: Int) extends B(b)
// class D extends B( { class E(val a: Int) extends AnyVal; new E(1) } ) // error
}


1 change: 0 additions & 1 deletion tests/pos/extmethods.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
trait That1[A]
class T[A, This <: That1[A]](val x: Int) extends AnyVal {
self: This =>
var next: This = _
final def loop(x: This, cnt: Int): Int = loop(x, cnt + 1)
def const[B](): Boolean = return true
}
Expand Down