diff options
author | Martin Odersky <odersky@gmail.com> | 2007-01-31 15:41:15 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2007-01-31 15:41:15 +0000 |
commit | 485a79aa79142c2740a64f8e7a83036067c7ee9c (patch) | |
tree | ec7b727adab3da022e61ddff8692b118333e1cb6 | |
parent | e566c7126ce2a593e9d5711784667a25bc92d514 (diff) | |
download | scala-485a79aa79142c2740a64f8e7a83036067c7ee9c.tar.gz scala-485a79aa79142c2740a64f8e7a83036067c7ee9c.tar.bz2 scala-485a79aa79142c2740a64f8e7a83036067c7ee9c.zip |
made pattern constructors termsymbols
20 files changed, 141 insertions, 127 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/Trees.scala b/src/compiler/scala/tools/nsc/ast/Trees.scala index 4c760c7e12..9de909d08c 100644 --- a/src/compiler/scala/tools/nsc/ast/Trees.scala +++ b/src/compiler/scala/tools/nsc/ast/Trees.scala @@ -436,7 +436,7 @@ trait Trees requires Global { vparamss map (.map (vd => { val ret = ValDef(Modifiers(vd.mods.flags & IMPLICIT | PARAM) setAttr vd.mods.attributes, vd.name, vd.tpt.duplicate, EmptyTree).setPos(vd.pos) - if (false /*(bq) was: inIDE*/ && vd.symbol != NoSymbol) + if (false/*inIDE*/ && vd.symbol != NoSymbol) ret.symbol = vd.symbol ret })); diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 6f26578833..c9e5d94a0d 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -508,7 +508,7 @@ trait Parsers requires SyntaxAnalyzer { if (isSymLit) { atPos(pos) { var symid = scalaDot(nme.Symbol) - if (isPattern) { symid = convertToTypeId(symid) } + if (isPattern) { symid = /*convertToTypeId*/(symid) } val symobj = Apply(symid, List(t)) if (isPattern) symobj else Select(symobj, nme.intern) } @@ -1356,9 +1356,9 @@ trait Parsers requires SyntaxAnalyzer { } else */ if (in.token == LPAREN) { - atPos(in.currentPos) { Apply(convertToTypeId(t), argumentPatterns()) } + atPos(in.currentPos) { Apply(/*convertToTypeId*/(t), argumentPatterns()) } } else if (in.token == LBRACE) { - atPos(in.currentPos) { Apply(convertToTypeId(t), List(tuplePattern())) } + atPos(in.currentPos) { Apply(/*convertToTypeId*/(t), List(tuplePattern())) } } else t case USCORE => atPos(in.skipToken()) { Ident(nme.WILDCARD) } diff --git a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala index 1d6b1319a7..e06581bdbc 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala @@ -36,6 +36,7 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser def _PrefixedAttribute = global.newTypeName("PrefixedAttribute") def _UnprefixedAttribute = global.newTypeName("UnprefixedAttribute") def _Elem = global.newTypeName("Elem") + def __Elem = global.newTermName("Elem") def _Group = global.newTypeName("Group") def _Unparsed = global.newTypeName("Unparsed") def _Seq = global.newTypeName("Seq") @@ -52,6 +53,7 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser def _Some = global.newTypeName("Some") def _ProcInstr = global.newTypeName("ProcInstr") def _Text = global.newTypeName("Text") + def __Text = global.newTermName("Text") def _EntityRef = global.newTypeName("EntityRef") final def _md = global.newTermName("$md") final def _scope = global.newTermName("$scope") @@ -77,7 +79,9 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser private def _scala_xml_Comment = _scala_xml(_Comment) private def _scala_xml_ProcInstr = _scala_xml(_ProcInstr) private def _scala_xml_Text = _scala_xml(_Text) + private def _scala_xml__Text = _scala_xml(__Text) private def _scala_xml_Elem = _scala_xml(_Elem) + private def _scala_xml__Elem = _scala_xml(__Elem) private def _scala_xml_Attribute = _scala_xml(_Attribute) private def _scala_xml_Group = _scala_xml(_Group) private def _scala_xml_Unparsed = _scala_xml(_Unparsed) @@ -103,7 +107,7 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser if (isPattern) { convertToTextPat(children) atPos (pos) { //@todo maybe matching on attributes, scope? - Apply( _scala_xml_Elem, List( + Apply( _scala_xml__Elem, List( pre, label, Ident( nme.WILDCARD ) /* md */ , Ident( nme.WILDCARD )) /* scope */ ::: children.toList ) } } else { @@ -131,7 +135,7 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser } // create scala.xml.Text here <: scala.xml.Node - def makeTextPat(txt: Tree) = Apply(_scala_xml_Text, List(txt)) + def makeTextPat(txt: Tree) = Apply(_scala_xml__Text, List(txt)) def makeText1(txt: Tree) = New(_scala_xml_Text, LL(txt)) diff --git a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala index e2d2fb4a97..bbc5cde1dc 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala @@ -94,7 +94,7 @@ abstract class TreeBuilder { def makeTuplePattern(trees: List[Tree]): Tree = trees match { case List() => Literal(()) - case _ => makeTuple(trees, true) + case _ => makeTuple(trees, false) } def makeTupleType(trees: List[Tree], flattenUnary: boolean): Tree = trees match { @@ -129,7 +129,7 @@ abstract class TreeBuilder { Apply(Select(right, op.encode), List(Ident(x)))) } } else { - Apply(Ident(op.encode.toTypeName), left :: arguments) + Apply(Ident(op.encode), left :: arguments) } } @@ -303,7 +303,7 @@ abstract class TreeBuilder { val rhs1 = makeForYield( List(ValFrom(pos, patX1, rhs)), Block(pdefs, makeTupleTerm(ids, true))) - makeFor(mapName, flatMapName, ValFrom(pos, makeTuple(pat :: pats, true), rhs1) :: rest1, body) + makeFor(mapName, flatMapName, ValFrom(pos, makeTuple(pat :: pats, false), rhs1) :: rest1, body) case _ => EmptyTree //may happen for erroneous input } @@ -324,7 +324,7 @@ abstract class TreeBuilder { case g :: Nil => g case ValFrom(pos1, pat1, rhs1) :: gs2 => val ValFrom(pos2, pat2, rhs2) = combine(gs2) - ValFrom(pos1, makeTuple(List(pat1, pat2), true), Apply(Select(rhs1, nme.zip), List(rhs2))) + ValFrom(pos1, makeTuple(List(pat1, pat2), false), Apply(Select(rhs1, nme.zip), List(rhs2))) } makeForYield(List(combine(gs)), body) } diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala index b4ea587f04..d1478f629c 100644 --- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala +++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala @@ -412,7 +412,7 @@ trait Definitions requires SymbolTable { private def newTypeParam(owner: Symbol, index: int): Symbol = owner.newTypeParameter(NoPos, "T" + index) - .setInfo(TypeBounds(AllClass.typeConstructor, AnyClass.typeConstructor)) + .setInfo(mkTypeBounds(AllClass.typeConstructor, AnyClass.typeConstructor)) val boxedClass = new HashMap[Symbol, Symbol] val unboxMethod = new HashMap[Symbol, Symbol] // Type -> Method @@ -568,7 +568,7 @@ trait Definitions requires SymbolTable { } def addModuleMethod(clazz: Symbol, name: Name, value: Any): Unit = { val owner = clazz.linkedClassOfClass - newParameterlessMethod(owner, name, ConstantType(Constant(value))) + newParameterlessMethod(owner, name, mkConstantType(Constant(value))) } addModuleMethod(ByteClass, "MinValue", java.lang.Byte.MIN_VALUE) addModuleMethod(ByteClass, "MaxValue", java.lang.Byte.MAX_VALUE) diff --git a/src/compiler/scala/tools/nsc/symtab/Symbols.scala b/src/compiler/scala/tools/nsc/symtab/Symbols.scala index a6ade53cf6..5d8fcac6b8 100644 --- a/src/compiler/scala/tools/nsc/symtab/Symbols.scala +++ b/src/compiler/scala/tools/nsc/symtab/Symbols.scala @@ -1230,7 +1230,7 @@ trait Symbols requires SymbolTable { val period = thisTypePeriod if (period != currentPeriod) { thisTypePeriod = currentPeriod - if (!isValid(period)) thisTypeCache = ThisType(this) + if (!isValid(period)) thisTypeCache = mkThisType(this) } thisTypeCache } diff --git a/src/compiler/scala/tools/nsc/symtab/Types.scala b/src/compiler/scala/tools/nsc/symtab/Types.scala index 24acb66646..bdf4a4edf8 100644 --- a/src/compiler/scala/tools/nsc/symtab/Types.scala +++ b/src/compiler/scala/tools/nsc/symtab/Types.scala @@ -104,7 +104,7 @@ trait Types requires SymbolTable { * for a reference denoting an abstract type, its bounds, * for all other types, a TypeBounds type all of whose bounds are this type. */ - def bounds: TypeBounds = TypeBounds(this, this) + def bounds: TypeBounds = mkTypeBounds(this, this) /** For a class or intersection type, its parents. * For a TypeBounds type, the parents of its hi bound. @@ -581,7 +581,7 @@ trait Types requires SymbolTable { /** A class for this-types of the form <sym>.this.type */ - abstract case class ThisType(sym: Symbol) extends SingletonType { + case class ThisType(sym: Symbol) extends SingletonType { //assert(sym.isClass && !sym.isModuleClass || sym.isRoot, sym) override def isTrivial: boolean = sym.isPackageClass // override def isNonNull = true @@ -604,7 +604,7 @@ trait Types requires SymbolTable { * Cannot be created directly; one should always use * <code>singleType</code> for creation. */ - abstract case class SingleType(pre: Type, sym: Symbol) extends SingletonType { + case class SingleType(pre: Type, sym: Symbol) extends SingletonType { override val isTrivial: boolean = pre.isTrivial // override def isNullable = supertype.isNullable // override def isNonNull = supertype.isNonNull @@ -640,7 +640,7 @@ trait Types requires SymbolTable { else pre.prefixString + sym.nameString + "." } - abstract case class SuperType(thistpe: Type, supertp: Type) extends SingletonType { + case class SuperType(thistpe: Type, supertp: Type) extends SingletonType { override val isTrivial: boolean = thistpe.isTrivial && supertp.isTrivial // override def isNonNull = true; override def symbol = thistpe.symbol @@ -655,7 +655,7 @@ trait Types requires SymbolTable { /** A class for the bounds of abstract types and type parameters */ - abstract case class TypeBounds(lo: Type, hi: Type) extends SubType { + case class TypeBounds(lo: Type, hi: Type) extends SubType { override val isTrivial: boolean = lo.isTrivial && hi.isTrivial def supertype: Type = hi override def bounds: TypeBounds = this @@ -829,8 +829,8 @@ trait Types requires SymbolTable { * Cannot be created directly; * one should always use <code>refinedType</code> for creation. */ - abstract case class RefinedType(override val parents: List[Type], - override val decls: Scope) extends CompoundType + case class RefinedType(override val parents: List[Type], + override val decls: Scope) extends CompoundType /** A class representing a class info */ @@ -853,7 +853,7 @@ trait Types requires SymbolTable { * * @param value ... */ - abstract case class ConstantType(value: Constant) extends SingletonType { + case class ConstantType(value: Constant) extends SingletonType { assert(value.tpe.symbol != UnitClass) override def isTrivial: boolean = true override def symbol: Symbol = value.tpe.symbol @@ -874,7 +874,7 @@ trait Types requires SymbolTable { * @param sym ... * @param args ... */ - abstract case class TypeRef(pre: Type, sym: Symbol, args: List[Type]) extends Type { + case class TypeRef(pre: Type, sym: Symbol, args: List[Type]) extends Type { assert(!checkMalformedSwitch || !sym.isAbstractType || pre.isStable || pre.isError) assert(!pre.isInstanceOf[ClassInfoType], this) assert(!sym.isTypeParameterOrSkolem || pre == NoPrefix, this) @@ -1125,7 +1125,7 @@ trait Types requires SymbolTable { override def bounds: TypeBounds = { val oftp = tp.bounds oftp match { - case TypeBounds(lo, hi) if((lo eq this) && (hi eq this)) => TypeBounds(this,this) + case TypeBounds(lo, hi) if((lo eq this) && (hi eq this)) => mkTypeBounds(this,this) case _ => oftp } } @@ -1199,7 +1199,7 @@ trait Types requires SymbolTable { } /** The canonical creator for this-types */ - def ThisType(sym: Symbol): Type = + def mkThisType(sym: Symbol): Type = if (phase.erasedTypes) sym.tpe else unique(new ThisType(sym) with UniqueType) /** The canonical creator for single-types */ @@ -1211,7 +1211,7 @@ trait Types requires SymbolTable { if (phase.erasedTypes) sym.tpe.resultType else if (sym.isRootPackage) - ThisType(RootClass) + mkThisType(RootClass) else { var sym1 = rebind(pre, sym) val pre1 = removeSuper(pre, sym1) @@ -1227,12 +1227,12 @@ trait Types requires SymbolTable { } /** The canonical creator for super-types */ - def SuperType(thistp: Type, supertp: Type): Type = + def mkSuperType(thistp: Type, supertp: Type): Type = if (phase.erasedTypes) supertp else unique(new SuperType(thistp, supertp) with UniqueType) /** The canonical creator for type bounds */ - def TypeBounds(lo: Type, hi: Type): TypeBounds = + def mkTypeBounds(lo: Type, hi: Type): TypeBounds = unique(new TypeBounds(lo, hi) with UniqueType) def refinementOfClass(clazz: Symbol, parents: List[Type], decls: Scope) = @@ -1274,7 +1274,7 @@ trait Types requires SymbolTable { } /** the canonical creator for a constant type */ - def ConstantType(value: Constant): ConstantType = + def mkConstantType(value: Constant): ConstantType = unique(new ConstantType(value) with UniqueType) @@ -1434,7 +1434,7 @@ trait Types requires SymbolTable { val thistp1 = this(thistp) val supertp1 = this(supertp) if ((thistp1 eq thistp) && (supertp1 eq supertp)) tp - else SuperType(thistp1, supertp1) + else mkSuperType(thistp1, supertp1) case TypeRef(pre, sym, args) => val pre1 = this(pre) //val args1 = List.mapConserve(args)(this) @@ -1452,7 +1452,7 @@ trait Types requires SymbolTable { variance = -variance val hi1 = this(hi) if ((lo1 eq lo) && (hi1 eq hi)) tp - else TypeBounds(lo1, hi1) + else mkTypeBounds(lo1, hi1) case BoundedWildcardType(bounds) => val bounds1 = this(bounds) if (bounds1 eq bounds) tp @@ -1775,7 +1775,7 @@ trait Types requires SymbolTable { def apply(tp: Type): Type = tp match { case ThisType(sym) if (sym.isModuleClass) => val sym1 = adaptToNewRun(sym.owner.thisType, sym) - if (sym1 == sym) tp else ThisType(sym1) + if (sym1 == sym) tp else mkThisType(sym1) case SingleType(pre, sym) => if (sym.isPackage) tp else { @@ -2288,7 +2288,7 @@ trait Types requires SymbolTable { MethodType(pts, lub0(matchingRestypes(ts, pts))) case ts @ TypeBounds(_, _) :: rest => assert(false) - TypeBounds(glb(ts map (.bounds.lo), depth), lub(ts map (.bounds.hi), depth)) + mkTypeBounds(glb(ts map (.bounds.lo), depth), lub(ts map (.bounds.hi), depth)) case ts => val closures: List[Array[Type]] = ts map (.closure) val lubBaseTypes: Array[Type] = lubArray(closures, depth) @@ -2317,7 +2317,7 @@ trait Types requires SymbolTable { proto.cloneSymbol(lubType.symbol).setInfo(symtypes.head) else { def lubBounds(bnds: List[TypeBounds]): TypeBounds = - TypeBounds(glb(bnds map (.lo), depth-1), lub(bnds map (.hi), depth-1)) + mkTypeBounds(glb(bnds map (.lo), depth-1), lub(bnds map (.hi), depth-1)) proto.owner.newAbstractType(proto.pos, proto.name) .setInfo(lubBounds(symtypes map (.bounds))) } @@ -2370,7 +2370,7 @@ trait Types requires SymbolTable { case ts @ MethodType(pts, _) :: rest => MethodType(pts, glb0(matchingRestypes(ts, pts))) case ts @ TypeBounds(_, _) :: rest => - TypeBounds(lub(ts map (.bounds.lo), depth), glb(ts map (.bounds.hi), depth)) + mkTypeBounds(lub(ts map (.bounds.lo), depth), glb(ts map (.bounds.hi), depth)) case ts => try { val glbOwner = commonOwner(ts) @@ -2397,13 +2397,13 @@ trait Types requires SymbolTable { def glbBounds(bnds: List[Type]): TypeBounds = { val lo = lub(bnds map (.bounds.lo), depth-1) val hi = glb(bnds map (.bounds.hi), depth-1) - if (lo <:< hi) TypeBounds(lo, hi) + if (lo <:< hi) mkTypeBounds(lo, hi) else throw new MalformedClosure(bnds) } val symbounds = symtypes filter isTypeBound var result: Type = if (symbounds.isEmpty) - TypeBounds(AllClass.tpe, AnyClass.tpe) + mkTypeBounds(AllClass.tpe, AnyClass.tpe) else glbBounds(symbounds) for (val t <- symtypes; !isTypeBound(t)) if (result.bounds containsType t) result = t diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala index a44d001084..bc348962b6 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala @@ -429,7 +429,7 @@ abstract class ClassfileParser { val info = pool.getType(in.nextChar) val sym = getOwner(jflags) .newValue(NoPos, name).setFlag(sflags) - sym.setInfo(if ((jflags & JAVA_ACC_ENUM) == 0) info else ConstantType(Constant(sym))) + sym.setInfo(if ((jflags & JAVA_ACC_ENUM) == 0) info else mkConstantType(Constant(sym))) setPrivateWithin(sym, jflags) parseAttributes(sym, info) getScope(jflags).enter(sym) @@ -496,12 +496,12 @@ abstract class ClassfileParser { case variance @ ('+' | '-' | '*') => index = index + 1 val bounds = variance match { - case '+' => TypeBounds(definitions.AllRefClass.typeConstructor, - sig2type(tparams, covariant)) - case '-' => TypeBounds(sig2type(tparams, covariant), - definitions.AnyRefClass.typeConstructor) - case '*' => TypeBounds(definitions.AllRefClass.typeConstructor, - definitions.AnyRefClass.typeConstructor) + case '+' => mkTypeBounds(definitions.AllRefClass.typeConstructor, + sig2type(tparams, covariant)) + case '-' => mkTypeBounds(sig2type(tparams, covariant), + definitions.AnyRefClass.typeConstructor) + case '*' => mkTypeBounds(definitions.AllRefClass.typeConstructor, + definitions.AnyRefClass.typeConstructor) } val name = fresh.newName("T_" + sym.name) val newtparam = @@ -572,8 +572,8 @@ abstract class ClassfileParser { if (sig(index) != ':') // guard against empty class bound ts += sig2type(tparams, false) } - s.setInfo(TypeBounds(definitions.AllRefClass.typeConstructor, - intersectionType(ts.toList, sym))) + s.setInfo(mkTypeBounds(definitions.AllRefClass.typeConstructor, + intersectionType(ts.toList, sym))) newTParams += s } index = index + 1 @@ -627,7 +627,7 @@ abstract class ClassfileParser { case nme.ConstantValueATTR => val c = pool.getConstant(in.nextChar) val c1 = convertTo(c, symtype) - if (c1 ne null) sym.setInfo(ConstantType(c1)) + if (c1 ne null) sym.setInfo(mkConstantType(c1)) else Console.println("failure to convert " + c + " to " + symtype); //debug case nme.InnerClassesATTR => if (!isScala) parseInnerClasses() else in.skip(attrLen) diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/MetaParser.scala b/src/compiler/scala/tools/nsc/symtab/classfile/MetaParser.scala index 83b5147ef9..e68e6914f5 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/MetaParser.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/MetaParser.scala @@ -74,7 +74,7 @@ abstract class MetaParser{ val hi = if (token == "<") { nextToken(); parseType() } else definitions.AnyClass.tpe - sym.setInfo(TypeBounds(lo, hi)) + sym.setInfo(mkTypeBounds(lo, hi)) locals enter sym; sym } diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/UnPickler.scala b/src/compiler/scala/tools/nsc/symtab/classfile/UnPickler.scala index 5dad581a45..4aa93590c4 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/UnPickler.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/UnPickler.scala @@ -211,15 +211,15 @@ abstract class UnPickler { case NOPREFIXtpe => NoPrefix case THIStpe => - ThisType(readSymbolRef()) + mkThisType(readSymbolRef()) case SINGLEtpe => singleType(readTypeRef(), readSymbolRef()) case CONSTANTtpe => - ConstantType(readConstantRef()) + mkConstantType(readConstantRef()) case TYPEREFtpe => rawTypeRef(readTypeRef(), readSymbolRef(), until(end, readTypeRef)) case TYPEBOUNDStpe => - TypeBounds(readTypeRef(), readTypeRef()) + mkTypeBounds(readTypeRef(), readTypeRef()) case REFINEDtpe => val clazz = readSymbolRef() /* diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala index fc4e9a1769..7615e04cf8 100644 --- a/src/compiler/scala/tools/nsc/transform/Erasure.scala +++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala @@ -138,7 +138,7 @@ abstract class Erasure extends AddInterfaces with typechecker.Analyzer { else if (sym == Object_isInstanceOf || sym == ArrayClass) PolyType(sym.info.typeParams, erasure(sym.info.resultType)) else if (sym.isAbstractType) - TypeBounds(WildcardType, WildcardType) + mkTypeBounds(WildcardType, WildcardType) else if (sym.isTerm && sym.owner == ArrayClass) { if (sym.isClassConstructor) tp match { diff --git a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala index 7509938c2f..6be8b472d4 100644 --- a/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala +++ b/src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala @@ -386,9 +386,7 @@ abstract class ExplicitOuter extends InfoTransform with TransMatcher with Patter val rhs = ExplicitOuterTransformer.this.transform(path) localTyper.typed { atPos(currentClass.pos) { - val result = DefDef(outerAcc, {vparamss=>rhs}) - //Console.println("==> "+result) - result + DefDef(outerAcc, {vparamss=>rhs}) } } } diff --git a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala index 21f8afe7ac..7323b17fa3 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala @@ -41,7 +41,7 @@ abstract class ConstantFolder { private def fold(tree: Tree, compX: => Constant): Tree = try { val x = compX - if ((x ne null) && x.tag != UnitTag) tree setType ConstantType(x) + if ((x ne null) && x.tag != UnitTag) tree setType mkConstantType(x) else tree } catch { case _: ArithmeticException => tree // the code will crash at runtime, diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index 589e36b5d3..a52773fbfd 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -784,7 +784,7 @@ trait Infer requires Analyzer { if (settings.debug.value) log("inconsistent: "+tparam+" "+lo+" "+hi) } else if (!((lo <:< tparam.info.bounds.lo) && (tparam.info.bounds.hi <:< hi))) { context.nextEnclosing(.tree.isInstanceOf[CaseDef]).pushTypeBounds(tparam) - tparam setInfo TypeBounds(lo, hi) + tparam setInfo mkTypeBounds(lo, hi) if (settings.debug.value) log("new bounds of " + tparam + " = " + tparam.info) } else { if (settings.debug.value) log("redundant: "+tparam+" "+tparam.info+"/"+lo+" "+hi) diff --git a/src/compiler/scala/tools/nsc/typechecker/Namers.scala b/src/compiler/scala/tools/nsc/typechecker/Namers.scala index 8e4e0d317e..4bede93d85 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Namers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Namers.scala @@ -32,7 +32,7 @@ trait Namers requires Analyzer { if (tparam == sym || !(tparams contains tparam)) tp else rawTypeRef(NoPrefix, tparam, args)) case SingleType(pre, sym) if (sym.isThisSkolem) => - ThisType(sym.deSkolemize) + mkThisType(sym.deSkolemize) case PolyType(tparams1, restpe) => new DeSkolemizeMap(tparams1 ::: tparams).mapOver(tp) case ClassInfoType(parents, decls, clazz) => @@ -253,7 +253,7 @@ trait Namers requires Analyzer { context.make(tree, tree.symbol.moduleClass, tree.symbol.info.decls)) namer.enterSyms(stats) case ClassDef(mods, name, tparams, _, impl) => - if ((mods.flags & (CASE | ABSTRACT)) == CASE) { // enter case factory method. + if ((mods.flags & CASE) != 0) { // enter case factory method. tree.symbol = enterCaseFactorySymbol( tree.pos, mods.flags & AccessFlags | METHOD | CASE, name.toTermName) tree.symbol.setInfo(namerOf(tree.symbol).caseFactoryCompleter(tree)) @@ -597,7 +597,7 @@ trait Namers requires Analyzer { if (lt.isError) lt = AllClass.tpe var ht = typer.typedType(hi).tpe if (ht.isError) ht = AnyClass.tpe - TypeBounds(lt, ht) + mkTypeBounds(lt, ht) case Import(expr, selectors) => val expr1 = typer.typedQualifier(expr) @@ -754,6 +754,7 @@ trait Namers requires Analyzer { checkNoConflict(PRIVATE, PROTECTED) checkNoConflict(PRIVATE, OVERRIDE) checkNoConflict(DEFERRED, FINAL) + checkNoConflict(ABSTRACT, CASE) } } diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 39a3441f20..d35f5d8e21 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -480,7 +480,7 @@ trait Typers requires Analyzer { * and it is not a constructor, apply it to () * otherwise issue an error * (5) Convert constructors in a pattern as follows: - * (5.1) If constructor refers to a case class, set tree's type to the unique + * (5.1) If constructor refers to a case class factory, set tree's type to the unique * instance of its primary constructor that is a subtype of the expected type. * (5.2) If constructor refers to an exractor, convert to application of * unapply or unapplySeq method. @@ -508,7 +508,7 @@ trait Typers requires Analyzer { case TypeRef(_, sym, List(arg)) if ((mode & EXPRmode) != 0 && sym == ByNameParamClass) => // (2) adapt(tree setType arg, mode, pt) - case PolyType(tparams, restpe) if ((mode & TAPPmode) == 0) => // (3) + case PolyType(tparams, restpe) if ((mode & (TAPPmode | PATTERNmode)) == 0) => // (3) val tparams1 = cloneSymbols(tparams) val tree1 = if (tree.isType) tree else TypeApply(tree, tparams1 map (tparam => @@ -548,61 +548,39 @@ trait Typers requires Analyzer { } case _ => if (tree.isType) { - val clazz = tree.tpe.symbol - if ((mode & PATTERNmode) != 0) { // (5) - if (tree.tpe.isInstanceOf[MethodType]) { - tree // everything done already - } else { - clazz.initialize - if (clazz.hasFlag(CASE)) { // (5.1) - val tree1 = TypeTree(clazz.primaryConstructor.tpe.asSeenFrom(tree.tpe.prefix, clazz.owner)) setOriginal tree - try { - inferConstructorInstance(tree1, clazz.typeParams, widen(pt)) - } catch { - case tpe : TypeError => throw tpe - case t : Throwable => - logError("CONTEXT: " + context.unit.source.dbg(tree.pos), t) - throw t - } - tree1 - } else if (!settings.Xunapply.value && clazz.isNonBottomSubClass(SeqClass)) { // (5.2) - val restpe = pt.baseType(clazz) - restpe.baseType(SeqClass) match { - case TypeRef(pre, seqClass, args) => - tree.setType(MethodType(List(typeRef(pre, RepeatedParamClass, args)), restpe)) - case NoType => - errorTree(tree, "expected pattern type "+pt + - " does not conform to sequence "+clazz) - case ErrorType => - setError(tree) - } - } else if (settings.Xunapply.value) { /*unapply (5.3) */ - // fix symbol -- we are using the module not the class - val consp = if (clazz.isModule) clazz else { - val obj = clazz.linkedModuleOfClass - if (obj != NoSymbol) tree.setSymbol(obj) - obj - } - if (definitions.unapplyMember(consp.tpe).exists) - atPos(tree.pos) { - //Console.println("UNAPPLY1: "+gen.mkAttributedRef(tree.tpe.prefix,consp)) - gen.mkAttributedRef(tree.tpe.prefix,consp) - } - // needs member type, but member of what? ^^^` - // see test/pending/pos/unapplyNeedsMemberType.scala - else errorTree(tree, "" + clazz + " is not a case class, nor does it have unapply/unapplySeq method") - } else { - errorTree(tree, "" + clazz + " is neither a case class nor a sequence class") - } - } - } else if ((mode & FUNmode) != 0) { + if ((mode & FUNmode) != 0) { tree } else if (tree.hasSymbol && !tree.symbol.typeParams.isEmpty) { // (7) - errorTree(tree, ""+clazz+" takes type parameters") + errorTree(tree, tree.symbol+" takes type parameters") } else tree match { // (6) case TypeTree() => tree case _ => TypeTree(tree.tpe) setOriginal(tree) } + } else if ((mode & (PATTERNmode | FUNmode)) == (PATTERNmode | FUNmode)) { // (5) + val constr = tree.symbol.filter(.isCaseFactory) + if (constr != NoSymbol) { + val clazz = constr.tpe.finalResultType.symbol + assert(clazz hasFlag CASE, tree) + val prefix = tree.tpe.finalResultType.prefix + val tree1 = TypeTree(clazz.primaryConstructor.tpe.asSeenFrom(prefix, clazz.owner)) setOriginal tree + try { + inferConstructorInstance(tree1, clazz.typeParams, widen(pt)) + } catch { + case tpe : TypeError => throw tpe + case t : Throwable => + logError("CONTEXT: " + context.unit.source.dbg(tree.pos), t) + throw t + } + tree1 + } else { + val extractor = tree.symbol.filter(sym => definitions.unapplyMember(sym.tpe).exists) + if (extractor != NoSymbol) { + tree setSymbol extractor + } else { + Console.println(tree.symbol+tree.symbol.locationString+":"+tree.symbol.tpe) + errorTree(tree, tree.symbol + " is not a case class constructor, nor does it have an unapply/unapplySeq method") + } + } } else if ((mode & (EXPRmode | FUNmode)) == (EXPRmode | FUNmode) && !tree.tpe.isInstanceOf[MethodType] && !tree.tpe.isInstanceOf[OverloadedType] && ((mode & TAPPmode) == 0 || tree.tpe.typeParams.isEmpty) && @@ -628,9 +606,9 @@ trait Typers requires Analyzer { } else { if ((mode & PATTERNmode) != 0) { if ((tree.symbol ne null) && tree.symbol.isModule) - inferModulePattern(tree, pt) + inferModulePattern(tree, pt) if (isPopulated(tree.tpe, approximateAbstracts(pt))) - return tree + return tree } val tree1 = constfold(tree, pt) // (10) (11) if (tree1.tpe <:< pt) adapt(tree1, mode, pt) @@ -1402,7 +1380,7 @@ trait Typers requires Analyzer { case _ => errorTree(arg, "constant required") } val arrayConst = new ArrayConstant(elems, restpe) - Literal(arrayConst) setType ConstantType(arrayConst) + Literal(arrayConst) setType mkConstantType(arrayConst) } else constfold(copy.Apply(tree, fun, args1).setType(ifPatternSkipFormals(restpe))) @@ -1589,7 +1567,7 @@ trait Typers requires Analyzer { val qual1 = if ((args1 ne null) && !pt.isError) { def templateArgType(arg: Tree) = - new BoundedWildcardType(TypeBounds(arg.tpe, AnyClass.tpe)) + new BoundedWildcardType(mkTypeBounds(arg.tpe, AnyClass.tpe)) adaptToMember(qual, name, MethodType(args1 map templateArgType, pt)) } else qual if (qual1 ne qual) { @@ -1726,6 +1704,13 @@ trait Typers requires Analyzer { var defSym: Symbol = tree.symbol // the directly found symbol var pre: Type = NoPrefix // the prefix type of defSym, if a class member var qual: Tree = EmptyTree // the qualififier tree if transformed tree is a select + // if we are in a constructor of a pattern, ignore all methods + // which are not case factories (note if we don't do that + // case x :: xs in class List would return the :: method. + def qualifies(sym: Symbol): boolean = + sym.exists && + ((mode & PATTERNmode | FUNmode) != (PATTERNmode | FUNmode) || + !sym.isSourceMethod || sym.isCaseFactory) if (defSym == NoSymbol) { var defEntry: ScopeEntry = null // the scope entry of defSym, if defined in a local scope @@ -1734,12 +1719,12 @@ trait Typers requires Analyzer { while (defSym == NoSymbol && cx != NoContext) { pre = cx.enclClass.prefix defEntry = cx.scope.lookupEntry(name) - if ((defEntry ne null) && defEntry.sym.exists) { + if ((defEntry ne null) && qualifies(defEntry.sym)) { defSym = defEntry.sym } else { cx = cx.enclClass defSym = pre.member(name) filter ( - sym => sym.exists && context.isAccessible(sym, pre, false)) + sym => qualifies(sym) && context.isAccessible(sym, pre, false)) if (defSym == NoSymbol) cx = cx.outer } } @@ -1766,6 +1751,7 @@ trait Typers requires Analyzer { (context.unit ne null) && defSym.sourceFile != context.unit.source.file)) defSym = NoSymbol +/* /*<unapply>*/ if(settings.Xunapply.value) // unapply: in patterns, look for an object if can't find type @@ -1774,6 +1760,7 @@ trait Typers requires Analyzer { return tree setSymbol mod.symbol setType mod.tpe } /*</unapply>*/ +*/ if (defSym.exists) { if (impSym.exists) @@ -1897,7 +1884,7 @@ trait Typers requires Analyzer { context.owner.newAliasType(tree.pos, name) setInfo pt else context.owner.newAbstractType(tree.pos, name) setInfo - TypeBounds(AllClass.tpe, AnyClass.tpe) + mkTypeBounds(AllClass.tpe, AnyClass.tpe) if (vble.name == nme.WILDCARD.toTypeName) context.scope.enter(vble) else namer.enterInScope(vble) tree setType vble.tpe @@ -2113,7 +2100,7 @@ trait Typers requires Analyzer { ErrorType } } - tree setSymbol clazz setType SuperType(selftype, owntype) + tree setSymbol clazz setType mkSuperType(selftype, owntype) } case This(qual) => @@ -2155,7 +2142,7 @@ trait Typers requires Analyzer { case Literal(value) => tree setType ( if (value.tag == UnitTag) UnitClass.tpe - else ConstantType(value)) + else mkConstantType(value)) case AttributedTypeTree(attribs, tpt) => attribs.foreach(t => typed(t, EXPRmode, WildcardType)) @@ -2202,7 +2189,7 @@ trait Typers requires Analyzer { } case WildcardTypeTree(lo, hi) => - tree setType TypeBounds(typedType(lo).tpe, typedType(hi).tpe) + tree setType mkTypeBounds(typedType(lo).tpe, typedType(hi).tpe) case TypeTree() => // we should get here only when something before failed @@ -2452,7 +2439,7 @@ trait Typers requires Analyzer { val best = (NoImplicitInfo /: applicable) ((best, alt) => if (improves(alt, best)) alt else best) if (best == NoImplicitInfo) EmptyTree else { - val competing = applicable dropWhile (alt => best == alt || improves(best, alt) || alt.sym.owner == PredefModule.moduleClass) + val competing = applicable dropWhile (alt => best == alt || improves(best, alt)) if (!competing.isEmpty) ambiguousImplicitError(best, competing.head, "both", "and", "") for (val alt <- applicable) if (alt.sym.owner != best.sym.owner && isSubClassOrObject(alt.sym.owner, best.sym.owner)) { diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index b562fbed97..b4724266cd 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -102,10 +102,16 @@ object Predef { // --- Tupling ---------------------------------------------- type Pair[+a, +b] = Tuple2[a, b] - def Pair[a, b](x: a, y: b) = Tuple2(x, y) + object Pair { + def apply[a, b](x: a, y: b) = Tuple2(x, y) + def unapply[a, b](x: Tuple2[a, b]): Option[Tuple2[a, b]] = Some(x) + } type Triple[+a, +b, +c] = Tuple3[a, b, c] - def Triple[a, b, c](x: a, y: b, z: c) = Tuple3(x, y, z) + object Triple { + def apply[a, b, c](x: a, y: b, z: c) = Tuple3(x, y, z) + def unapply[a, b, c](x: Tuple3[a, b, c]): Option[Tuple3[a, b, c]] = Some(x) + } class ArrowAssoc[a](x: a) { def -> [b](y: b): Tuple2[a, b] = Tuple2(x, y) diff --git a/test/files/neg/scopes.check b/test/files/neg/scopes.check index 09b1bcbdb6..ca32035316 100644 --- a/test/files/neg/scopes.check +++ b/test/files/neg/scopes.check @@ -1,9 +1,6 @@ scopes.scala:1: error: x is already defined as value x case class test0(x: int, x: float) ^ -scopes.scala:2: error: x is already defined as value x - -^ scopes.scala:5: error: t is already defined as type t type t = float ^ @@ -25,4 +22,4 @@ scopes.scala:15: error: x is already defined as value x scopes.scala:17: error: x is already defined as value x case x::x => x ^ -9 errors found +8 errors found diff --git a/test/files/run/bug920.check b/test/files/run/bug920.check new file mode 100644 index 0000000000..76018072e0 --- /dev/null +++ b/test/files/run/bug920.check @@ -0,0 +1 @@ +baz diff --git a/test/files/run/bug920.scala b/test/files/run/bug920.scala new file mode 100644 index 0000000000..6a7f122d55 --- /dev/null +++ b/test/files/run/bug920.scala @@ -0,0 +1,20 @@ +object Test { + trait A; + trait Foo0 { def foo : A; } + trait Baz extends Foo0; + trait B extends A { + def initialize = { + trait Foo extends Test.Foo0 { + def foo : B.this.type = B.this; + } + class baz extends Baz with Foo { + override def toString = "baz" + } + Console.println(new baz); + } + } + object b extends B; + def main(args : Array[String]) : Unit = { + b.initialize; + } +} |