File tree Expand file tree Collapse file tree 3 files changed +39
-1
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -164,7 +164,20 @@ class VarianceChecker(using Context) {
164164 i " \n ${hl(" enum case" )} ${towner.name} requires explicit declaration of $tvar to resolve this issue. \n $example"
165165 else
166166 " "
167- em " ${varianceLabel(tvar.flags)} $tvar occurs in ${varianceLabel(required)} position in type ${sym.info} of $sym$enumAddendum"
167+ val privateParamAddendum =
168+ if sym.flags.is(ParamAccessor ) && sym.flags.is(Private ) then
169+ val varOrVal = if sym.is(Mutable ) then " var" else " val"
170+ val varFieldInstead = if sym.is(Mutable ) then " and add\n a field inside the class instead" else " "
171+ s """
172+ |
173+ |Implementation limitation: ${hl(f " private $varOrVal" )} parameters cannot be inferred to be local
174+ |and therefore are always variance-checked.
175+ |
176+ |Potential fix: remove the ${hl(f " private $varOrVal" )} modifiers on the parameter ${sym.name}$varFieldInstead.
177+ """ .stripMargin
178+ else
179+ " "
180+ em " ${varianceLabel(tvar.flags)} $tvar occurs in ${varianceLabel(required)} position in type ${sym.info} of $sym$enumAddendum$privateParamAddendum"
168181 if (migrateTo3 &&
169182 (sym.owner.isConstructor || sym.ownersIterator.exists(_.isAllOf(ProtectedLocal ))))
170183 report.migrationWarning(
Original file line number Diff line number Diff line change 1+ -- Error: tests/neg/i22620.scala:4:34 ----------------------------------------------------------------------------------
2+ 4 |class PrivateTest[-M](private val v: ArrayBuffer[M]) // error
3+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+ | contravariant type M occurs in invariant position in type scala.collection.mutable.ArrayBuffer[M] of value v
5+ |
6+ | Implementation limitation: private val parameters cannot be inferred to be local
7+ | and therefore are always variance-checked.
8+ |
9+ | Potential fix: remove the private val modifiers on the parameter v.
10+ |
11+ -- Error: tests/neg/i22620.scala:6:37 ----------------------------------------------------------------------------------
12+ 6 |class PrivateTestMut[-M](private var v: ArrayBuffer[M]) // error
13+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+ | contravariant type M occurs in invariant position in type scala.collection.mutable.ArrayBuffer[M] of variable v
15+ |
16+ | Implementation limitation: private var parameters cannot be inferred to be local
17+ | and therefore are always variance-checked.
18+ |
19+ | Potential fix: remove the private var modifiers on the parameter v and add
20+ | a field inside the class instead.
21+ |
Original file line number Diff line number Diff line change 22import scala .collection .mutable .ArrayBuffer
33
44class PrivateTest [- M ](private val v : ArrayBuffer [M ]) // error
5+
6+ class PrivateTestMut [- M ](private var v : ArrayBuffer [M ]) // error
7+
8+ class PrivateTestParamOnly [- M ](v : ArrayBuffer [M ]) // no error
You can’t perform that action at this time.
0 commit comments