Skip to content

Commit de1ee0a

Browse files
committed
Fix #9161: Add toString to companion modules
1 parent 3dfba6d commit de1ee0a

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
@@ -699,7 +699,10 @@ object desugar {
699699
DefDef(methName, derivedTparams, (unapplyParam :: Nil) :: Nil, unapplyResTp, unapplyRHS)
700700
.withMods(synthetic)
701701
}
702-
companionDefs(companionParent, applyMeths ::: unapplyMeth :: companionMembers)
702+
val toStringMeth =
703+
DefDef(nme.toString_, Nil, Nil, TypeTree(), Literal(Constant(className.toString))).withMods(Modifiers(Override | Synthetic))
704+
705+
companionDefs(companionParent, applyMeths ::: unapplyMeth :: toStringMeth :: companionMembers)
703706
}
704707
else if (companionMembers.nonEmpty || companionDerived.nonEmpty || isEnum)
705708
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())), TypeSelect(Select(Ident("_root_"), "scala"), "Serializable")), 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())), TypeSelect(Select(Ident("_root_"), "scala"), "Serializable")), 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
@@ -339,7 +339,7 @@ Schema => SemanticDB v4
339339
Uri => Classes.scala
340340
Text => empty
341341
Language => Scala
342-
Symbols => 106 entries
342+
Symbols => 109 entries
343343
Occurrences => 130 entries
344344

345345
Symbols:
@@ -364,6 +364,7 @@ classes/C3#x. => val method x
364364
classes/C3. => final object C3
365365
classes/C3.apply(). => method apply
366366
classes/C3.apply().(x) => param x
367+
classes/C3.toString(). => method toString
367368
classes/C3.unapply(). => method unapply
368369
classes/C3.unapply().(x$1) => param x$1
369370
classes/C4# => case class C4
@@ -377,6 +378,7 @@ classes/C4#x. => val method x
377378
classes/C4. => final object C4
378379
classes/C4.apply(). => method apply
379380
classes/C4.apply().(x) => param x
381+
classes/C4.toString(). => method toString
380382
classes/C4.unapply(). => method unapply
381383
classes/C4.unapply().(x$1) => param x$1
382384
classes/C6# => case class C6
@@ -390,6 +392,7 @@ classes/C6#x. => val method x
390392
classes/C6. => final object C6
391393
classes/C6.apply(). => method apply
392394
classes/C6.apply().(x) => param x
395+
classes/C6.toString(). => method toString
393396
classes/C6.unapply(). => method unapply
394397
classes/C6.unapply().(x$1) => param x$1
395398
classes/C7# => class C7
@@ -639,7 +642,7 @@ Schema => SemanticDB v4
639642
Uri => Enums.scala
640643
Text => empty
641644
Language => Scala
642-
Symbols => 157 entries
645+
Symbols => 159 entries
643646
Occurrences => 203 entries
644647

645648
Symbols:
@@ -705,6 +708,7 @@ _empty_/Enums.Maybe.Just. => final object Just
705708
_empty_/Enums.Maybe.Just.apply(). => method apply
706709
_empty_/Enums.Maybe.Just.apply().(value) => param value
707710
_empty_/Enums.Maybe.Just.apply().[A] => typeparam A
711+
_empty_/Enums.Maybe.Just.toString(). => method toString
708712
_empty_/Enums.Maybe.Just.unapply(). => method unapply
709713
_empty_/Enums.Maybe.Just.unapply().(x$1) => param x$1
710714
_empty_/Enums.Maybe.Just.unapply().[A] => typeparam A
@@ -788,6 +792,7 @@ _empty_/Enums.`<:<`.Refl#copy().[C] => typeparam C
788792
_empty_/Enums.`<:<`.Refl. => final object Refl
789793
_empty_/Enums.`<:<`.Refl.apply(). => method apply
790794
_empty_/Enums.`<:<`.Refl.apply().[C] => typeparam C
795+
_empty_/Enums.`<:<`.Refl.toString(). => method toString
791796
_empty_/Enums.`<:<`.Refl.unapply(). => method unapply
792797
_empty_/Enums.`<:<`.Refl.unapply().(x$1) => param x$1
793798
_empty_/Enums.`<:<`.Refl.unapply().[C] => typeparam C
@@ -2071,7 +2076,7 @@ Schema => SemanticDB v4
20712076
Uri => NamedApplyBlock.scala
20722077
Text => empty
20732078
Language => Scala
2074-
Symbols => 45 entries
2079+
Symbols => 46 entries
20752080
Occurrences => 46 entries
20762081

20772082
Symbols:
@@ -2100,6 +2105,7 @@ example/NamedApplyBlockCaseClassConstruction.Msg.apply(). => method apply
21002105
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(body) => param body
21012106
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(head) => param head
21022107
example/NamedApplyBlockCaseClassConstruction.Msg.apply().(tail) => param tail
2108+
example/NamedApplyBlockCaseClassConstruction.Msg.toString(). => method toString
21032109
example/NamedApplyBlockCaseClassConstruction.Msg.unapply(). => method unapply
21042110
example/NamedApplyBlockCaseClassConstruction.Msg.unapply().(x$1) => param x$1
21052111
example/NamedApplyBlockCaseClassConstruction.bodyText. => val method bodyText
@@ -2177,7 +2183,7 @@ Schema => SemanticDB v4
21772183
Uri => NamedArguments.scala
21782184
Text => empty
21792185
Language => Scala
2180-
Symbols => 15 entries
2186+
Symbols => 16 entries
21812187
Occurrences => 13 entries
21822188

