diff options
author | Paul Phillips <paulp@improving.org> | 2011-04-23 22:04:36 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-04-23 22:04:36 +0000 |
commit | 63c7c9d857568879bfda146dd31bfc4ee38cb9fb (patch) | |
tree | f11372dc3d38c1eb0a327b33afb9fb25a0a7a658 /src/compiler/scala/tools/nsc/typechecker/Typers.scala | |
parent | 21121ff62ea72523ed5ea6a906b2ce42e9e47f69 (diff) | |
download | scala-63c7c9d857568879bfda146dd31bfc4ee38cb9fb.tar.gz scala-63c7c9d857568879bfda146dd31bfc4ee38cb9fb.tar.bz2 scala-63c7c9d857568879bfda146dd31bfc4ee38cb9fb.zip |
Working my way through pattern matcher sequence...
Working my way through pattern matcher sequence issues mostly caused by
the special handling of Lists. Also deleting all kinds of useless or
almost useless code which is presently only clutter.
Closes #2756, #2800, #3050, #3530, #3972, no review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/Typers.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index fb71c8caae..9b23cf188b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -2466,35 +2466,33 @@ trait Typers extends Modes { /* --- begin unapply --- */ case otpe if inPatternMode(mode) && unapplyMember(otpe).exists => - val unapp = unapplyMember(otpe) - assert(unapp.exists, tree) - val unappType = otpe.memberType(unapp) - val argDummyType = pt // was unappArg - // @S: do we need to memoize this? - val argDummy = context.owner.newValue(fun.pos, nme.SELECTOR_DUMMY) - .setFlag(SYNTHETIC) - .setInfo(argDummyType) if (args.length > MaxTupleArity) error(fun.pos, "too many arguments for unapply pattern, maximum = "+MaxTupleArity) - val arg = Ident(argDummy) setType argDummyType - val oldArgType = arg.tpe - if (!isApplicableSafe(List(), unappType, List(arg.tpe), WildcardType)) { + + def freshArgType(tp: Type): (Type, List[Symbol]) = (tp: @unchecked) match { + case MethodType(param :: _, _) => + (param.tpe, Nil) + case PolyType(tparams, restype) => + val tparams1 = cloneSymbols(tparams) + (freshArgType(restype)._1.substSym(tparams, tparams1), tparams1) + case OverloadedType(_, _) => + error(fun.pos, "cannot resolve overloaded unapply") + (ErrorType, Nil) + } + + val unapp = unapplyMember(otpe) + val unappType = otpe.memberType(unapp) + val argDummy = context.owner.newValue(fun.pos, nme.SELECTOR_DUMMY) setFlag SYNTHETIC setInfo pt + val arg = Ident(argDummy) setType pt + + if (!isApplicableSafe(Nil, unappType, List(pt), WildcardType)) { //Console.println("UNAPP: need to typetest, arg.tpe = "+arg.tpe+", unappType = "+unappType) - def freshArgType(tp: Type): (Type, List[Symbol]) = tp match { - case MethodType(params, _) => - (params(0).tpe, Nil) - case PolyType(tparams, restype) => - val tparams1 = cloneSymbols(tparams) - (freshArgType(restype)._1.substSym(tparams, tparams1), tparams1) - case OverloadedType(_, _) => - error(fun.pos, "cannot resolve overloaded unapply") - (ErrorType, Nil) - } val (unappFormal, freeVars) = freshArgType(unappType.skolemizeExistential(context.owner, tree)) val context1 = context.makeNewScope(context.tree, context.owner) freeVars foreach context1.scope.enter + val typer1 = newTyper(context1) - val pattp = typer1.infer.inferTypedPattern(tree.pos, unappFormal, arg.tpe) + val pattp = typer1.infer.inferTypedPattern(tree.pos, unappFormal, arg.tpe) // turn any unresolved type variables in freevars into existential skolems val skolems = freeVars map { fv => @@ -2504,8 +2502,7 @@ trait Typers extends Modes { skolem } arg.tpe = pattp.substSym(freeVars, skolems) - //todo: replace arg with arg.asInstanceOf[inferTypedPattern(unappFormal, arg.tpe)] instead. - argDummy.setInfo(arg.tpe) // bq: this line fixed #1281. w.r.t. comment ^^^, maybe good enough? + argDummy setInfo arg.tpe } // setType null is necessary so that ref will be stabilized; see bug 881 @@ -2517,12 +2514,13 @@ trait Typers extends Modes { val formals1 = formalTypes(formals0, args.length) if (sameLength(formals1, args)) { val args1 = typedArgs(args, mode, formals0, formals1) - if (!isFullyDefined(pt)) assert(false, tree+" ==> "+UnApply(fun1, args1)+", pt = "+pt) - val itype = glb(List(pt, arg.tpe)) - // restore old type (arg is a dummy tree, just needs to pass typechecking) - arg.tpe = oldArgType + assert(isFullyDefined(pt), tree+" ==> "+UnApply(fun1, args1)+", pt = "+pt) + + val itype = glb(List(pt, arg.tpe)) + arg.tpe = pt // restore type (arg is a dummy tree, just needs to pass typechecking) UnApply(fun1, args1) setPos tree.pos setType itype - } else { + } + else { errorTree(tree, "wrong number of arguments for "+treeSymTypeMsg(fun)) } } @@ -3890,7 +3888,7 @@ trait Typers extends Modes { case UnApply(fun, args) => val fun1 = typed(fun) val tpes = formalTypes(unapplyTypeList(fun.symbol, fun1.tpe), args.length) - val args1 = (args, tpes).zipped map (typedPattern(_, _)) + val args1 = (args, tpes).zipped map typedPattern treeCopy.UnApply(tree, fun1, args1) setType pt case ArrayValue(elemtpt, elems) => |