diff options
author | Burak Emir <emir@epfl.ch> | 2007-01-05 15:24:26 +0000 |
---|---|---|
committer | Burak Emir <emir@epfl.ch> | 2007-01-05 15:24:26 +0000 |
commit | a3d868bf57e42a3391df774a5e2cfd7123828625 (patch) | |
tree | b763b2759b734ad9c904736d4af787c673a5f0e6 /src | |
parent | 735573067a9f827534aa6cf28d0f4e845466540a (diff) | |
download | scala-a3d868bf57e42a3391df774a5e2cfd7123828625.tar.gz scala-a3d868bf57e42a3391df774a5e2cfd7123828625.tar.bz2 scala-a3d868bf57e42a3391df774a5e2cfd7123828625.zip |
removed Xkilloption from ExplicitOuter
tab->whitespace in PatternMatcher
modified test case unapplySeq
new test case unapplyGeneric
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/matching/PatternMatchers.scala | 110 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala | 145 |
2 files changed, 55 insertions, 200 deletions
diff --git a/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala b/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala index ecead9e7d2..cda0e309f2 100644 --- a/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala +++ b/src/compiler/scala/tools/nsc/matching/PatternMatchers.scala @@ -242,28 +242,28 @@ trait PatternMatchers requires (transform.ExplicitOuter with PatternNodes) { var nref = 0 var nsafeRef = 0 override def traverse(tree: Tree) = tree match { - case t:Ident if t.symbol == sym => - nref = nref + 1 - if(sym.owner == currentOwner) { // oldOwner should match currentOwner - nsafeRef = nsafeRef + 1 - } /*else if(nref == 1) { - Console.println("sym owner: "+sym.owner+" but currentOwner = "+currentOwner) - }*/ - case t if nref > 1 => // abort, no story to tell - - case t => super . traverse (t) + case t:Ident if t.symbol == sym => + nref = nref + 1 + if(sym.owner == currentOwner) { // oldOwner should match currentOwner + nsafeRef = nsafeRef + 1 + } /*else if(nref == 1) { + Console.println("sym owner: "+sym.owner+" but currentOwner = "+currentOwner) + }*/ + case t if nref > 1 => // abort, no story to tell + + case t => super . traverse (t) } } class Subst(sym:Symbol,rhs:Tree) extends Transformer { var stop = false override def transform(tree: Tree) = tree match { - case t:Ident if t.symbol == sym => - stop = true - rhs - case t if stop => - t - case t => - super . transform (t) + case t:Ident if t.symbol == sym => + stop = true + rhs + case t if stop => + t + case t => + super . transform (t) } } vds match { @@ -274,26 +274,26 @@ trait PatternMatchers requires (transform.ExplicitOuter with PatternNodes) { val exp1 = squeezedBlock(rest, exp) //Console.println("squeezedBlock for valdef "+vd) - val sym = vd.symbol - val rt = new RefTraverser(sym) - rt.atOwner (PatternMatcher.this.owner) (rt.traverse(exp1)) - rt.nref match { - case 0 => - nremoved = nremoved + 1 - exp1 - case 1 if rt.nsafeRef == 1 => - nsubstituted = nsubstituted + 1 - new Subst(sym, vd.rhs).transform( exp1 ) - case _ => + val sym = vd.symbol + val rt = new RefTraverser(sym) + rt.atOwner (PatternMatcher.this.owner) (rt.traverse(exp1)) + rt.nref match { + case 0 => + nremoved = nremoved + 1 + exp1 + case 1 if rt.nsafeRef == 1 => + nsubstituted = nsubstituted + 1 + new Subst(sym, vd.rhs).transform( exp1 ) + case _ => exp1 match { - case Block(vds2, exp2) => Block(vd::vds2, exp2) - case exp2 => Block(vd::Nil, exp2) + case Block(vds2, exp2) => Block(vd::vds2, exp2) + case exp2 => Block(vd::Nil, exp2) } } case x::xs => squeezedBlock(xs, exp) match { - case Block(vds2, exp2) => Block(x::vds2, exp2) - case exp2 => Block(x::Nil, exp2) + case Block(vds2, exp2) => Block(x::vds2, exp2) + case exp2 => Block(x::Nil, exp2) } } } @@ -385,7 +385,7 @@ trait PatternMatchers requires (transform.ExplicitOuter with PatternNodes) { //Console.println("tree.getClass() "+tree.getClass()); val t = tree match { case Bind(name, Typed(Ident( nme.WILDCARD ), tpe)) => // x@_:Type - val tpe2test = tree.symbol.tpe // safest! typer does not always use tpe.tpe! + val tpe2test = tree.symbol.tpe // safest! typer does not always use tpe.tpe! // @todo: this optimization probably not allowed, because of null -- but can rework it //if (isSubType(header.getTpe(), tpe2test)) { @@ -706,8 +706,8 @@ print() var target = target1 var casted = casted1 target match { - case UnapplyPat(newCasted, fn) => - casted = newCasted + case UnapplyPat(newCasted, fn) => + casted = newCasted case ConstrPat(newCasted) => casted = newCasted case SequencePat(newCasted, len) => @@ -1057,7 +1057,7 @@ print() .setFlag(Flags.SYNTHETIC) .setInfo(body(i).tpe); // used to be resultType var res0: Tree = - squeezedBlock( + squeezedBlock( List( ValDef(temp, body(i)), Apply(Ident(exit), List(Ident(temp)) ) @@ -1091,7 +1091,7 @@ print() final private def inheritsFromSealed(tpe:Type): Boolean = { val it = tpe.baseClasses.elements while(it.hasNext) { - if(it.next.isSealed) return true + if(it.next.isSealed) return true } return false } @@ -1159,12 +1159,12 @@ print() */ var nCases: List[CaseDef] = Nil while (cases ne null) { - if(inheritsFromSealed(cases.node.tpe)) { - val t = toTree_refined(cases.node, selector, true) - //Console.println("optimize this"+t+" from this "+cases.node) + if(inheritsFromSealed(cases.node.tpe)) { + val t = toTree_refined(cases.node, selector, true) + //Console.println("optimize this"+t+" from this "+cases.node) nCases = CaseDef(Literal(Constant(cases.tag)), t) :: nCases; - } else + } else nCases = CaseDef(Literal(Constant(cases.tag)), toTree(cases.node, selector)) :: nCases; cases = cases.next @@ -1252,18 +1252,14 @@ print() var __opt_nonemp__ = Not(Select(Ident(v), nme.isEmpty)) Or(And(checkType, - squeezedBlock( - List(ValDef(v,Apply(fn1,List(useSelector)))), - And(__opt_nonemp__, - squeezedBlock(List(ValDef(casted, __opt_get__)),toTree(node.and)))) + squeezedBlock( + List(ValDef(v,Apply(fn1,List(useSelector)))), + And(__opt_nonemp__, + squeezedBlock(List(ValDef(casted, __opt_get__)),toTree(node.and)))) ), toTree(node.or, selector.duplicate)) case ConstrPat(casted) => - if(!isSubType(casted.tpe,selector.tpe) && settings.Xkilloption.value && definitions.isOptionType(selector.tpe)) { - // option of options - warning("in pattern match:problem matching "+casted.tpe+" against "+selector.tpe+"\n please avoid options of options!") - } def outerAlwaysEqual(left: Type, right: Type) = Pair(left,right) match { case Pair(TypeRef(lprefix, _,_), TypeRef(rprefix,_,_)) if lprefix =:= rprefix => true @@ -1279,8 +1275,8 @@ print() cond = NotNull(selector.duplicate) nstatic = nstatic + 1 } else if(ignoreSelectorType) { - cond = Literal(Constant(true)) - } else { + cond = Literal(Constant(true)) + } else { cond = typed { gen.mkIsInstanceOf(selector.duplicate, ntpe) } } // compare outer instance for patterns like foo1.Bar foo2.Bar if not statically known to match @@ -1377,15 +1373,11 @@ print() fail); case VariablePat(tree) => - val cmp = if(tree.tpe.symbol.isModuleClass && // objects are compared by eq, not == (avoids unnecessary null-magic) - selector.tpe <:< definitions.AnyRefClass.tpe) { - if(settings.Xkilloption.value && tree.symbol == definitions.NoneClass) - IsNull(selector) - else - Eq(selector.duplicate, tree) - } else { + // objects are compared by eq, not == + val cmp = if(tree.tpe.symbol.isModuleClass && selector.tpe <:< definitions.AnyRefClass.tpe) + Eq(selector.duplicate, tree) + else Equals(selector.duplicate, tree) - } return myIf( cmp, toTree(node.and), toTree(node.or, selector.duplicate)); diff --git a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala index 14d4f7d5a9..97c31bed53 100644 --- a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala +++ b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala @@ -169,10 +169,7 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter * </ol> */ def transformInfo(sym: Symbol, tp: Type): Type = tp match { - case MethodType(formals1, restpe1) => - //Console.println("hello methodtype"+tp) - val formals = if(settings.Xkilloption.value) {formals1 map {x=>transformInfo(sym, x)}} // removeOption - else formals1 + case MethodType(formals, restpe1) => val restpe = transformInfo(sym, restpe1) // removeOption if (sym.owner.isTrait && ((sym hasFlag SUPERACCESSOR) || sym.isModule)) { // 5 sym.makeNotPrivate(sym.owner) @@ -183,11 +180,7 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter else if (restpe ne restpe1) MethodType(formals, restpe) // removeOption else tp - case ClassInfoType(parents1, decls, clazz) => - if(settings.Xkilloption.value && (clazz.linkedModuleOfClass eq definitions.NoneClass)) - return tp - val parents = if(settings.Xkilloption.value) parents1 map {x => transformInfo(sym,x)} - else parents1 + case ClassInfoType(parents, decls, clazz) => var decls1 = decls if (isInner(clazz) && !(clazz hasFlag INTERFACE)) { decls1 = newScope(decls.toList) @@ -214,38 +207,13 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter } } } - if ((decls1 eq decls)&&(parents1 eq parents)) tp else ClassInfoType(parents, decls1, clazz) + if (decls1 eq decls) tp else ClassInfoType(parents, decls1, clazz) case PolyType(tparams, restp) => val restp1 = transformInfo(sym, restp) if (restp eq restp1) tp else PolyType(tparams, restp1) - // <removeOption> - case TypeRef(pre,sym,args) if(settings.Xkilloption.value) => - if(wasOptionRef(tp)) { // ((sym eq definitions.OptionClass) || (sym eq definitions.SomeClass)) { - getOptionArg(tp) - } else { - val t = typeRef(pre,sym, args.map {t=>transformInfo(sym,t)}) - //Console.println("turning typeref "+tp+" to "+t) - t - } - - // </removeOption> case _ => - if(!settings.Xkilloption.value) tp - else { // bq: gross hack for removing options in refinements (e.g. dbc). YMMV - if(tp.symbol.isRefinementClass) { - if(settings.debug.value) - Console.println("[k.o.] rewriting refinement ") - for(val d <- tp.decls.elements) { - val otpe = d.tpe - d setInfo transformInfo(tp.symbol, d.tpe) - if(settings.debug.value) - Console.println(" "+otpe+"-->"+d.tpe) - } - } - tp - } } /** A base class for transformers that maintain <code>outerParam</code> @@ -430,98 +398,6 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter if (sym hasFlag PROTECTED) sym setFlag notPROTECTED } tree match { - - // <removeOption> - - case sm @ Apply(fn1, List(vlue)) // scala.Some(x:T) -> x if T <: AnyRef - if settings.Xkilloption.value - && wasOptionRef(sm.tpe) - && ((fn1.symbol eq null) // in patterns, symbol is null - || fn1.symbol.isClassConstructor) => - - val fn = transform(fn1) - if(settings.debug.value) - Console.println("[k.o. (apply)]"+tree+" ==> "+vlue) - if((fn.symbol eq null) && (vlue.symbol ne null/*isInstanceOf[Ident]*/)) { - val argtpe = getOptionArg(sm.tpe) - Typed(vlue, TypeTree().setType(argtpe)).setType(argtpe) // pattern matcher optimizes to null check - } else vlue - - case Ident(_) if settings.Xkilloption.value && (definitions.NoneClass eq tree.symbol) => // only appears in actors/Channel, possibly bug - if(settings.debug.value) - Console.println("[k.o. (ident)]"+tree+" ==> null") - atPos(tree.pos) { typer.typed { Literal(Constant(null)) }} - - case nne: Select // scala.None -> null - if settings.Xkilloption.value && nne.symbol == definitions.NoneClass => // wasNone(nne.tpe) => - - if(settings.debug.value) - Console.println("[k.o. (select)]"+tree+" ==> null") - atPos(tree.pos) { typer.typed { Literal(Constant(null)) }} - - case Apply(Select(t, nme.isEmpty),List()) // t.isEmpty -> t eq null - if settings.Xkilloption.value && wasOption(t.tpe) => - if(settings.debug.value) - Console.println("[k.o. (isEmpty)]"+tree+" ==> "+t+" eq null") - IsNull(t) - - case Apply(Select(t, nme.get),List()) // t.get -> t if T <: Option[AnyRef] - if settings.Xkilloption.value && wasOptionRef(t.tpe) => - if(settings.debug.value) - Console.println("[k.o. (get)]"+tree+" ==> "+t) - t - - case Apply(TypeApply(Select(t, nme.isInstanceOf_), List(tt)), _) if settings.Xkilloption.value && wasOptionRef(t.tpe) => - NotNull(t) - - case Apply(ta@TypeApply(s@Select(t, nme.asInstanceOf_), List(tt)), List()) if settings.Xkilloption.value && wasOptionRef(t.tpe) => //&& definitions.isSomeType(tt.tpe) => - val tpe = tt.tpe - copy.Apply(tree, copy.TypeApply(ta, transform(s), List(TypeTree(tpe))), List()) - case Select(t, n) // methods ... - if settings.Xkilloption.value && wasOptionRef(t.tpe) && n != nme.get => - - val s = transform(t) - val tmp = currentOwner.newVariable(tree.pos, cunit.fresh.newName("opt")).setInfo(s.tpe) // careful: pos has special meaning - val nt = atPos(tree.pos) { typer.typed { - Block(List(ValDef(tmp,s)), - If(NotNull(Ident(tmp)), - Apply(Select(New(TypeTree(definitions.someType(s.tpe))), nme.CONSTRUCTOR), List(Ident(tmp))), - gen.mkAttributedRef(definitions.NoneClass) ) - ) - }} - val ntree = copy.Select(tree,nt,n) - if(settings.debug.value) - Console.println("[k.o. (select)]"+tree+" ==> "+ntree) - ntree - - case Select(t, n) // methods ... - if settings.Xkilloption.value && wasOption(t.tpe) && n != nme.get && t.symbol!=null => - - def ntpe = if(t.tpe.isInstanceOf[SingleType]) t.tpe.widen else t.tpe - val otpe = atPhase(phase.prev){ntpe} - val nt = atPos(tree.pos) { typer.typed { - If(NotNull(t), - t, - gen.mkAttributedRef(definitions.NoneClass)) - }} - val ntree = copy.Select(tree,nt,n) - if(settings.debug.value) - Console.println("[k.o. (select2)]"+tree+" ==> "+ntree) - ntree - - case _ // e.g. map.get("bar") -> fix type - if settings.Xkilloption.value - && wasOptionRef(tree.tpe) => - if(settings.debug.value) { - Console.println("[k.o.] setting type of "+tree+" which is a "+tree.getClass) - Console.println(" ... to "+getOptionArg(tree.tpe)) - } - - val argtpe = getOptionArg(tree.tpe) - transform(tree.setType(argtpe)) // need to transform? super.transform? - - // </removeOption> - case Template(parents, decls) => val newDefs = new ListBuffer[Tree] atOwner(tree, currentOwner) { @@ -560,15 +436,6 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter } else { //todo: see whether we can move this to transformInfo if (sym.owner.isTrait && (sym hasFlag (ACCESSOR | SUPERACCESSOR))) sym.makeNotPrivate(sym.owner); //(2) - - if(settings.Xkilloption.value) { - //Console.println("vparamss"+vparamss) - val nvparamss = vparamss.map { x => super.transformValDefs(x) } - //Console.println("nvparamss"+nvparamss) - val ntpt = if(wasOptionRef(tpt.tpe)) TypeTree(getOptionArg(tpt.tpe)) else tpt - - return super.transform(copy.DefDef(tree,mods,name,tparams,nvparamss, ntpt, rhs)) - } super.transform(tree) } @@ -617,7 +484,7 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter } val nselector = transform(selector) - assert(nselector.tpe =:= selector.tpe || settings.Xkilloption.value) + assert(nselector.tpe =:= selector.tpe) val ncases = transformCaseDefs(cases) ExplicitOuter.this.resultType = tree.tpe @@ -646,10 +513,6 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter System.exit(-1) null } - //<removeOption> - case TypeTree() if settings.Xkilloption.value => - tree setType transformInfo(currentOwner, tree.tpe) - tree case _ => val x = super.transform(tree) |