Skip to content

Commit 31228c0

Browse files
committed
Do Variance checking at PostTyper
The plan is that by then we know when a members is private[this].
1 parent c8c7520 commit 31228c0

File tree

11 files changed

+26
-14
lines changed

11 files changed

+26
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ object desugar {
150150
* ==>
151151
* def x: Int = expr
152152
* def x_=($1: <TypeTree()>): Unit = ()
153+
*
154+
* Generate the setter only for non-private class members and all trait members.
153155
*/
154156
def valDef(vdef0: ValDef)(implicit ctx: Context): Tree = {
155157
val vdef @ ValDef(name, tpt, rhs) = transformQuotedPatternName(vdef0)

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,12 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
243243
case tree: TypeDef =>
244244
val sym = tree.symbol
245245
if (sym.isClass)
246+
VarianceChecker.check(tree)
246247
// Add SourceFile annotation to top-level classes
247-
if (sym.owner.is(Package) &&
248-
ctx.compilationUnit.source.exists &&
249-
sym != defn.SourceFileAnnot)
248+
if sym.owner.is(Package)
249+
&& ctx.compilationUnit.source.exists
250+
&& sym != defn.SourceFileAnnot
251+
then
250252
sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path))
251253
processMemberDef(super.transform(tree))
252254
case tree: New if isCheckable(tree) =>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ class ReTyper extends Typer with ReChecking {
130130

131131
override def inlineExpansion(mdef: DefDef)(implicit ctx: Context): Tree = mdef
132132

133-
override def checkVariance(tree: Tree)(implicit ctx: Context): Unit = ()
134133
override def inferView(from: Tree, to: Type)(implicit ctx: Context): Implicits.SearchResult =
135134
Implicits.NoMatchingImplicitsFailure
136135
override def checkCanEqual(ltp: Type, rtp: Type, span: Span)(implicit ctx: Context): Unit = ()

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,6 @@ class Typer extends Namer
17401740
checkEnum(cdef, cls, firstParent)
17411741
}
17421742
val cdef1 = assignType(cpy.TypeDef(cdef)(name, impl1), cls)
1743-
checkVariance(cdef1)
17441743

17451744
val reportDynamicInheritance =
17461745
ctx.phase.isTyper &&
@@ -1828,9 +1827,6 @@ class Typer extends Namer
18281827
else parents
18291828
}
18301829

1831-
/** Overridden in retyper */
1832-
def checkVariance(tree: Tree)(implicit ctx: Context): Unit = VarianceChecker.check(tree)
1833-
18341830
def localDummy(cls: ClassSymbol, impl: untpd.Template)(implicit ctx: Context): Symbol =
18351831
ctx.newLocalDummy(cls, impl.span)
18361832

tests/neg-custom-args/matchtype-loop.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object Test {
22
type L[X] = X match {
33
case Int => L[X]
44
}
5-
type LL[X] = X match { // error: recursion limit exceeded
5+
type LL[X] = X match {
66
case Int => LL[LL[X]]
77
}
88
def a: L[Boolean] = ???
@@ -11,7 +11,7 @@ object Test {
1111
val x: Int = g[Int] // error: found: L[Int], required: Int
1212

1313
def aa: LL[Boolean] = ???
14-
def bb: LL[Int] = ??? // error: recursion limit exceeded with reduce type LazyRef(Test.LL[Int]) match ... // error
14+
def bb: LL[Int] = ??? // error: recursion limit exceeded with reduce type LazyRef(Test.LL[Int]) match ...
1515
def gg[X]: LL[X] = ???
1616
val xx: Int = gg[Int] // error: recursion limit exceeded with reduce type LazyRef(Test.LL[Int]) match ...
1717
}

tests/neg/i4385.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class i0 {
22
class i1 { type i2 }
3-
type i3 = i1.i2 // error
3+
val i1 = new i1()
4+
type i3 = i1.i2
45
type i4 = i0 { type i1 <: i4 } // error: recursion limit exceeded
56
// This test ensures the above stack overflow is reported and not hidden by the earlier error.
67
}

tests/neg/i5202.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Foo[A] {
99
def foo(a: A): Unit = {}
1010
}
1111
class Co[+A] {
12-
def foo(a: A): Unit = {} // error: contravariant occurs in covariant position
12+
def foo(a: A): Unit = {}
1313
def bar: A = ???
1414
}
1515
class Contra[-A] {

tests/neg/i5202a.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Co[+A] {
2+
def foo(a: A): Unit = {} // error: contravariant occurs in covariant position
3+
def bar: A = ???
4+
}

tests/neg/matchtype-loop2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test {
2+
type L[X] = X match {
3+
case Int => L[X]
4+
}
5+
type LL[X] = X match { // error: recursion limit exceeded
6+
case Int => LL[LL[X]]
7+
}
8+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
type A = B // error: recursion limit exceeded // error: recursion limit exceeded
1+
type A = B // error: recursion limit exceeded
22

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
type B = A // error: recursion limit exceeded // error: recursion limit exceeded
1+
type B = A // error: recursion limit exceeded

0 commit comments

Comments
 (0)