Skip to content

Commit

Permalink
Merge pull request #31 from mbovel/mb/object-anyref
Browse files Browse the repository at this point in the history
Use CannotExtendAnyVal errors when an object extends AnyRef
  • Loading branch information
nicolasstucki authored Oct 18, 2023
2 parents 216428b + 8bb3c72 commit 577dd8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ extends Message(PatternMatchExhaustivityID) {

val pathes = List(
ActionPatch(
srcPos = endPos,
srcPos = endPos,
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
.mkString("\n", "\n", "")
),
Expand Down Expand Up @@ -1678,10 +1678,15 @@ class CannotExtendAnyVal(sym: Symbol)(using Context)
extends SyntaxMsg(CannotExtendAnyValID) {
def msg(using Context) = i"""$sym cannot extend ${hl("AnyVal")}"""
def explain(using Context) =
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
|"""
if sym.is(Trait) then
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
|"""
else if sym.is(Module) then
i"""Only classes (not objects) are allowed to extend ${hl("AnyVal")}.
|"""
else ""
}

class CannotExtendJavaEnum(sym: Symbol)(using Context)
Expand Down
7 changes: 3 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ object Checking {
if (isDerivedValueClass(clazz)) {
if (clazz.is(Trait))
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
if clazz.is(Module) then
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
if (clazz.is(Abstract))
report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos)
if (!clazz.isStatic)
Expand All @@ -738,10 +740,7 @@ object Checking {
for (p <- params if !p.is(Erased))
report.error("value class can only have one non `erased` parameter", p.srcPos)
case Nil =>
if clazz.is(Module) then
report.error("A module cannot extends AnyVal",clazz.srcPos)
else
report.error(ValueClassNeedsOneValParam(clazz), clazz.srcPos)
report.error(ValueClassNeedsOneValParam(clazz), clazz.srcPos)
}
}
stats.foreach(checkValueClassMember)
Expand Down
11 changes: 8 additions & 3 deletions tests/neg/i18274.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
-- Error: tests/neg/i18274.scala:1:7 -----------------------------------------------------------------------------------
1 |object Foo extends AnyVal // error
-- [E068] Syntax Error: tests/neg/i18274.scala:3:7 ---------------------------------------------------------------------
3 |object Foo extends AnyVal // error
| ^
| A module cannot extends AnyVal
| object Foo cannot extend AnyVal
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Only classes (not objects) are allowed to extend AnyVal.
---------------------------------------------------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions tests/neg/i18274.scala
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
//> using options -explain

object Foo extends AnyVal // error

0 comments on commit 577dd8f

Please sign in to comment.