From fa55e4f0f8d2834becdb9a9aef9c3ea65cb31fee Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 5 Mar 2012 16:07:51 +0100 Subject: Added check that primary constructor of a value class must be public. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 7f6e253045..20616e7af2 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1195,23 +1195,26 @@ trait Typers extends Modes with Adaptations with PatMatVirtualiser { private def validateDerivedValueClass(clazz: Symbol, body: List[Tree]) = { if (clazz.isTrait) - unit.error(clazz.pos, "Only classes (not traits) are allowed to extend AnyVal") + unit.error(clazz.pos, "only classes (not traits) are allowed to extend AnyVal") if (!clazz.isStatic) - unit.error(clazz.pos, "Value class may not be a "+ + unit.error(clazz.pos, "value class may not be a "+ (if (clazz.owner.isTerm) "local class" else "member of another class")) + val constr = clazz.primaryConstructor + if ((constr hasFlag (PRIVATE | PROTECTED)) || constr.privateWithin != NoSymbol) + unit.error(constr.pos, "value class must have public primary constructor") clazz.info.decls.toList.filter(acc => acc.isMethod && (acc hasFlag PARAMACCESSOR)) match { case List(acc) => def isUnderlyingAcc(sym: Symbol) = sym == acc || acc.hasAccessorFlag && sym == acc.accessed if (acc.accessBoundary(clazz) != RootClass) - unit.error(acc.pos, "Value class needs to have a publicly accessible val parameter") + unit.error(acc.pos, "value class needs to have a publicly accessible val parameter") for (stat <- body) if (!treeInfo.isAllowedInUniversalTrait(stat) && !isUnderlyingAcc(stat.symbol)) unit.error(stat.pos, - if (stat.symbol hasFlag PARAMACCESSOR) "Illegal parameter for value class" - else "This statement is not allowed in value class: "+stat) + if (stat.symbol hasFlag PARAMACCESSOR) "illegal parameter for value class" + else "this statement is not allowed in value class: "+stat) case x => - unit.error(clazz.pos, "Value class needs to have exactly one public val parameter") + unit.error(clazz.pos, "value class needs to have exactly one public val parameter") } for (tparam <- clazz.typeParams) if (tparam hasAnnotation definitions.SpecializedClass) -- cgit v1.2.3