From c4e94008b972e2a5aa16fcc6ef2838b14e399d35 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 25 Nov 2013 18:14:53 +0100 Subject: Final set of Types tweaks - make namedParts return an ordered collection. --- src/dotty/tools/dotc/ast/CheckTrees.scala | 4 ++-- src/dotty/tools/dotc/core/Types.scala | 23 ++++++++++------------- src/dotty/tools/dotc/typer/Inferencing.scala | 4 ++-- 3 files changed, 14 insertions(+), 17 deletions(-) (limited to 'src/dotty/tools/dotc') diff --git a/src/dotty/tools/dotc/ast/CheckTrees.scala b/src/dotty/tools/dotc/ast/CheckTrees.scala index 4d8e25ae3..56a8b9a88 100644 --- a/src/dotty/tools/dotc/ast/CheckTrees.scala +++ b/src/dotty/tools/dotc/ast/CheckTrees.scala @@ -17,7 +17,7 @@ object CheckTrees { check(bounds contains arg.tpe) } - def escapingRefs(block: Block)(implicit ctx: Context): Set[NamedType] = { + def escapingRefs(block: Block)(implicit ctx: Context): collection.Set[NamedType] = { var hoisted: Set[Symbol] = Set() lazy val locals = localSyms(block.stats).toSet def isLocal(sym: Symbol): Boolean = @@ -29,7 +29,7 @@ object CheckTrees { !classLeaks(sym.asClass) } } - def leakingTypes(tp: Type): Set[NamedType] = + def leakingTypes(tp: Type): collection.Set[NamedType] = tp namedPartsWith (tp => isLocal(tp.symbol)) def typeLeaks(tp: Type): Boolean = leakingTypes(tp).nonEmpty def classLeaks(sym: ClassSymbol): Boolean = diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 9c0ccbe8b..437839c4f 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -186,14 +186,14 @@ object Types { !existsPart(!p(_)) /** The parts of this type which are type or term refs */ - final def namedParts(implicit ctx: Context): Set[NamedType] = + final def namedParts(implicit ctx: Context): collection.Set[NamedType] = namedPartsWith(Function.const(true)) /** The parts of this type which are type or term refs and which * satisfy predicate `p`. */ - def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): Set[NamedType] = - new NamedPartsAccumulator(p).apply(Set(), this) + def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): collection.Set[NamedType] = + new NamedPartsAccumulator(p).apply(mutable.LinkedHashSet(), this) // needed? //final def foreach(f: Type => Unit): Unit = ??? @@ -2136,10 +2136,10 @@ object Types { this(this(x, annot), underlying) case tp: TypeVar => - foldOver(x, tp.underlying) + this(x, tp.underlying) case tp: WildcardType => - foldOver(x, tp.optBounds) + this(x, tp.optBounds) case _ => x } @@ -2149,14 +2149,12 @@ object Types { def apply(x: Boolean, tp: Type) = x || p(tp) || foldOver(x, tp) } - class NamedPartsAccumulator(p: NamedType => Boolean)(implicit ctx: Context) extends TypeAccumulator[Set[NamedType]] { - def apply(x: Set[NamedType], tp: Type): Set[NamedType] = tp match { // !!! make set linked!!! + class NamedPartsAccumulator(p: NamedType => Boolean)(implicit ctx: Context) extends TypeAccumulator[mutable.Set[NamedType]] { + def apply(x: mutable.Set[NamedType], tp: Type): mutable.Set[NamedType] = tp match { case tp: NamedType if (p(tp)) => - foldOver(x + tp, tp) + foldOver(x += tp, tp) case tp: ThisType => apply(x, tp.underlying) - case tp: TypeVar => - apply(x, tp.underlying) case _ => foldOver(x, tp) } @@ -2169,7 +2167,7 @@ object Types { * following invariant: If `keep` is a name filter, and `pre` has * class `C` as a base class, then * - * keep(pre, name) => keep(C.this, name) + * keep(pre, name) implies keep(C.this, name) */ abstract class NameFilter { def apply(pre: Type, name: Name)(implicit ctx: Context): Boolean @@ -2207,8 +2205,7 @@ object Types { class MalformedType(pre: Type, denot: Denotation, absMembers: Set[Name]) extends FatalTypeError( - s"""malformed type: $pre is not a legal prefix for $denot because it contains abstract type member${if (absMembers.size == 1) "" else "s"} ${absMembers.mkString(", ")}""" - .stripMargin) + s"""malformed type: $pre is not a legal prefix for $denot because it contains abstract type member${if (absMembers.size == 1) "" else "s"} ${absMembers.mkString(", ")}""") class CyclicReference(val denot: SymDenotation) extends FatalTypeError(s"cyclic reference involving $denot") { diff --git a/src/dotty/tools/dotc/typer/Inferencing.scala b/src/dotty/tools/dotc/typer/Inferencing.scala index 599dc5f4e..070a149d4 100644 --- a/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/src/dotty/tools/dotc/typer/Inferencing.scala @@ -97,8 +97,8 @@ object Inferencing { case class ViewProto(argType: Type, override val resultType: Type)(implicit ctx: Context) extends CachedGroundType with ProtoType { def isMatchedBy(tp: Type)(implicit ctx: Context) = ctx.typer.isApplicableToTypes(tp, argType :: Nil, resultType) - override def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): Set[NamedType] = - argType.namedPartsWith(p) | resultType.namedPartsWith(p) + override def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): collection.Set[NamedType] = + AndType(argType, resultType).namedPartsWith(p) // this is more efficient than oring two namedParts sets override def computeHash = doHash(argType, resultType) } -- cgit v1.2.3