Skip to content

Commit 46df5a2

Browse files
committed
Move CrossVersionChecks before FirstTransform
Fixes #17292
1 parent 6980b2e commit 46df5a2

File tree

7 files changed

+52
-34
lines changed

7 files changed

+52
-34
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class Compiler {
5959
/** Phases dealing with the transformation from pickled trees to backend trees */
6060
protected def transformPhases: List[List[Phase]] =
6161
List(new InstrumentCoverage) :: // Perform instrumentation for code coverage (if -coverage-out is set)
62+
List(new CrossVersionChecks) :: // Check issues related to deprecated and experimental
6263
List(new FirstTransform, // Some transformations to put trees into a canonical form
6364
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
6465
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
@@ -71,8 +72,7 @@ class Compiler {
7172
new ElimRepeated, // Rewrite vararg parameters and arguments
7273
new RefChecks) :: // Various checks mostly related to abstract members and overriding
7374
List(new init.Checker) :: // Check initialization of objects
74-
List(new CrossVersionChecks, // Check issues related to deprecated and experimental
75-
new ProtectedAccessors, // Add accessors for protected members
75+
List(new ProtectedAccessors, // Add accessors for protected members
7676
new ExtensionMethods, // Expand methods of value classes with extension methods
7777
new UncacheGivenAliases, // Avoid caching RHS of simple parameterless given aliases
7878
new ElimByName, // Map by-name parameters to functions

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
4242

4343
override def description: String = FirstTransform.description
4444

45+
override def runsAfterGroupsOf: Set[String] = Set(CrossVersionChecks.name)
46+
// We need to check references to deprecated or experimental definitions before
47+
// we collapse type trees into TypeTree. We check the tree to find references in
48+
// trees such as mach types where the type is not yet collapsed.
49+
4550
/** eliminate self symbol in ClassInfo */
4651
override def transformInfo(tp: Type, sym: Symbol)(using Context): Type = tp match {
4752
case tp @ ClassInfo(_, _, _, _, self: Symbol) =>

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ class CrossVersionChecks extends MiniPhase:
1818

1919
override def description: String = CrossVersionChecks.description
2020

21-
override def runsAfterGroupsOf: Set[String] = Set(FirstTransform.name)
22-
// We assume all type trees except TypeTree have been eliminated
23-
2421
// Note: if a symbol has both @deprecated and @migration annotations and both
2522
// warnings are enabled, only the first one checked here will be emitted.
2623
// I assume that's a consequence of some code trying to avoid noise by suppressing
@@ -69,18 +66,8 @@ class CrossVersionChecks extends MiniPhase:
6966
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
7067
report.deprecationWarning(em"${sym.showLocated} is deprecated${since}${msg}", pos)
7168

72-
private def checkExperimentalSignature(sym: Symbol, pos: SrcPos)(using Context): Unit =
73-
class Checker extends TypeTraverser:
74-
def traverse(tp: Type): Unit =
75-
if tp.typeSymbol.isExperimental then
76-
Feature.checkExperimentalDef(tp.typeSymbol, pos)
77-
else
78-
traverseChildren(tp)
79-
if !sym.isInExperimentalScope then
80-
new Checker().traverse(sym.info)
81-
8269
private def checkExperimentalAnnots(sym: Symbol)(using Context): Unit =
83-
if !sym.isInExperimentalScope then
70+
if sym.exists && !sym.isInExperimentalScope then
8471
for annot <- sym.annotations if annot.symbol.isExperimental do
8572
Feature.checkExperimentalDef(annot.symbol, annot.tree)
8673

@@ -119,13 +106,16 @@ class CrossVersionChecks extends MiniPhase:
119106
override def transformValDef(tree: ValDef)(using Context): ValDef =
120107
checkDeprecatedOvers(tree)
121108
checkExperimentalAnnots(tree.symbol)
122-
checkExperimentalSignature(tree.symbol, tree)
123109
tree
124110

125111
override def transformDefDef(tree: DefDef)(using Context): DefDef =
126112
checkDeprecatedOvers(tree)
127113
checkExperimentalAnnots(tree.symbol)
128-
checkExperimentalSignature(tree.symbol, tree)
114+
tree
115+
116+
override def transformTypeDef(tree: TypeDef)(using Context): TypeDef =
117+
// TODO do we need to check checkDeprecatedOvers(tree)?
118+
checkExperimentalAnnots(tree.symbol)
129119
tree
130120

131121
override def transformIdent(tree: Ident)(using Context): Ident = {
@@ -157,19 +147,14 @@ class CrossVersionChecks extends MiniPhase:
157147
tree
158148
}
159149

160-
override def transformTypeDef(tree: TypeDef)(using Context): TypeDef = {
161-
checkExperimentalAnnots(tree.symbol)
150+
override def transformOther(tree: Tree)(using Context): Tree =
151+
tree.foreachSubTree { // Find references in type trees and imports
152+
case tree: Ident => transformIdent(tree)
153+
case tree: Select => transformSelect(tree)
154+
case tree: TypeTree => transformTypeTree(tree)
155+
case _ =>
156+
}
162157
tree
163-
}
164-
165-
override def transformOther(tree: Tree)(using Context): Tree = tree match
166-
case tree: Import =>
167-
tree.foreachSubTree {
168-
case t: RefTree => checkUndesiredProperties(t.symbol, t.srcPos)
169-
case _ =>
170-
}
171-
tree
172-
case _ => tree
173158

174159
end CrossVersionChecks
175160

tests/neg-custom-args/deprecation/14034b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ type Foo0 = Exp // error
99
type Foo = Option[Exp] // error
1010
type Bar = Option[exp.type] // error
1111
type Baz = Exp | Int // error
12-
type Quux = [X] =>> X match // error
13-
case Exp => Int
12+
type Quux = [X] =>> X match
13+
case Exp => Int // error
1414
type Quuz[A <: Exp] = Int // error

tests/neg-custom-args/no-experimental/14034.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ type Foo0 = Exp // error
77
type Foo = Option[Exp] // error
88
type Bar = Option[exp.type] // error
99
type Baz = Exp | Int // error
10-
type Quux = [X] =>> X match // error
11-
case Exp => Int
10+
type Quux = [X] =>> X match
11+
case Exp => Int // error
1212
type Quuz[A <: Exp] = Int // error
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import annotation.experimental
2+
3+
class Foo { @experimental type Bar = (Int, String) }
4+
5+
val f: Foo = Foo()
6+
7+
def g: Tuple.Elem[f.Bar, 0] = ??? // error
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import annotation.experimental
2+
type A[T] = Int
3+
class Foo {
4+
@experimental type Bar = (Int, String)
5+
}
6+
7+
type Elem1[X <: Tuple, N <: Int] = X match { case x *: xs => N match { case 0 => x } }
8+
type Elem2[X <: Tuple, N <: Int]
9+
10+
val f: Foo = Foo()
11+
12+
def bar1: f.Bar = ??? // error
13+
def bar2 = // error
14+
??? : f.Bar // error
15+
16+
def g0: Elem1[f.Bar, 0] = ??? // error
17+
def g1(a: Elem1[f.Bar, 0]) = ??? // error
18+
def g2 =
19+
??? : Elem1[f.Bar, 0] // error
20+
21+
def h: Elem2[f.Bar, 0] = ??? // error

0 commit comments

Comments
 (0)