From 0c5de3cf31e11614eb93c6a1ae31b87d01ac7db5 Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Fri, 11 May 2012 13:40:21 +0200 Subject: Fix SI-5626. By not replacing 'CaseClass.apply()' factor by 'new CaseClass()' when the class type 'CaseClass' is not accessible. --- .../scala/tools/nsc/typechecker/RefChecks.scala | 19 +++++++++++++++++-- test/files/pos/t5626.scala | 12 ++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/files/pos/t5626.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index b878ce3a53..4e578e3f0d 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1488,8 +1488,23 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R private def transformCaseApply(tree: Tree, ifNot: => Unit) = { val sym = tree.symbol - - if (sym.isSourceMethod && sym.isCase && sym.name == nme.apply) + + def isClassTypeAccessible(tree: Tree): Boolean = tree match { + case TypeApply(fun, targs) => + isClassTypeAccessible(fun) + case Select(module, apply) => + // Fixes SI-5626. Classes in refinement types cannot be constructed with `new`. In this case, + // the companion class is actually not a ClassSymbol, but a reference to an abstract type. + module.symbol.companionClass.isClass + } + + val doTransform = + sym.isSourceMethod && + sym.isCase && + sym.name == nme.apply && + isClassTypeAccessible(tree) + + if (doTransform) toConstructor(tree.pos, tree.tpe) else { ifNot diff --git a/test/files/pos/t5626.scala b/test/files/pos/t5626.scala new file mode 100644 index 0000000000..c501dfbe60 --- /dev/null +++ b/test/files/pos/t5626.scala @@ -0,0 +1,12 @@ +class C { + val blob = { + new { case class Foo() } + } + val blub = { + class Inner { case class Foo() } + new Inner + } + + val foo = blob.Foo() + val bar = blub.Foo() +} -- cgit v1.2.3