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

Cannot inherit final override toString in companion object of case class #16879

Closed
CoolDalek opened this issue Feb 10, 2023 · 2 comments · Fixed by #16890
Closed

Cannot inherit final override toString in companion object of case class #16879

CoolDalek opened this issue Feb 10, 2023 · 2 comments · Fixed by #16890

Comments

@CoolDalek
Copy link

CoolDalek commented Feb 10, 2023

Compiler version

3.2.1 and 3.2.2

Minimized code

trait Companion:
  final override def toString: String = "Companion"

case class Example(value: Int)
object Example extends Companion

Output

error overriding method toString in trait Companion of type => String;
  method toString of type => String cannot override final member method toString in trait Companion
case class Example(value: Int)

Expectation

Companion object inherits toString implementation from the trait.

@CoolDalek CoolDalek added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 10, 2023
@jchyb jchyb added area:transform and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 10, 2023
@odersky
Copy link
Contributor

odersky commented Feb 11, 2023

This is caused by #9165. I think #9165 should be refined to match Scala 2's behavior.

@odersky
Copy link
Contributor

odersky commented Feb 11, 2023

Another problem case:

case class Example(value: Int)
object Example {
  def toString = "Ex"
}

gives

-- [E120] Naming Error: i16879.scala:8:6 ---------------------------------------
8 |  def toString = "Ex"
  |      ^
  |Double definition:
  |override def toString: String in object Example at line 6 and
  |def toString: String in object Example at line 8
  |have the same type after erasure.
  |
  |Consider adding a @targetName annotation to one of the conflicting definitions
  |for disambiguation.

odersky added a commit to dotty-staging/dotty that referenced this issue Feb 11, 2023
Don't generate a toString method if the companion is explicitly given.
This matches Scala 2's behavior.

Fixes scala#16879
odersky added a commit that referenced this issue Feb 19, 2023
Don't generate a toString method if the companion is explicitly given.
This matches Scala 2's behavior.

Fixes #16879
@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants