Skip to content

Commit 769ca3b

Browse files
authored
Generate toString only for synthetic companions of case classes (#16890)
Don't generate a toString method if the companion is explicitly given. This matches Scala 2's behavior. Fixes #16879
2 parents 3e18fee + afd499b commit 769ca3b

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,11 @@ class Namer { typer: Typer =>
541541
res = cpy.TypeDef(modCls)(
542542
rhs = cpy.Template(modTempl)(
543543
derived = if (fromTempl.derived.nonEmpty) fromTempl.derived else modTempl.derived,
544-
body = fromTempl.body ++ modTempl.body))
544+
body = fromTempl.body.filter {
545+
case stat: DefDef => stat.name != nme.toString_
546+
// toString should only be generated if explicit companion is missing
547+
case _ => true
548+
} ++ modTempl.body))
545549
if (fromTempl.derived.nonEmpty) {
546550
if (modTempl.derived.nonEmpty)
547551
report.error(em"a class and its companion cannot both have `derives` clauses", mdef.srcPos)

tests/run/i16879.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Companion:
2+
final override def toString: String = "Companion"
3+
4+
case class Example(value: Int)
5+
object Example extends Companion
6+
7+
case class C()
8+
object C:
9+
override def toString = "CC"
10+
11+
case class D()
12+
13+
@main def Test =
14+
assert(Example.toString == "Companion")
15+
assert(C.toString == "CC")
16+
assert(D.toString == "D")

tests/semanticdb/metac.expect

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ Schema => SemanticDB v4
467467
Uri => Classes.scala
468468
Text => empty
469469
Language => Scala
470-
Symbols => 109 entries
470+
Symbols => 108 entries
471471
Occurrences => 114 entries
472472
Synthetics => 2 entries
473473

@@ -504,10 +504,9 @@ classes/C4#copy$default$1(). => method copy$default$1 => Int @uncheckedVariance
504504
classes/C4#copy(). => method copy (param x: Int): C4
505505
classes/C4#copy().(x) => param x: Int
506506
classes/C4#x. => val method x Int
507-
classes/C4. => final object C4 extends Object { self: C4.type => +4 decls }
507+
classes/C4. => final object C4 extends Object { self: C4.type => +3 decls }
508508
classes/C4.apply(). => method apply (param x: Int): C4
509509
classes/C4.apply().(x) => param x: Int
510-
classes/C4.toString(). => method toString => String <: scala/Any#toString().
511510
classes/C4.unapply(). => method unapply (param x$1: C4): C4
512511
classes/C4.unapply().(x$1) => param x$1: C4
513512
classes/C6# => case class C6 extends Object with Product with Serializable { self: C6 => +5 decls }
@@ -4559,7 +4558,7 @@ Schema => SemanticDB v4
45594558
Uri => semanticdb-Types.scala
45604559
Text => empty
45614560
Language => Scala
4562-
Symbols => 144 entries
4561+
Symbols => 143 entries
45634562
Occurrences => 228 entries
45644563
Synthetics => 1 entries
45654564

@@ -4585,10 +4584,9 @@ types/Foo#copy$default$1(). => method copy$default$1 => "abc" @uncheckedVariance
45854584
types/Foo#copy(). => method copy (param s: "abc"): Foo
45864585
types/Foo#copy().(s) => param s: "abc"
45874586
types/Foo#s. => val method s "abc"
4588-
types/Foo. => final object Foo extends Object { self: Foo.type => +6 decls }
4587+
types/Foo. => final object Foo extends Object { self: Foo.type => +5 decls }
45894588
types/Foo.apply(). => method apply (param s: "abc"): Foo
45904589
types/Foo.apply().(s) => param s: "abc"
4591-
types/Foo.toString(). => method toString => String <: scala/Any#toString().
45924590
types/Foo.unapply(). => method unapply (param x$1: Foo): Foo
45934591
types/Foo.unapply().(x$1) => param x$1: Foo
45944592
types/Foo.x. => val method x "abc" @deprecated

0 commit comments

Comments
 (0)