Skip to content

Commit 577dd8f

Browse files
Merge pull request #31 from mbovel/mb/object-anyref
Use CannotExtendAnyVal errors when an object extends AnyRef
2 parents 216428b + 8bb3c72 commit 577dd8f

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ extends Message(PatternMatchExhaustivityID) {
876876

877877
val pathes = List(
878878
ActionPatch(
879-
srcPos = endPos,
879+
srcPos = endPos,
880880
replacement = uncoveredCases.map(c => indent(s"case $c => ???", startColumn))
881881
.mkString("\n", "\n", "")
882882
),
@@ -1678,10 +1678,15 @@ class CannotExtendAnyVal(sym: Symbol)(using Context)
16781678
extends SyntaxMsg(CannotExtendAnyValID) {
16791679
def msg(using Context) = i"""$sym cannot extend ${hl("AnyVal")}"""
16801680
def explain(using Context) =
1681-
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
1682-
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
1683-
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
1684-
|"""
1681+
if sym.is(Trait) then
1682+
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
1683+
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
1684+
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
1685+
|"""
1686+
else if sym.is(Module) then
1687+
i"""Only classes (not objects) are allowed to extend ${hl("AnyVal")}.
1688+
|"""
1689+
else ""
16851690
}
16861691

16871692
class CannotExtendJavaEnum(sym: Symbol)(using Context)

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,8 @@ object Checking {
716716
if (isDerivedValueClass(clazz)) {
717717
if (clazz.is(Trait))
718718
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
719+
if clazz.is(Module) then
720+
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
719721
if (clazz.is(Abstract))
720722
report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos)
721723
if (!clazz.isStatic)
@@ -738,10 +740,7 @@ object Checking {
738740
for (p <- params if !p.is(Erased))
739741
report.error("value class can only have one non `erased` parameter", p.srcPos)
740742
case Nil =>
741-
if clazz.is(Module) then
742-
report.error("A module cannot extends AnyVal",clazz.srcPos)
743-
else
744-
report.error(ValueClassNeedsOneValParam(clazz), clazz.srcPos)
743+
report.error(ValueClassNeedsOneValParam(clazz), clazz.srcPos)
745744
}
746745
}
747746
stats.foreach(checkValueClassMember)

tests/neg/i18274.check

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
-- Error: tests/neg/i18274.scala:1:7 -----------------------------------------------------------------------------------
2-
1 |object Foo extends AnyVal // error
1+
-- [E068] Syntax Error: tests/neg/i18274.scala:3:7 ---------------------------------------------------------------------
2+
3 |object Foo extends AnyVal // error
33
| ^
4-
| A module cannot extends AnyVal
4+
| object Foo cannot extend AnyVal
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| Only classes (not objects) are allowed to extend AnyVal.
9+
---------------------------------------------------------------------------------------------------------------------

tests/neg/i18274.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
//> using options -explain
2+
13
object Foo extends AnyVal // error

0 commit comments

Comments
 (0)