Skip to content

Commit 2b4e164

Browse files
committed
Fix #9161: Add toString to companion modules
1 parent 6149e92 commit 2b4e164

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,10 @@ object desugar {
719719
DefDef(methName, derivedTparams, (unapplyParam :: Nil) :: Nil, unapplyResTp, unapplyRHS)
720720
.withMods(synthetic)
721721
}
722-
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
722+
val toStringMeth =
723+
DefDef(nme.toString_, Nil, Nil, TypeTree(), Literal(Constant(className.toString))).withMods(Modifiers(Override | Synthetic))
724+
725+
companionDefs(companionParent, applyMeths ::: unapplyMeth :: toStringMeth :: companionMembers)
723726
}
724727
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
725728
companionDefs(anyRef, companionMembers)

tests/run-macros/tasty-extractors-2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
4949
Inlined(None, Nil, Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(DefDef("a", Nil, Nil, Inferred(), Some(Literal(Constant(0))))))), Literal(Constant(()))))
5050
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
5151

52-
Inlined(None, Nil, Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil), TypeSelect(Select(Ident("_root_"), "scala"), "Product"), TypeSelect(Select(Ident("_root_"), "scala"), "Serializable")), Nil, None, List(DefDef("copy", Nil, List(Nil), Inferred(), Some(Apply(Select(New(Inferred()), "<init>"), Nil))))), ValDef("Foo", TypeIdent("Foo$"), Some(Apply(Select(New(TypeIdent("Foo$")), "<init>"), Nil))), ClassDef("Foo$", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil), Applied(Inferred(), List(Inferred()))), Nil, Some(ValDef("_", Singleton(Ident("Foo")), None)), List(DefDef("apply", Nil, List(Nil), Inferred(), Some(Apply(Select(New(Inferred()), "<init>"), Nil))), DefDef("unapply", Nil, List(List(ValDef("x$1", Inferred(), None))), Singleton(Literal(Constant(true))), Some(Literal(Constant(true))))))), Literal(Constant(()))))
52+
Inlined(None, Nil, Block(List(ClassDef("Foo", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil), TypeSelect(Select(Ident("_root_"), "scala"), "Product"), TypeSelect(Select(Ident("_root_"), "scala"), "Serializable")), Nil, None, List(DefDef("copy", Nil, List(Nil), Inferred(), Some(Apply(Select(New(Inferred()), "<init>"), Nil))))), ValDef("Foo", TypeIdent("Foo$"), Some(Apply(Select(New(TypeIdent("Foo$")), "<init>"), Nil))), ClassDef("Foo$", DefDef("<init>", Nil, List(Nil), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil), Applied(Inferred(), List(Inferred()))), Nil, Some(ValDef("_", Singleton(Ident("Foo")), None)), List(DefDef("apply", Nil, List(Nil), Inferred(), Some(Apply(Select(New(Inferred()), "<init>"), Nil))), DefDef("unapply", Nil, List(List(ValDef("x$1", Inferred(), None))), Singleton(Literal(Constant(true))), Some(Literal(Constant(true)))), DefDef("toString", Nil, Nil, Inferred(), Some(Literal(Constant("Foo"))))))), Literal(Constant(()))))
5353
TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Unit")
5454

5555
Inlined(None, Nil, Block(List(ClassDef("Foo1", DefDef("<init>", Nil, List(List(ValDef("a", TypeIdent("Int"), None))), Inferred(), None), List(Apply(Select(New(Inferred()), "<init>"), Nil)), Nil, None, List(ValDef("a", Inferred(), None)))), Literal(Constant(()))))

tests/run/i9161.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
final case class T(i:Int)
3+
4+
@main def Test = assert(T.toString == "T")

tests/semanticdb/metac.expect

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Schema => SemanticDB v4
338338
Uri => Classes.scala
339339
Text => empty
340340
Language => Scala
341-
Symbols => 106 entries
341+
Symbols => 109 entries
342342
Occurrences => 130 entries
343343

