Skip to content

Add checks for value classes #1092

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 1 commit into from
Feb 17, 2016
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
14 changes: 13 additions & 1 deletion src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,19 @@ object RefChecks {
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)
ctx.error(s"value class may not be a ${if (clazz.owner.isTerm) "local class" else "member of another class"}", clazz.pos)
else {
val clParamAccessors = clazz.asClass.paramAccessors.filter(sym => sym.isTerm && !sym.is(Method))
clParamAccessors match {
case List(param) =>
if (param.is(Mutable))
ctx.error("value class parameter must not be a var", param.pos)
if (param.is(PrivateLocal))
ctx.error("value class parameter must not be private[this]", param.pos)
case _ =>
ctx.error("value class needs to have exactly one val parameter", clazz.pos)
}
}
stats.foreach(checkValueClassMember)
}
}
Expand Down
1 change: 1 addition & 0 deletions test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class tests extends CompilerTest {
@Test def neg_validateRefchecks = compileFile(negDir, "validate-refchecks", xerrors = 2)
@Test def neg_skolemize = compileFile(negDir, "skolemize", xerrors = 2)
@Test def neg_nested_bounds = compileFile(negDir, "nested_bounds", xerrors = 1)
@Test def neg_valueClasses = compileFile(negDir, "valueClasses", xerrors = 4)

@Test def run_all = runFiles(runDir)

Expand Down
10 changes: 10 additions & 0 deletions tests/neg/valueClasses.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class A1 {
class A2(x: Int) extends AnyVal // error: value class may not be a member of another class
}
class B1 {
def test = {
class B2(x: Int) extends AnyVal // error: value class may not be a local class
}
}
class C(private[this] val u: Int) extends AnyVal // error: value class parameter must not be private[this]
class D(u: Int) extends AnyVal // error: value class parameter must not be private[this]