From 7359e0bd65cd4401a7e7cf6c1584263262b3bd0d Mon Sep 17 00:00:00 2001 From: rytz Date: Tue, 29 Jun 2010 14:19:42 +0000 Subject: [PATCH] companion objects of case classes know their name. review by extempore. close #3579. git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@22420 5e8d7ff9-d8ef-0310-90f0-a4852d11357a --- .../scala/tools/nsc/typechecker/Unapplies.scala | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala index d2021d96a..b4f770706 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala @@ -130,21 +130,28 @@ trait Unapplies extends ast.TreeDSL } } - /** The module corresponding to a case class; without any member definitions + /** The module corresponding to a case class; overrides toString to show the module's name */ def caseModuleDef(cdef: ClassDef): ModuleDef = { def inheritFromFun = !(cdef.mods hasFlag ABSTRACT) && cdef.tparams.isEmpty && constrParamss(cdef).length == 1 def createFun = gen.scalaFunctionConstr(constrParamss(cdef).head map (_.tpt), toIdent(cdef), abstractFun = true) def parents = if (inheritFromFun) List(createFun) else Nil - - companionModuleDef(cdef, parents ::: List(gen.scalaScalaObjectConstr)) + def toString = DefDef( + Modifiers(OVERRIDE | FINAL), + nme.toString_, + Nil, + List(Nil), + TypeTree(), + Literal(Constant(cdef.name.decode))) + + companionModuleDef(cdef, parents ::: List(gen.scalaScalaObjectConstr), List(toString)) } - def companionModuleDef(cdef: ClassDef, parents: List[Tree]): ModuleDef = atPos(cdef.pos.focus) { + def companionModuleDef(cdef: ClassDef, parents: List[Tree], body: List[Tree] = Nil): ModuleDef = atPos(cdef.pos.focus) { ModuleDef( Modifiers(cdef.mods.flags & AccessFlags | SYNTHETIC, cdef.mods.privateWithin), cdef.name.toTermName, - Template(parents, emptyValDef, NoMods, Nil, List(Nil), Nil, cdef.impl.pos.focus)) + Template(parents, emptyValDef, NoMods, Nil, List(Nil), body, cdef.impl.pos.focus)) } private val caseMods = Modifiers(SYNTHETIC | CASE)