344344
Symbols:
@@ -363,6 +363,7 @@ classes/C3#x. => val method x
363363
classes/C3. => final object C3
364364
classes/C3.apply(). => method apply
365365
classes/C3.apply().(x) => param x
366+
classes/C3.toString(). => method toString
366367
classes/C3.unapply(). => method unapply
367368
classes/C3.unapply().(x$1) => param x$1
368369
classes/C4# => case class C4
@@ -376,6 +377,7 @@ classes/C4#x. => val method x
376377
classes/C4. => final object C4
377378
classes/C4.apply(). => method apply
378379
classes/C4.apply().(x) => param x
380+
classes/C4.toString(). => method toString
379381
classes/C4.unapply(). => method unapply
380382
classes/C4.unapply().(x$1) => param x$1
381383
classes/C6# => case class C6
@@ -389,6 +391,7 @@ classes/C6#x. => val method x
389391
classes/C6. => final object C6
390392
classes/C6.apply(). => method apply
391393
classes/C6.apply().(x) => param x
394+
classes/C6.toString(). => method toString
392395
classes/C6.unapply(). => method unapply
393396
classes/C6.unapply().(x$1) => param x$1
394397
classes/C7# => class C7
@@ -638,7 +641,7 @@ Schema => SemanticDB v4
638641
Uri => Enums.scala
639642
Text => empty
640643
Language => Scala
641-
Symbols => 167 entries
644+
Symbols => 169 entries
642645
Occurrences => 203 entries
643646

644647
Symbols:
@@ -706,6 +709,7 @@ _empty_/Enums.Maybe.Just. => final object Just
706709
_empty_/Enums.Maybe.Just.apply(). => method apply
707710
_empty_/Enums.Maybe.Just.apply().(value) => param value
708711
_empty_/Enums.Maybe.Just.apply().[A] => typeparam A
712+
_empty_/Enums.Maybe.Just.toString(). => method toString
709713
_empty_/Enums.Maybe.Just.unapply(). => method unapply
710714
_empty_/Enums.Maybe.Just.unapply().(x$1) => param x$1
711715
_empty_/Enums.Maybe.Just.unapply().[A] => typeparam A
@@ -797,6 +801,7 @@ _empty_/Enums.`<:<`.Refl#ordinal(). => method ordinal
797801
_empty_/Enums.`<:<`.Refl. => final object Refl
798802
_empty_/Enums.`<:<`.Refl.apply(). => method apply
799803
_empty_/Enums.`<:<`.Refl.apply().[C] => typeparam C
804+
_empty_/Enums.`<:<`.Refl.toString(). => method toString
800805
_empty_/Enums.`<:<`.Refl.unapply(). => method unapply
801806
_empty_/Enums.`<:<`.Refl.unapply().(x$1) => param x$1
802807
_empty_/Enums.`<:<`.Refl.unapply().[C] => typeparam C
@@ -2075,7 +2080,7 @@ Schema => SemanticDB v4
20752080
Uri => NamedApplyBlock.scala
20762081
Text => empty
20772082
Language => Scala
2078-
Symbols => 45 entries
2083+
Symbols => 46 entries
20792084
Occurrences => 46 entries
20802085

20812086
Symbols:
@@ -2104,6 +2109,7 @@ example/NamedApplyBlockCaseClassConstruction.Msg.apply(). => method apply
21042109
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(body) => param body
21052110
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(head) => param head
21062111
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(tail) => param tail
2112+
example/NamedApplyBlockCaseClassConstruction.Msg.toString(). => method toString
21072113
example/NamedApplyBlockCaseClassConstruction.Msg.unapply(). => method unapply
21082114
example/NamedApplyBlockCaseClassConstruction.Msg.unapply().(x$1) => param x$1
21092115
example/NamedApplyBlockCaseClassConstruction.bodyText. => val method bodyText
@@ -2181,7 +2187,7 @@ Schema => SemanticDB v4
21812187
Uri => NamedArguments.scala
21822188
Text => empty
21832189
Language => Scala
2184-
Symbols => 15 entries
2190+
Symbols => 16 entries
21852191
Occurrences => 13 entries
21862192

21872193
Symbols:
@@ -2197,6 +2203,7 @@ example/NamedArguments#User#name. => val method name
21972203
example/NamedArguments#User. => final object User
21982204
example/NamedArguments#User.apply(). => method apply
21992205
example/NamedArguments#User.apply().(name) => param name
2206+
example/NamedArguments#User.toString(). => method toString
22002207
example/NamedArguments#User.unapply(). => method unapply
22012208
example/NamedArguments#User.unapply().(x$1) => param x$1
22022209
example/NamedArguments#`<init>`(). => primary ctor <init>
@@ -2434,7 +2441,7 @@ Schema => SemanticDB v4
24342441
Uri => Synthetic.scala
24352442
Text => empty
24362443
Language => Scala
2437-
Symbols => 37 entries
2444+
Symbols => 38 entries
24382445
Occurrences => 162 entries
24392446

