Skip to content

Commit bbeb0ab

Browse files
nicolasstuckimbovel
andcommitted
Elide companion defs to a object extending AnyVal
Co-authored-by: Matt Bovel <[email protected]>
1 parent 3783220 commit bbeb0ab

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ object desugar {
820820
}
821821
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
822822
companionDefs(anyRef, companionMembers)
823-
else if (isValueClass)
823+
else if isValueClass && !isObject then
824824
companionDefs(anyRef, Nil)
825825
else Nil
826826

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -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

+2
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)

tests/neg/i18274.check

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- [E068] Syntax Error: tests/neg/i18274.scala:3:7 ---------------------------------------------------------------------
2+
3 |object Foo extends AnyVal // error
3+
| ^
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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//> using options -explain
2+
3+
object Foo extends AnyVal // error

0 commit comments

Comments
 (0)