From 7fc77f83c873ce3fada0e01db4947caeb67527ab Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Tue, 28 Jan 2014 18:37:18 +0300 Subject: sane semantics for Symbols.companionSymbol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While playing with tests for Type.companionType, I figured out that companionSymbol isn’t what it seems to be: scala> ScalaPackage.companionSymbol res5: $r.intp.global.Symbol = scala> ScalaPackageClass.companionSymbol res6: $r.intp.global.Symbol = package scala Or even funnier observation: scala> class C; object C defined class C defined object C scala> val classC = typeOf[C].typeSymbol classC: $r.intp.global.Symbol = class C scala> val moduleC = classC.companionSymbol moduleC: $r.intp.global.Symbol = object C scala> classC.companionSymbol == moduleC res0: Boolean = true scala> moduleC.companionSymbol == classC res1: Boolean = true scala> moduleC.moduleClass.companionSymbol == moduleC res2: Boolean = true Of course, I rushed to clean this up, so that `companionSymbol` only returns something other than NoSymbol if the target has a companion in the common sense, not wrt the internal “class with the same name in the same package” convention of scalac, and that `companionSymbol` for module classes is a class, not a source module. Unfortunately it’s not that easy, because api.Symbol#companionSymbol has the same name as internal.Symbol#companionSymbol, so we can’t change the behavior of the former without changing the behavior of the latter. Therefore I deprecated api.Symbol#companionSymbol and introduced a replacement called api.Symbol#companion with sane semantics. --- test/files/run/t6989/Test_2.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/files/run/t6989') diff --git a/test/files/run/t6989/Test_2.scala b/test/files/run/t6989/Test_2.scala index e48e82422d..3f578158e8 100644 --- a/test/files/run/t6989/Test_2.scala +++ b/test/files/run/t6989/Test_2.scala @@ -11,9 +11,9 @@ import scala.reflect.runtime.universe._ package object foo { def testAll(): Unit = { test(typeOf[foo.PackagePrivateJavaClass].typeSymbol) - test(typeOf[foo.PackagePrivateJavaClass].typeSymbol.companionSymbol) + test(typeOf[foo.PackagePrivateJavaClass].typeSymbol.companion) test(typeOf[foo.JavaClass_1].typeSymbol) - test(typeOf[foo.JavaClass_1].typeSymbol.companionSymbol) + test(typeOf[foo.JavaClass_1].typeSymbol.companion) } def test(sym: Symbol): Unit = { -- cgit v1.2.3