24402447
Symbols:
@@ -2460,6 +2467,7 @@ example/Synthetic#s.Bar#`<init>`(). => primary ctor <init>
24602467
example/Synthetic#s.Bar#copy(). => method copy
24612468
example/Synthetic#s.Bar. => final object Bar
24622469
example/Synthetic#s.Bar.apply(). => method apply
2470+
example/Synthetic#s.Bar.toString(). => method toString
24632471
example/Synthetic#s.Bar.unapply(). => method unapply
24642472
example/Synthetic#s.Bar.unapply().(x$1) => param x$1
24652473
example/Synthetic#s.apply(). => method apply
@@ -3116,7 +3124,7 @@ Schema => SemanticDB v4
31163124
Uri => recursion.scala
31173125
Text => empty
31183126
Language => Scala
3119-
Symbols => 35 entries
3127+
Symbols => 36 entries
31203128
Occurrences => 56 entries
31213129

31223130
Symbols:
@@ -3148,6 +3156,7 @@ recursion/Nats.Succ. => final object Succ
31483156
recursion/Nats.Succ.apply(). => method apply
31493157
recursion/Nats.Succ.apply().(p) => param p
31503158
recursion/Nats.Succ.apply().[N] => typeparam N
3159+
recursion/Nats.Succ.toString(). => method toString
31513160
recursion/Nats.Succ.unapply(). => method unapply
31523161
recursion/Nats.Succ.unapply().(x$1) => param x$1
31533162
recursion/Nats.Succ.unapply().[N] => typeparam N
@@ -3403,7 +3412,7 @@ Schema => SemanticDB v4
34033412
Uri => semanticdb-Types.scala
34043413
Text => empty
34053414
Language => Scala
3406-
Symbols => 140 entries
3415+
Symbols => 142 entries
34073416
Occurrences => 250 entries
34083417

34093418
Symbols:
@@ -3431,6 +3440,7 @@ types/Foo#s. => val method s
34313440
types/Foo. => final object Foo
34323441
types/Foo.apply(). => method apply
34333442
types/Foo.apply().(s) => param s
3443+
types/Foo.toString(). => method toString
34343444
types/Foo.unapply(). => method unapply
34353445
types/Foo.unapply().(x$1) => param x$1
34363446
types/Foo.x. => val method x
@@ -3482,6 +3492,7 @@ types/Test.C#RepeatedType#s. => val method s
34823492
types/Test.C#RepeatedType. => final object RepeatedType
34833493
types/Test.C#RepeatedType.apply(). => method apply
34843494
types/Test.C#RepeatedType.apply().(s) => param s
3495+
types/Test.C#RepeatedType.toString(). => method toString
34853496
types/Test.C#RepeatedType.unapplySeq(). => method unapplySeq
34863497
types/Test.C#RepeatedType.unapplySeq().(x$1) => param x$1
34873498
types/Test.C#TypeType. => final object TypeType
@@ -3808,7 +3819,7 @@ Schema => SemanticDB v4
38083819
Uri => semanticdb-extract.scala
38093820
Text => empty
38103821
Language => Scala
3811-
Symbols => 17 entries
3822+
Symbols => 18 entries
38123823
Occurrences => 22 entries
38133824

38143825
Symbols:
@@ -3824,6 +3835,7 @@ _empty_/AnObject.Foo#x. => val method x
38243835
_empty_/AnObject.Foo. => final object Foo
38253836
_empty_/AnObject.Foo.apply(). => method apply
38263837
_empty_/AnObject.Foo.apply().(x) => param x
3838+
_empty_/AnObject.Foo.toString(). => method toString
38273839
_empty_/AnObject.Foo.unapply(). => method unapply
38283840
_empty_/AnObject.Foo.unapply().(x$1) => param x$1
38293841
_empty_/AnObject.foo(). => method foo

0 commit comments

Comments
 (0)