Skip to content
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

Elide companion defs to a object extending AnyVal #18451

Merged
merged 1 commit into from
Oct 18, 2023
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ object desugar {
}
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
companionDefs(anyRef, companionMembers)
else if (isValueClass)
else if isValueClass && !isObject then
companionDefs(anyRef, Nil)
else Nil

Expand Down
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 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 Down
9 changes: 9 additions & 0 deletions tests/neg/i18274.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- [E068] Syntax Error: tests/neg/i18274.scala:3:7 ---------------------------------------------------------------------
3 |object Foo extends AnyVal // error
| ^
| object Foo cannot extend AnyVal
|---------------------------------------------------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Only classes (not objects) are allowed to extend AnyVal.
---------------------------------------------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions tests/neg/i18274.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//> using options -explain

object Foo extends AnyVal // error
Loading