Skip to content

Commit ba3f0a5

Browse files
committed
Add patch for variance errors
1 parent 42ac2e1 commit ba3f0a5

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/dotty/tools/dotc/typer/VarianceChecker.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import core._
66
import Types._, Contexts._, Flags._, Symbols._, Annotations._, Trees._, NameOps._
77
import Decorators._
88
import Variances._
9+
import util.Positions._
10+
import rewrite.Rewrites.patch
911
import config.Printers.variances
1012

1113
/** Provides `check` method to check that all top-level definitions
@@ -108,11 +110,13 @@ class VarianceChecker()(implicit ctx: Context) {
108110
}
109111

110112
private object Traverser extends TreeTraverser {
111-
def checkVariance(sym: Symbol) = Validator.validateDefinition(sym) match {
113+
def checkVariance(sym: Symbol, pos: Position) = Validator.validateDefinition(sym) match {
112114
case Some(VarianceError(tvar, required)) =>
113115
def msg = i"${varianceString(tvar.flags)} $tvar occurs in ${varianceString(required)} position in type ${sym.info} of $sym"
114-
if (ctx.scala2Mode && sym.owner.isConstructor)
115-
ctx.migrationWarning(s"According to new variance rules, this is no longer accepted; need to annotate with @uncheckedVariance:\n$msg", sym.pos)
116+
if (ctx.scala2Mode && sym.owner.isConstructor) {
117+
ctx.migrationWarning(s"According to new variance rules, this is no longer accepted; need to annotate with @uncheckedVariance:\n$msg, pos = ${sym.pos}", sym.pos)
118+
patch(ctx.compilationUnit.source, Position(pos.end), " @scala.annotation.unchecked.uncheckedVariance") // TODO use an import or shorten if possible
119+
}
116120
else ctx.error(msg, sym.pos)
117121
case None =>
118122
}
@@ -128,12 +132,11 @@ class VarianceChecker()(implicit ctx: Context) {
128132
case defn: MemberDef if skip =>
129133
ctx.debuglog(s"Skipping variance check of ${sym.showDcl}")
130134
case tree: TypeDef =>
131-
checkVariance(sym)
132-
traverseChildren(tree)
135+
checkVariance(sym, tree.envelope)
133136
case tree: ValDef =>
134-
checkVariance(sym)
137+
checkVariance(sym, tree.envelope)
135138
case DefDef(_, tparams, vparamss, _, _) =>
136-
checkVariance(sym)
139+
checkVariance(sym, tree.envelope)
137140
tparams foreach traverse
138141
vparamss foreach (_ foreach traverse)
139142
case Template(_, _, _, body) =>

tests/pos-scala2/rewrites.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ object Test {
2727

2828
}
2929

30+
class Stream[+A] {
31+
32+
class Inner(x: A) extends Stream[A]
33+
34+
}
35+

0 commit comments

Comments
 (0)