Permalink
Browse files

SI-7345 Drive by refactoring of pattern matching for `arg: _*`.

  • Loading branch information...
1 parent e112db6 commit 2304a78a00fe45cb05d1c3b77e4381813109cbfd @retronym retronym committed Apr 16, 2013
@@ -19,6 +19,7 @@ trait NamesDefaults { self: Analyzer =>
import global._
import definitions._
import NamesDefaultsErrorsGen._
+ import treeInfo.WildcardStarArg
// Default getters of constructors are added to the companion object in the
// typeCompleter of the constructor (methodSig). To compute the signature,
@@ -278,8 +279,8 @@ trait NamesDefaults { self: Analyzer =>
val repeated = isScalaRepeatedParamType(paramTpe)
val argTpe = (
if (repeated) arg match {
- case Typed(expr, Ident(tpnme.WILDCARD_STAR)) => expr.tpe
- case _ => seqType(arg.tpe)
+ case WildcardStarArg(expr) => expr.tpe
+ case _ => seqType(arg.tpe)
}
else
// Note stabilizing can lead to a non-conformant argument when existentials are involved, e.g. neg/t3507-old.scala, hence the filter.
@@ -302,11 +303,8 @@ trait NamesDefaults { self: Analyzer =>
} else {
new ChangeOwnerTraverser(context.owner, sym) traverse arg // fixes #4502
if (repeated) arg match {
- case Typed(expr, Ident(tpnme.WILDCARD_STAR)) =>
- expr
- case _ =>
- val factory = Select(gen.mkAttributedRef(SeqModule), nme.apply)
- blockTyper.typed(Apply(factory, List(resetLocalAttrs(arg))))
+ case WildcardStarArg(expr) => expr
+ case _ => blockTyper typed gen.mkSeqApply(resetLocalAttrs(arg))
} else arg
}
Some(atPos(body.pos)(ValDef(sym, body).setType(NoType)))
@@ -1591,7 +1591,7 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
enterReference(tree.pos, tpt.tpe.typeSymbol)
tree
- case Typed(_, Ident(tpnme.WILDCARD_STAR)) if !isRepeatedParamArg(tree) =>
+ case treeInfo.WildcardStarArg(_) if !isRepeatedParamArg(tree) =>
unit.error(tree.pos, "no `: _*' annotation allowed here\n"+
"(such annotations are only allowed in arguments to *-parameters)")
tree
@@ -3101,7 +3101,7 @@ trait Typers extends Adaptations with Tags {
// the assign is untyped; that's ok because we call doTypedApply
val arg1 = treeCopy.AssignOrNamedArg(arg, arg.lhs, rhs1)
(arg1, NamedType(name, rhs1.tpe.deconst))
- case arg @ Typed(repeated, Ident(tpnme.WILDCARD_STAR)) =>
+ case arg @ treeInfo.WildcardStarArg(repeated) =>
val arg1 = typedArg0(arg)
(arg1, RepeatedType(arg1.tpe.deconst))
case arg =>
@@ -4986,7 +4986,7 @@ trait Typers extends Adaptations with Tags {
typedEta(checkDead(exprTyped))
}
- case Ident(tpnme.WILDCARD_STAR) =>
+ case t if treeInfo isWildcardStarType t =>
val exprTyped = typed(expr, mode.onlySticky, WildcardType)
def subArrayType(pt: Type) =
if (isPrimitiveValueClass(pt.typeSymbol) || !isFullyDefined(pt)) arrayType(pt)
@@ -298,4 +298,9 @@ abstract class TreeGen extends macros.TreeBuilder {
def mkPackageDef(packageName: String, stats: List[Tree]): PackageDef = {
PackageDef(mkUnattributedRef(newTermName(packageName)), stats)
}
+
+ def mkSeqApply(arg: Tree): Apply = {
+ val factory = Select(gen.mkAttributedRef(SeqModule), nme.apply)
+ Apply(factory, List(arg))
+ }
}
@@ -401,8 +401,15 @@ abstract class TreeInfo {
/** Is this argument node of the form <expr> : _* ?
*/
def isWildcardStarArg(tree: Tree): Boolean = tree match {
- case Typed(_, Ident(tpnme.WILDCARD_STAR)) => true
- case _ => false
+ case WildcardStarArg(_) => true
+ case _ => false
+ }
+
+ object WildcardStarArg {
+ def unapply(tree: Typed): Option[Tree] = tree match {
+ case Typed(expr, Ident(tpnme.WILDCARD_STAR)) => Some(expr)
+ case _ => None
+ }
}
/** If this tree has type parameters, those. Otherwise Nil.

0 comments on commit 2304a78

Please sign in to comment.