From 898755056642cc3771d33260295f1f51cabc6513 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 28 Sep 2011 21:52:39 +0000 Subject: Fix bug in ModuleDef elimination. Fixes corner case diagnosed by miguel. Closes SI-5012, no review. --- .../scala/reflect/internal/Definitions.scala | 6 ++++++ .../scala/tools/nsc/typechecker/RefChecks.scala | 22 ++++++++-------------- test/files/pos/t5012.scala | 12 ++++++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 test/files/pos/t5012.scala diff --git a/src/compiler/scala/reflect/internal/Definitions.scala b/src/compiler/scala/reflect/internal/Definitions.scala index ba9bd27ab6..1f918d0df1 100644 --- a/src/compiler/scala/reflect/internal/Definitions.scala +++ b/src/compiler/scala/reflect/internal/Definitions.scala @@ -297,6 +297,12 @@ trait Definitions extends reflect.api.StandardDefinitions { def isVarArgsList(params: List[Symbol]) = params.nonEmpty && isRepeatedParamType(params.last.tpe) def isVarArgTypes(formals: List[Type]) = formals.nonEmpty && isRepeatedParamType(formals.last) + def hasRepeatedParam(tp: Type): Boolean = tp match { + case MethodType(formals, restpe) => isScalaVarArgs(formals) || hasRepeatedParam(restpe) + case PolyType(_, restpe) => hasRepeatedParam(restpe) + case _ => false + } + def isPrimitiveArray(tp: Type) = tp match { case TypeRef(_, ArrayClass, arg :: Nil) => isValueClass(arg.typeSymbol) case _ => false diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 89ec052556..3b9cf88a00 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -151,12 +151,6 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R // Override checking ------------------------------------------------------------ - def hasRepeatedParam(tp: Type): Boolean = tp match { - case MethodType(formals, restpe) => isScalaVarArgs(formals) || hasRepeatedParam(restpe) - case PolyType(_, restpe) => hasRepeatedParam(restpe) - case _ => false - } - /** Add bridges for vararg methods that extend Java vararg methods */ def addVarargBridges(clazz: Symbol): List[Tree] = { @@ -1100,14 +1094,13 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R */ private def eliminateModuleDefs(tree: Tree): List[Tree] = { val ModuleDef(mods, name, impl) = tree - val sym = tree.symbol - - val classSym = sym.moduleClass - val cdef = ClassDef(mods | MODULE, name.toTypeName, Nil, impl) setSymbol classSym setType NoType + val sym = tree.symbol + val classSym = sym.moduleClass + val cdef = ClassDef(mods | MODULE, name.toTypeName, Nil, impl) setSymbol classSym setType NoType def findOrCreateModuleVar() = localTyper.typedPos(tree.pos) { lazy val createModuleVar = gen.mkModuleVarDef(sym) - sym.owner.info.decl(nme.moduleVarName(sym.name.toTermName)) match { + sym.enclClass.info.decl(nme.moduleVarName(sym.name.toTermName)) match { // In case we are dealing with local symbol then we already have // to correct error with forward reference case NoSymbol => createModuleVar @@ -1117,7 +1110,9 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R def createStaticModuleAccessor() = atPhase(phase.next) { val method = ( sym.owner.newMethod(sym.pos, sym.name.toTermName) - setFlag (sym.flags | STABLE) resetFlag MODULE setInfo NullaryMethodType(sym.moduleClass.tpe) + setFlag (sym.flags | STABLE) + resetFlag MODULE + setInfo NullaryMethodType(sym.moduleClass.tpe) ) sym.owner.info.decls enter method localTyper.typedPos(tree.pos)(gen.mkModuleAccessDef(method, sym)) @@ -1180,8 +1175,7 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R else { val lazySym = tree.symbol.lazyAccessorOrSelf if (lazySym.isLocal && index <= currentLevel.maxindex) { - if (settings.debug.value) - Console.println(currentLevel.refsym) + debuglog("refsym = " + currentLevel.refsym) unit.error(currentLevel.refpos, "forward reference extends over definition of " + lazySym) } List(tree1) diff --git a/test/files/pos/t5012.scala b/test/files/pos/t5012.scala new file mode 100644 index 0000000000..772b8f4486 --- /dev/null +++ b/test/files/pos/t5012.scala @@ -0,0 +1,12 @@ +class D { + object p // (program point 1) +} + +class C { + def m: D = { + if("abc".length == 0) { + object p // (program point 2) + } + null + } +} -- cgit v1.2.3