21832189
Symbols:
@@ -2193,6 +2199,7 @@ example/NamedArguments#User#name. => val method name
21932199
example/NamedArguments#User. => final object User
21942200
example/NamedArguments#User.apply(). => method apply
21952201
example/NamedArguments#User.apply().(name) => param name
2202+
example/NamedArguments#User.toString(). => method toString
21962203
example/NamedArguments#User.unapply(). => method unapply
21972204
example/NamedArguments#User.unapply().(x$1) => param x$1
21982205
example/NamedArguments#`<init>`(). => primary ctor <init>
@@ -2430,7 +2437,7 @@ Schema => SemanticDB v4
24302437
Uri => Synthetic.scala
24312438
Text => empty
24322439
Language => Scala
2433-
Symbols => 37 entries
2440+
Symbols => 38 entries
24342441
Occurrences => 163 entries
24352442

24362443
Symbols:
@@ -2456,6 +2463,7 @@ example/Synthetic#s.Bar#`<init>`(). => primary ctor <init>
24562463
example/Synthetic#s.Bar#copy(). => method copy
24572464
example/Synthetic#s.Bar. => final object Bar
24582465
example/Synthetic#s.Bar.apply(). => method apply
2466+
example/Synthetic#s.Bar.toString(). => method toString
24592467
example/Synthetic#s.Bar.unapply(). => method unapply
24602468
example/Synthetic#s.Bar.unapply().(x$1) => param x$1
24612469
example/Synthetic#s.apply(). => method apply
@@ -3113,7 +3121,7 @@ Schema => SemanticDB v4
31133121
Uri => recursion.scala
31143122
Text => empty
31153123
Language => Scala
3116-
Symbols => 35 entries
3124+
Symbols => 36 entries
31173125
Occurrences => 56 entries
31183126

31193127
Symbols:
@@ -3145,6 +3153,7 @@ recursion/Nats.Succ. => final object Succ
31453153
recursion/Nats.Succ.apply(). => method apply
31463154
recursion/Nats.Succ.apply().(p) => param p
31473155
recursion/Nats.Succ.apply().[N] => typeparam N
3156+
recursion/Nats.Succ.toString(). => method toString
31483157
recursion/Nats.Succ.unapply(). => method unapply
31493158
recursion/Nats.Succ.unapply().(x$1) => param x$1
31503159
recursion/Nats.Succ.unapply().[N] => typeparam N
@@ -3400,7 +3409,7 @@ Schema => SemanticDB v4
34003409
Uri => semanticdb-Types.scala
34013410
Text => empty
34023411
Language => Scala
3403-
Symbols => 140 entries
3412+
Symbols => 142 entries
34043413
Occurrences => 250 entries
34053414

34063415
Symbols:
@@ -3428,6 +3437,7 @@ types/Foo#s. => val method s
34283437
types/Foo. => final object Foo
34293438
types/Foo.apply(). => method apply
34303439
types/Foo.apply().(s) => param s
3440+
types/Foo.toString(). => method toString
34313441
types/Foo.unapply(). => method unapply
34323442
types/Foo.unapply().(x$1) => param x$1
34333443
types/Foo.x. => val method x
@@ -3479,6 +3489,7 @@ types/Test.C#RepeatedType#s. => val method s
34793489
types/Test.C#RepeatedType. => final object RepeatedType
34803490
types/Test.C#RepeatedType.apply(). => method apply
34813491
types/Test.C#RepeatedType.apply().(s) => param s
3492+
types/Test.C#RepeatedType.toString(). => method toString
34823493
types/Test.C#RepeatedType.unapplySeq(). => method unapplySeq
34833494
types/Test.C#RepeatedType.unapplySeq().(x$1) => param x$1
34843495
types/Test.C#TypeType. => final object TypeType
@@ -3805,7 +3816,7 @@ Schema => SemanticDB v4
38053816
Uri => semanticdb-extract.scala
38063817
Text => empty
38073818
Language => Scala
3808-
Symbols => 17 entries
3819+
Symbols => 18 entries
38093820
Occurrences => 22 entries
38103821

38113822
Symbols:
@@ -3821,6 +3832,7 @@ _empty_/AnObject.Foo#x. => val method x
38213832
_empty_/AnObject.Foo. => final object Foo
38223833
_empty_/AnObject.Foo.apply(). => method apply
38233834
_empty_/AnObject.Foo.apply().(x) => param x
3835+
_empty_/AnObject.Foo.toString(). => method toString
38243836
_empty_/AnObject.Foo.unapply(). => method unapply
38253837
_empty_/AnObject.Foo.unapply().(x$1) => param x$1
38263838
_empty_/AnObject.foo(). => method foo

0 commit comments

Comments
 (0)