From e7fdb416006cfa4091b90935c74b5602adb5f68e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 22 Nov 2015 17:47:57 +0100 Subject: [PATCH 1/2] Swap order of arguments in annotations The fact that the annotation comes first is weird, because when I write an annotated type it's @. Also, annotated types are like RefinedTypes in that they derive from a parent type. And in RefinedTypes the parent comes first. So swapping the arguments improves consistency. --- .../backend/jvm/DottyBackendInterface.scala | 2 +- src/dotty/tools/dotc/core/TypeErasure.scala | 2 +- src/dotty/tools/dotc/core/TypeOps.scala | 2 +- src/dotty/tools/dotc/core/Types.scala | 27 +++++++++---------- .../tools/dotc/core/tasty/TreeUnpickler.scala | 3 ++- .../core/unpickleScala2/Scala2Unpickler.scala | 5 +--- .../tools/dotc/printing/PlainPrinter.scala | 2 +- .../tools/dotc/transform/PostTyper.scala | 2 +- src/dotty/tools/dotc/typer/Inferencing.scala | 2 +- src/dotty/tools/dotc/typer/TypeAssigner.scala | 2 +- src/dotty/tools/dotc/typer/Typer.scala | 2 +- .../tools/dotc/typer/VarianceChecker.scala | 2 +- src/dotty/tools/dotc/typer/Variances.scala | 4 +-- 13 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala index 1498be3546d5..f701438884dd 100644 --- a/src/dotty/tools/backend/jvm/DottyBackendInterface.scala +++ b/src/dotty/tools/backend/jvm/DottyBackendInterface.scala @@ -843,7 +843,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{ * meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning. * The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala. */ - case a @ AnnotatedType(_, t) => + case a @ AnnotatedType(t, _) => debuglog(s"typeKind of annotated type $a") t.toTypeKind(ct)(storage) diff --git a/src/dotty/tools/dotc/core/TypeErasure.scala b/src/dotty/tools/dotc/core/TypeErasure.scala index 8f116c85f26b..7cca379116a3 100644 --- a/src/dotty/tools/dotc/core/TypeErasure.scala +++ b/src/dotty/tools/dotc/core/TypeErasure.scala @@ -44,7 +44,7 @@ object TypeErasure { true case JavaArrayType(elem) => isErasedType(elem) - case AnnotatedType(_, tp) => + case AnnotatedType(tp, _) => isErasedType(tp) case ThisType(tref) => isErasedType(tref) diff --git a/src/dotty/tools/dotc/core/TypeOps.scala b/src/dotty/tools/dotc/core/TypeOps.scala index 4cfc5290975f..f3884e11a1da 100644 --- a/src/dotty/tools/dotc/core/TypeOps.scala +++ b/src/dotty/tools/dotc/core/TypeOps.scala @@ -482,7 +482,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object. normalizeToRef(tp1) case ErrorType => defn.AnyType - case AnnotatedType(_, tpe) => + case AnnotatedType(tpe, _) => normalizeToRef(tpe) case _ => throw new TypeError(s"unexpected parent type: $tp") diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index db518d95939c..2558dcbb7de4 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -176,7 +176,7 @@ object Types { /** Does the type carry an annotation that is an instance of `cls`? */ final def hasAnnotation(cls: ClassSymbol)(implicit ctx: Context): Boolean = stripTypeVar match { - case AnnotatedType(annot, tp) => (annot matches cls) || (tp hasAnnotation cls) + case AnnotatedType(tp, annot) => (annot matches cls) || (tp hasAnnotation cls) case _ => false } @@ -758,7 +758,7 @@ object Types { case tp: LazyRef => tp.ref.dealias case tp: AnnotatedType => - tp.derivedAnnotatedType(tp.annot, tp.tpe.dealias) + tp.derivedAnnotatedType(tp.tpe.dealias, tp.annot) case tp => tp } @@ -2197,7 +2197,7 @@ object Types { def fromSymbols(params: List[Symbol], resultType: Type)(implicit ctx: Context) = { def translateRepeated(tp: Type): Type = tp match { case tp @ ExprType(tp1) => tp.derivedExprType(translateRepeated(tp1)) - case AnnotatedType(annot, tp) if annot matches defn.RepeatedAnnot => + case AnnotatedType(tp, annot) if annot matches defn.RepeatedAnnot => val typeSym = tp.typeSymbol.asClass assert(typeSym == defn.SeqClass || typeSym == defn.ArrayClass) tp.translateParameterized(typeSym, defn.RepeatedParamClass) @@ -2787,23 +2787,22 @@ object Types { // ----- Annotated and Import types ----------------------------------------------- /** An annotated type tpe @ annot */ - case class AnnotatedType(annot: Annotation, tpe: Type) + case class AnnotatedType(tpe: Type, annot: Annotation) extends UncachedProxyType with ValueType { // todo: cache them? but this makes only sense if annotations and trees are also cached. override def underlying(implicit ctx: Context): Type = tpe - def derivedAnnotatedType(annot: Annotation, tpe: Type) = - if ((annot eq this.annot) && (tpe eq this.tpe)) this - else AnnotatedType(annot, tpe) + def derivedAnnotatedType(tpe: Type, annot: Annotation) = + if ((tpe eq this.tpe) && (annot eq this.annot)) this + else AnnotatedType(tpe, annot) override def stripTypeVar(implicit ctx: Context): Type = - derivedAnnotatedType(annot, tpe.stripTypeVar) + derivedAnnotatedType(tpe.stripTypeVar, annot) override def stripAnnots(implicit ctx: Context): Type = tpe.stripAnnots } object AnnotatedType { - def make(annots: List[Annotation], underlying: Type) = - if (annots.isEmpty) underlying - else (underlying /: annots)((tp, ann) => AnnotatedType(ann, tp)) + def make(underlying: Type, annots: List[Annotation]) = + (underlying /: annots)(AnnotatedType(_, _)) } // Special type objects and classes ----------------------------------------------------- @@ -2997,9 +2996,9 @@ object Types { case tp: SkolemType => tp.derivedSkolemType(this(tp.info)) - case tp @ AnnotatedType(annot, underlying) => + case tp @ AnnotatedType(underlying, annot) => val underlying1 = this(underlying) - if (underlying1 eq underlying) tp else tp.derivedAnnotatedType(mapOver(annot), underlying1) + if (underlying1 eq underlying) tp else tp.derivedAnnotatedType(underlying1, mapOver(annot)) case tp @ WildcardType => tp.derivedWildcardType(mapOver(tp.optBounds)) @@ -3139,7 +3138,7 @@ object Types { case tp: SkolemType => this(x, tp.info) - case AnnotatedType(annot, underlying) => + case AnnotatedType(underlying, annot) => this(applyToAnnot(x, annot), underlying) case tp: TypeVar => diff --git a/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index 552d931d74c4..a59d042107f7 100644 --- a/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -199,7 +199,8 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) { else 0 TypeAlias(alias, variance) case ANNOTATED => - AnnotatedType(Annotation(readTerm()), readType()) + val annot = Annotation(readTerm()) + AnnotatedType(readType(), annot) case ANDtype => AndType(readType(), readType()) case ORtype => diff --git a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index d14423b7b009..cf7b487bbf85 100644 --- a/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -800,10 +800,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas val boundSyms = until(end, readSymbolRef) elimExistentials(boundSyms, restpe) case ANNOTATEDtpe => - val tp = readTypeRef() - // no annotation self type is supported, so no test whether this is a symbol ref - val annots = until(end, readAnnotationRef) - AnnotatedType.make(annots, tp) + AnnotatedType.make(readTypeRef(), until(end, readAnnotationRef)) case _ => noSuchTypeTag(tag, end) } diff --git a/src/dotty/tools/dotc/printing/PlainPrinter.scala b/src/dotty/tools/dotc/printing/PlainPrinter.scala index 8b2d68570642..0d925a27dc7e 100644 --- a/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -158,7 +158,7 @@ class PlainPrinter(_ctx: Context) extends Printer { } case PolyParam(pt, n) => toText(polyParamName(pt.paramNames(n))) ~ polyHash(pt) - case AnnotatedType(annot, tpe) => + case AnnotatedType(tpe, annot) => toTextLocal(tpe) ~ " " ~ toText(annot) case tp: TypeVar => if (tp.isInstantiated) diff --git a/src/dotty/tools/dotc/transform/PostTyper.scala b/src/dotty/tools/dotc/transform/PostTyper.scala index 221d097f9fda..75c98ae8af96 100644 --- a/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/src/dotty/tools/dotc/transform/PostTyper.scala @@ -190,7 +190,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran case tree: TypeTree => tree.withType( tree.tpe match { - case AnnotatedType(annot, tpe) => AnnotatedType(transformAnnot(annot), tpe) + case AnnotatedType(tpe, annot) => AnnotatedType(tpe, transformAnnot(annot)) case tpe => tpe } ) diff --git a/src/dotty/tools/dotc/typer/Inferencing.scala b/src/dotty/tools/dotc/typer/Inferencing.scala index ac4ad1b35624..1e8dcf4b27a1 100644 --- a/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/src/dotty/tools/dotc/typer/Inferencing.scala @@ -179,7 +179,7 @@ object Inferencing { /** Recursively widen and also follow type declarations and type aliases. */ def widenForMatchSelector(tp: Type)(implicit ctx: Context): Type = tp.widen match { case tp: TypeRef if !tp.symbol.isClass => widenForMatchSelector(tp.info.bounds.hi) - case tp: AnnotatedType => tp.derivedAnnotatedType(tp.annot, widenForMatchSelector(tp.tpe)) + case tp: AnnotatedType => tp.derivedAnnotatedType(widenForMatchSelector(tp.tpe), tp.annot) case tp => tp } diff --git a/src/dotty/tools/dotc/typer/TypeAssigner.scala b/src/dotty/tools/dotc/typer/TypeAssigner.scala index f7cda1ef6d4f..30d6baf8a47c 100644 --- a/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -404,7 +404,7 @@ trait TypeAssigner { tree.withType(sym.nonMemberTermRef) def assignType(tree: untpd.Annotated, annot: Tree, arg: Tree)(implicit ctx: Context) = - tree.withType(AnnotatedType(Annotation(annot), arg.tpe)) + tree.withType(AnnotatedType(arg.tpe, Annotation(annot))) def assignType(tree: untpd.PackageDef, pid: Tree)(implicit ctx: Context) = tree.withType(pid.symbol.valRef) diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 0ae04cbe84da..3f5c4f47ef89 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1051,7 +1051,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit if (ctx.mode is Mode.Type) assignType(cpy.Annotated(tree)(annot1, arg1), annot1, arg1) else { - val tpt = TypeTree(AnnotatedType(Annotation(annot1), arg1.tpe.widen)) + val tpt = TypeTree(AnnotatedType(arg1.tpe.widen, Annotation(annot1))) assignType(cpy.Typed(tree)(arg1, tpt), tpt) } } diff --git a/src/dotty/tools/dotc/typer/VarianceChecker.scala b/src/dotty/tools/dotc/typer/VarianceChecker.scala index 5d3bd4f20862..86b1676c5147 100644 --- a/src/dotty/tools/dotc/typer/VarianceChecker.scala +++ b/src/dotty/tools/dotc/typer/VarianceChecker.scala @@ -90,7 +90,7 @@ class VarianceChecker()(implicit ctx: Context) { this(status, tp.resultType) // params will be checked in their TypeDef nodes. case tp: PolyType => this(status, tp.resultType) // params will be checked in their ValDef nodes. - case AnnotatedType(annot, _) if annot.symbol == defn.UncheckedVarianceAnnot => + case AnnotatedType(_, annot) if annot.symbol == defn.UncheckedVarianceAnnot => status //case tp: ClassInfo => // ??? not clear what to do here yet. presumably, it's all checked at local typedefs diff --git a/src/dotty/tools/dotc/typer/Variances.scala b/src/dotty/tools/dotc/typer/Variances.scala index 0cc9e74ccb80..55e6b5232347 100644 --- a/src/dotty/tools/dotc/typer/Variances.scala +++ b/src/dotty/tools/dotc/typer/Variances.scala @@ -83,8 +83,8 @@ object Variances { varianceInType(restpe)(tparam) case tp @ PolyType(_) => flip(varianceInTypes(tp.paramBounds)(tparam)) & varianceInType(tp.resultType)(tparam) - case AnnotatedType(annot, tp) => - varianceInAnnot(annot)(tparam) & varianceInType(tp)(tparam) + case AnnotatedType(tp, annot) => + varianceInType(tp)(tparam) & varianceInAnnot(annot)(tparam) case tp: AndOrType => varianceInType(tp.tp1)(tparam) & varianceInType(tp.tp2)(tparam) case _ => From b7aae3f3deeb219c79a808454563145bf1b15243 Mon Sep 17 00:00:00 2001 From: VladimirNik Date: Mon, 23 Nov 2015 00:13:30 +0300 Subject: [PATCH 2/2] Swap order of arguments in annotations for Tasty --- src/dotty/tools/dotc/core/tasty/TastyFormat.scala | 2 +- src/dotty/tools/dotc/core/tasty/TreePickler.scala | 2 +- src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/dotty/tools/dotc/core/tasty/TastyFormat.scala b/src/dotty/tools/dotc/core/tasty/TastyFormat.scala index 94ed9405276c..1f24397c6fe0 100644 --- a/src/dotty/tools/dotc/core/tasty/TastyFormat.scala +++ b/src/dotty/tools/dotc/core/tasty/TastyFormat.scala @@ -131,7 +131,7 @@ Standard-Section: "ASTs" TopLevelStat* APPLIEDtype Length tycon_Type arg_Type* TYPEBOUNDS Length low_Type high_Type TYPEALIAS Length alias_Type (COVARIANT | CONTRAVARIANT)? - ANNOTATED Length fullAnnotation_Term underlying_Type + ANNOTATED Length underlying_Type fullAnnotation_Term ANDtype Length left_Type right_Type ORtype Length left_Type right_Type BIND Length boundName_NameRef bounds_Type diff --git a/src/dotty/tools/dotc/core/tasty/TreePickler.scala b/src/dotty/tools/dotc/core/tasty/TreePickler.scala index 86bbc893fbd8..dae201a79926 100644 --- a/src/dotty/tools/dotc/core/tasty/TreePickler.scala +++ b/src/dotty/tools/dotc/core/tasty/TreePickler.scala @@ -239,7 +239,7 @@ class TreePickler(pickler: TastyPickler) { withLength { pickleType(tpe.lo, richTypes); pickleType(tpe.hi, richTypes) } case tpe: AnnotatedType => writeByte(ANNOTATED) - withLength { pickleTree(tpe.annot.tree); pickleType(tpe.tpe, richTypes) } + withLength { pickleType(tpe.tpe, richTypes); pickleTree(tpe.annot.tree) } case tpe: AndOrType => writeByte(if (tpe.isAnd) ANDtype else ORtype) withLength { pickleType(tpe.tp1, richTypes); pickleType(tpe.tp2, richTypes) } diff --git a/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index a59d042107f7..426a6804bf72 100644 --- a/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -199,8 +199,7 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) { else 0 TypeAlias(alias, variance) case ANNOTATED => - val annot = Annotation(readTerm()) - AnnotatedType(readType(), annot) + AnnotatedType(readType(), Annotation(readTerm())) case ANDtype => AndType(readType(), readType()) case ORtype =>