Skip to content

Commit a0851f6

Browse files
nicolasstuckiWojciechMazur
authored andcommitted
Elide companion defs to a object extending AnyVal
Co-authored-by: Matt Bovel <[email protected]> [Cherry-picked bbeb0ab]
1 parent 2046d25 commit a0851f6

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
@@ -770,7 +770,7 @@ object desugar {
770770
}
771771
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
772772
companionDefs(anyRef, companionMembers)
773-
else if (isValueClass)
773+
else if isValueClass && !isObject then
774774
companionDefs(anyRef, Nil)
775775
else Nil
776776

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1670,10 +1670,15 @@ class CannotExtendAnyVal(sym: Symbol)(using Context)
16701670
extends SyntaxMsg(CannotExtendAnyValID) {
16711671
def msg(using Context) = i"""$sym cannot extend ${hl("AnyVal")}"""
16721672
def explain(using Context) =
1673-
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
1674-
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
1675-
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
1676-
|"""
1673+
if sym.is(Trait) then
1674+
i"""Only classes (not traits) are allowed to extend ${hl("AnyVal")}, but traits may extend
1675+
|${hl("Any")} to become ${Green("\"universal traits\"")} which may only have ${hl("def")} members.
1676+
|Universal traits can be mixed into classes that extend ${hl("AnyVal")}.
1677+
|"""
1678+
else if sym.is(Module) then
1679+
i"""Only classes (not objects) are allowed to extend ${hl("AnyVal")}.
1680+
|"""
1681+
else ""
16771682
}
16781683

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ object Checking {
701701
if (isDerivedValueClass(clazz)) {
702702
if (clazz.is(Trait))
703703
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
704+
if clazz.is(Module) then
705+
report.error(CannotExtendAnyVal(clazz), clazz.srcPos)
704706
if (clazz.is(Abstract))
705707
report.error(ValueClassesMayNotBeAbstract(clazz), clazz.srcPos)
706708
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)