diff options
author | Paul Phillips <paulp@improving.org> | 2010-11-04 00:07:36 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-11-04 00:07:36 +0000 |
commit | 379af580e2c8cf0ce5309fc0b31702f79e415abe (patch) | |
tree | cf741eb0d27624d6130bb5bc7f403ec7fb05af1d /src/compiler/scala/tools/nsc/typechecker/Unapplies.scala | |
parent | de012b3a6d04f1e7a9fd6fecd403e0492f6ad7c1 (diff) | |
download | scala-379af580e2c8cf0ce5309fc0b31702f79e415abe.tar.gz scala-379af580e2c8cf0ce5309fc0b31702f79e415abe.tar.bz2 scala-379af580e2c8cf0ce5309fc0b31702f79e415abe.zip |
Determined that half a dozen ways of checking f...
Determined that half a dozen ways of checking for varargs and
by-name-ness in param lists exceeded the legal limit. Also assessed that
names which are only used as type names would be a lot easier to deal
with if we created them as type names up front. Performed the changes
implied by the preceding along with a partial cleanup on TreeInfo which
one can see hasn't had a good look in a long time. (And still hasn't.)
No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/Unapplies.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Unapplies.scala | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala index 409cc30291..80b264821a 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Unapplies.scala @@ -19,10 +19,7 @@ trait Unapplies extends ast.TreeDSL import global._ import definitions._ import CODE.{ CASE => _, _ } - - private def isVarargs(vd: ValDef) = treeInfo isRepeatedParamType vd.tpt - private def isByName(vd: ValDef) = treeInfo isByNameParamType vd.tpt - private def toIdent(x: DefTree) = Ident(x.name) setPos x.pos.focus + import treeInfo.{ isRepeatedParamType, isByNameParamType } /** returns type list for return type of the extraction */ def unapplyTypeList(ufn: Symbol, ufntpe: Type) = { @@ -66,10 +63,10 @@ trait Unapplies extends ast.TreeDSL case OptionClass | SomeClass => val ts = unapplyTypeListFromReturnType(tp1) val last1 = (ts.last baseType SeqClass) match { - case TypeRef(pre, seqClass, args) => typeRef(pre, RepeatedParamClass, args) // XXX seqClass or SeqClass? + case TypeRef(pre, SeqClass, args) => typeRef(pre, RepeatedParamClass, args) case _ => throw new TypeError("last not seq") } - ts.init ::: List(last1) + ts.init :+ last1 case _ => throw new TypeError("result type "+tp+" of unapply not in {Option[_], Some[_]}") } @@ -92,10 +89,9 @@ trait Unapplies extends ast.TreeDSL case unapp => unapp } /** returns unapply member's parameter type. */ - def unapplyParameterType(extractor: Symbol) = { - val ps = extractor.tpe.params - if (ps.length == 1) ps.head.tpe.typeSymbol - else NoSymbol + def unapplyParameterType(extractor: Symbol) = extractor.tpe.params match { + case p :: Nil => p.tpe.typeSymbol + case _ => NoSymbol } def copyUntyped[T <: Tree](tree: T): T = @@ -107,6 +103,8 @@ trait Unapplies extends ast.TreeDSL returning[TypeDef](copy.duplicate)(UnTyper traverse _) } + private def toIdent(x: DefTree) = Ident(x.name) setPos x.pos.focus + private def classType(cdef: ClassDef, tparams: List[TypeDef]): Tree = { val tycon = REF(cdef.symbol) if (tparams.isEmpty) tycon else AppliedTypeTree(tycon, tparams map toIdent) @@ -136,7 +134,7 @@ trait Unapplies extends ast.TreeDSL // > MaxFunctionArity is caught in Namers, but for nice error reporting instead of // an abrupt crash we trim the list here. def primaries = constrParamss(cdef).head take MaxFunctionArity map (_.tpt) - def inheritFromFun = !(cdef.mods hasFlag ABSTRACT) && cdef.tparams.isEmpty && constrParamss(cdef).length == 1 + def inheritFromFun = !cdef.mods.hasAbstractFlag && cdef.tparams.isEmpty && constrParamss(cdef).length == 1 def createFun = gen.scalaFunctionConstr(primaries, toIdent(cdef), abstractFun = true) def parents = if (inheritFromFun) List(createFun) else Nil def toString = DefDef( @@ -151,7 +149,7 @@ trait Unapplies extends ast.TreeDSL } def companionModuleDef(cdef: ClassDef, parents: List[Tree] = Nil, body: List[Tree] = Nil): ModuleDef = atPos(cdef.pos.focus) { - val allParents = parents ::: List( gen.scalaScalaObjectConstr) + val allParents = parents :+ gen.scalaScalaObjectConstr ModuleDef( Modifiers(cdef.mods.flags & AccessFlags | SYNTHETIC, cdef.mods.privateWithin), cdef.name.toTermName, @@ -177,8 +175,8 @@ trait Unapplies extends ast.TreeDSL val tparams = cdef.tparams map copyUntypedInvariant val paramName = newTermName("x$0") val method = constrParamss(cdef) match { - case xs :: _ if !xs.isEmpty && isVarargs(xs.last) => nme.unapplySeq - case _ => nme.unapply + case xs :: _ if xs.nonEmpty && isRepeatedParamType(xs.last.tpt) => nme.unapplySeq + case _ => nme.unapply } val cparams = List(ValDef(Modifiers(PARAM | SYNTHETIC), paramName, classType(cdef, tparams), EmptyTree)) val ifNull = if (constrParamss(cdef).head.size == 0) FALSE else REF(NoneModule) @@ -190,11 +188,11 @@ trait Unapplies extends ast.TreeDSL } def caseClassCopyMeth(cdef: ClassDef): Option[DefDef] = { - def isDisallowed(vd: ValDef) = isVarargs(vd) || isByName(vd) + def isDisallowed(vd: ValDef) = isRepeatedParamType(vd.tpt) || isByNameParamType(vd.tpt) val cparamss = constrParamss(cdef) val flat = cparamss flatten - if (flat.isEmpty || (cdef.symbol hasFlag ABSTRACT) || (flat exists isDisallowed)) None + if (flat.isEmpty || cdef.symbol.hasAbstractFlag || (flat exists isDisallowed)) None else { val tparams = cdef.tparams map copyUntypedInvariant // the parameter types have to be exactly the same as the constructor's parameter types; so it's |