Skip to content

Commit 27919fb

Browse files
committed
declare derived type params as synthetic
1 parent a126ebf commit 27919fb

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ object desugar {
137137

138138
/** A derived type definition watching `sym` */
139139
def derivedTypeParamWithVariance(sym: TypeSymbol)(using Context): TypeDef =
140-
val variance = (Covariant | Contravariant) & sym.flags
141-
TypeDef(sym.name, DerivedFromParamTree().watching(sym)).withFlags(TypeParam | variance)
140+
val variance = VarianceFlags & sym.flags
141+
TypeDef(sym.name, DerivedFromParamTree().watching(sym)).withFlags(TypeParam | Synthetic | variance)
142142

143143
/** A value definition copied from `vdef` with a tpt typetree derived from it */
144144
def derivedTermParam(vdef: ValDef)(using Context): ValDef =

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import util.SrcPos
1212
import config.Printers.variances
1313
import config.Feature.migrateTo3
1414
import reporting.trace
15+
import printing.Formatting.hl
1516

1617
/** Provides `check` method to check that all top-level definitions
1718
* in tree are variance correct. Does not recurse inside methods.
@@ -169,13 +170,10 @@ class VarianceChecker(using Context) {
169170
def msg =
170171
val enumAddendum =
171172
val towner = tvar.owner
172-
if towner.isAllOf(EnumCase) && towner.isClass
173-
&& tvar.span.exists && towner.span.exists
174-
&& tvar.span.start < towner.span.start // implies that `tvar` was not user declared
175-
then
173+
if towner.isAllOf(EnumCase) && towner.isClass && tvar.is(Synthetic) then
176174
val example =
177175
"See an example at http://dotty.epfl.ch/docs/reference/enums/adts.html#parameter-variance-of-enums"
178-
i"\nenum case ${towner} requires explicit declaration of $tvar to resolve this issue.\n$example"
176+
i"\n${hl("enum case")} ${towner.name} requires explicit declaration of $tvar to resolve this issue.\n$example"
179177
else
180178
""
181179
i"${varianceLabel(tvar.flags)} $tvar occurs in ${varianceLabel(required)} position in type ${sym.info} of $sym$enumAddendum"

tests/neg-custom-args/fatal-warnings/enum-variance.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
2 | case Refl(f: T => T) // error: enum case class Refl requires explicit declaration of type T
33
| ^^^^^^^^^
44
| contravariant type T occurs in covariant position in type T => T of value f
5-
| enum case class Refl requires explicit declaration of type T to resolve this issue.
5+
| enum case Refl requires explicit declaration of type T to resolve this issue.
66
| See an example at http://dotty.epfl.ch/docs/reference/enums/adts.html#parameter-variance-of-enums
77
-- Error: tests/neg-custom-args/fatal-warnings/enum-variance.scala:5:16 ------------------------------------------------
88
5 | case Refl[-T](f: T => T) extends ExplicitView[T] // error: contravariant type T occurs in covariant position

0 commit comments

Comments
 (0)