|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import dotty.tools.dotc.ast.{ Trees, tpd } |
| 5 | +import core._ |
| 6 | +import Types._, Contexts._, Flags._, Symbols._, Annotations._, Trees._ |
| 7 | +import Decorators._ |
| 8 | +import Variances._ |
| 9 | + |
| 10 | +object VarianceChecker { |
| 11 | + |
| 12 | + case class VarianceError(tvar: Symbol, required: Variance) |
| 13 | +} |
| 14 | + |
| 15 | +/** See comments at scala.reflect.internal.Variance. |
| 16 | + */ |
| 17 | +class VarianceChecker(implicit ctx: Context) { |
| 18 | + import VarianceChecker._ |
| 19 | + import tpd._ |
| 20 | + |
| 21 | + private object Validator extends TypeAccumulator[Option[VarianceError]] { |
| 22 | + private var base: Symbol = _ |
| 23 | + |
| 24 | + /** The variance of a symbol occurrence of `tvar` seen at the level of the definition of `base`. |
| 25 | + * The search proceeds from `base` to the owner of `tvar`. |
| 26 | + * Initially the state is covariant, but it might change along the search. |
| 27 | + */ |
| 28 | + def relativeVariance(tvar: Symbol, base: Symbol, v: Variance = Covariant): Variance = { |
| 29 | + if (base.owner == tvar.owner) v |
| 30 | + else if ((base is Param) && base.owner.isTerm) relativeVariance(tvar, base.owner.owner, flip(v)) |
| 31 | + else if (base.isTerm) Bivariant |
| 32 | + else if (base.isAliasType) relativeVariance(tvar, base.owner, Invariant) |
| 33 | + else relativeVariance(tvar, base.owner, v) |
| 34 | + } |
| 35 | + |
| 36 | + def isUncheckedVariance(tp: Type): Boolean = tp match { |
| 37 | + case AnnotatedType(annot, tp1) => |
| 38 | + annot.symbol == defn.UncheckedVarianceAnnot || isUncheckedVariance(tp1) |
| 39 | + case _ => false |
| 40 | + } |
| 41 | + |
| 42 | + private def checkVarianceOfSymbol(tvar: Symbol): Option[VarianceError] = { |
| 43 | + val relative = relativeVariance(tvar, base) |
| 44 | + val required = Variances.compose(relative, this.variance) |
| 45 | + if (relative == Bivariant) None |
| 46 | + else { |
| 47 | + def tvar_s = s"$tvar (${tvar.variance}${tvar.showLocated})" |
| 48 | + def base_s = s"$base in ${base.owner}" + (if (base.owner.isClass) "" else " in " + base.owner.enclosingClass) |
| 49 | + ctx.log(s"verifying $tvar_s is $required at $base_s") |
| 50 | + if (tvar.variance == required) None |
| 51 | + else Some(VarianceError(tvar, required)) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** For PolyTypes, type parameters are skipped because they are defined |
| 56 | + * explicitly (their TypeDefs will be passed here.) For MethodTypes, the |
| 57 | + * same is true of the parameters (ValDefs) unless we are inside a |
| 58 | + * refinement, in which case they are checked from here. |
| 59 | + */ |
| 60 | + def apply(status: Option[VarianceError], tp: Type): Option[VarianceError] = |
| 61 | + if (status.isDefined) status |
| 62 | + else tp match { |
| 63 | + case tp: TypeRef => |
| 64 | + val sym = tp.symbol |
| 65 | + if (sym.variance != 0 && base.isContainedIn(sym.owner)) checkVarianceOfSymbol(sym) |
| 66 | + else if (sym.isAliasType) this(status, sym.info) |
| 67 | + else foldOver(status, tp) |
| 68 | + case tp: MethodType => |
| 69 | + this(status, tp.resultType) // params will be checked in their TypeDef nodes. |
| 70 | + case tp: PolyType => |
| 71 | + this(status, tp.resultType) // params will be checked in their ValDef nodes. |
| 72 | + case AnnotatedType(annot, _) if annot.symbol == defn.UncheckedVarianceAnnot => |
| 73 | + status |
| 74 | + case tp: ClassInfo => |
| 75 | + ??? |
| 76 | + case _ => |
| 77 | + foldOver(status, tp) |
| 78 | + } |
| 79 | + |
| 80 | + def validateDefinition(base: Symbol): Option[VarianceError] = { |
| 81 | + val saved = this.base |
| 82 | + this.base = base |
| 83 | + try apply(None, base.info) |
| 84 | + finally this.base = saved |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + def varianceString(v: Variance) = |
| 89 | + if (v is Covariant) "covariant" |
| 90 | + else if (v is Contravariant) "contravariant" |
| 91 | + else "invariant" |
| 92 | + |
| 93 | + object Traverser extends TreeTraverser { |
| 94 | + def checkVariance(sym: Symbol) = Validator.validateDefinition(sym) match { |
| 95 | + case Some(VarianceError(tvar, required)) => |
| 96 | + ctx.error( |
| 97 | + i"${varianceString(tvar.flags)} $tvar occurs in ${varianceString(required)} position in type ${sym.info} of $sym", |
| 98 | + sym.pos) |
| 99 | + case None => |
| 100 | + } |
| 101 | + |
| 102 | + override def traverse(tree: Tree) = { |
| 103 | + def sym = tree.symbol |
| 104 | + // No variance check for object-private/protected methods/values. |
| 105 | + // Or constructors, or case class factory or extractor. |
| 106 | + def skip = ( |
| 107 | + sym == NoSymbol |
| 108 | + || sym.is(Local) |
| 109 | + || sym.owner.isConstructor |
| 110 | + //|| sym.owner.isCaseApplyOrUnapply // not clear why needed |
| 111 | + ) |
| 112 | + tree match { |
| 113 | + case defn: MemberDef if skip => |
| 114 | + ctx.debuglog(s"Skipping variance check of ${sym.showDcl}") |
| 115 | + case tree: TypeDef => |
| 116 | + checkVariance(sym) |
| 117 | + super.traverse(tree) |
| 118 | + case tree: ValDef => |
| 119 | + checkVariance(sym) |
| 120 | + case DefDef(_, _, tparams, vparamss, _, _) => |
| 121 | + checkVariance(sym) |
| 122 | + tparams foreach traverse |
| 123 | + vparamss foreach (_ foreach traverse) |
| 124 | + case Template(_, _, _, body) => |
| 125 | + super.traverse(tree) |
| 126 | + case _ => |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments