Skip to content

Commit

Permalink
Move CrossVersionChecks before FirstTransform
Browse files Browse the repository at this point in the history
Fixes #17292
  • Loading branch information
nicolasstucki committed Apr 18, 2023
1 parent 6980b2e commit 46df5a2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Compiler {
/** Phases dealing with the transformation from pickled trees to backend trees */
protected def transformPhases: List[List[Phase]] =
List(new InstrumentCoverage) :: // Perform instrumentation for code coverage (if -coverage-out is set)
List(new CrossVersionChecks) :: // Check issues related to deprecated and experimental
List(new FirstTransform, // Some transformations to put trees into a canonical form
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
new ElimPackagePrefixes, // Eliminate references to package prefixes in Select nodes
Expand All @@ -71,8 +72,7 @@ class Compiler {
new ElimRepeated, // Rewrite vararg parameters and arguments
new RefChecks) :: // Various checks mostly related to abstract members and overriding
List(new init.Checker) :: // Check initialization of objects
List(new CrossVersionChecks, // Check issues related to deprecated and experimental
new ProtectedAccessors, // Add accessors for protected members
List(new ProtectedAccessors, // Add accessors for protected members
new ExtensionMethods, // Expand methods of value classes with extension methods
new UncacheGivenAliases, // Avoid caching RHS of simple parameterless given aliases
new ElimByName, // Map by-name parameters to functions
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/FirstTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>

override def description: String = FirstTransform.description

override def runsAfterGroupsOf: Set[String] = Set(CrossVersionChecks.name)
// We need to check references to deprecated or experimental definitions before
// we collapse type trees into TypeTree. We check the tree to find references in
// trees such as mach types where the type is not yet collapsed.

/** eliminate self symbol in ClassInfo */
override def transformInfo(tp: Type, sym: Symbol)(using Context): Type = tp match {
case tp @ ClassInfo(_, _, _, _, self: Symbol) =>
Expand Down
41 changes: 13 additions & 28 deletions compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class CrossVersionChecks extends MiniPhase:

override def description: String = CrossVersionChecks.description

override def runsAfterGroupsOf: Set[String] = Set(FirstTransform.name)
// We assume all type trees except TypeTree have been eliminated

// Note: if a symbol has both @deprecated and @migration annotations and both
// warnings are enabled, only the first one checked here will be emitted.
// I assume that's a consequence of some code trying to avoid noise by suppressing
Expand Down Expand Up @@ -69,18 +66,8 @@ class CrossVersionChecks extends MiniPhase:
val since = annot.argumentConstant(1).map(" since " + _.stringValue).getOrElse("")
report.deprecationWarning(em"${sym.showLocated} is deprecated${since}${msg}", pos)

private def checkExperimentalSignature(sym: Symbol, pos: SrcPos)(using Context): Unit =
class Checker extends TypeTraverser:
def traverse(tp: Type): Unit =
if tp.typeSymbol.isExperimental then
Feature.checkExperimentalDef(tp.typeSymbol, pos)
else
traverseChildren(tp)
if !sym.isInExperimentalScope then
new Checker().traverse(sym.info)

private def checkExperimentalAnnots(sym: Symbol)(using Context): Unit =
if !sym.isInExperimentalScope then
if sym.exists && !sym.isInExperimentalScope then
for annot <- sym.annotations if annot.symbol.isExperimental do
Feature.checkExperimentalDef(annot.symbol, annot.tree)

Expand Down Expand Up @@ -119,13 +106,16 @@ class CrossVersionChecks extends MiniPhase:
override def transformValDef(tree: ValDef)(using Context): ValDef =
checkDeprecatedOvers(tree)
checkExperimentalAnnots(tree.symbol)
checkExperimentalSignature(tree.symbol, tree)
tree

override def transformDefDef(tree: DefDef)(using Context): DefDef =
checkDeprecatedOvers(tree)
checkExperimentalAnnots(tree.symbol)
checkExperimentalSignature(tree.symbol, tree)
tree

override def transformTypeDef(tree: TypeDef)(using Context): TypeDef =
// TODO do we need to check checkDeprecatedOvers(tree)?
checkExperimentalAnnots(tree.symbol)
tree

override def transformIdent(tree: Ident)(using Context): Ident = {
Expand Down Expand Up @@ -157,19 +147,14 @@ class CrossVersionChecks extends MiniPhase:
tree
}

override def transformTypeDef(tree: TypeDef)(using Context): TypeDef = {
checkExperimentalAnnots(tree.symbol)
override def transformOther(tree: Tree)(using Context): Tree =
tree.foreachSubTree { // Find references in type trees and imports
case tree: Ident => transformIdent(tree)
case tree: Select => transformSelect(tree)
case tree: TypeTree => transformTypeTree(tree)
case _ =>
}
tree
}

override def transformOther(tree: Tree)(using Context): Tree = tree match
case tree: Import =>
tree.foreachSubTree {
case t: RefTree => checkUndesiredProperties(t.symbol, t.srcPos)
case _ =>
}
tree
case _ => tree

end CrossVersionChecks

Expand Down
4 changes: 2 additions & 2 deletions tests/neg-custom-args/deprecation/14034b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type Foo0 = Exp // error
type Foo = Option[Exp] // error
type Bar = Option[exp.type] // error
type Baz = Exp | Int // error
type Quux = [X] =>> X match // error
case Exp => Int
type Quux = [X] =>> X match
case Exp => Int // error
type Quuz[A <: Exp] = Int // error
4 changes: 2 additions & 2 deletions tests/neg-custom-args/no-experimental/14034.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ type Foo0 = Exp // error
type Foo = Option[Exp] // error
type Bar = Option[exp.type] // error
type Baz = Exp | Int // error
type Quux = [X] =>> X match // error
case Exp => Int
type Quux = [X] =>> X match
case Exp => Int // error
type Quuz[A <: Exp] = Int // error
7 changes: 7 additions & 0 deletions tests/neg-custom-args/no-experimental/i17292.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import annotation.experimental

class Foo { @experimental type Bar = (Int, String) }

val f: Foo = Foo()

def g: Tuple.Elem[f.Bar, 0] = ??? // error
21 changes: 21 additions & 0 deletions tests/neg-custom-args/no-experimental/i17292b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import annotation.experimental
type A[T] = Int
class Foo {
@experimental type Bar = (Int, String)
}

type Elem1[X <: Tuple, N <: Int] = X match { case x *: xs => N match { case 0 => x } }
type Elem2[X <: Tuple, N <: Int]

val f: Foo = Foo()

def bar1: f.Bar = ??? // error
def bar2 = // error
??? : f.Bar // error

def g0: Elem1[f.Bar, 0] = ??? // error
def g1(a: Elem1[f.Bar, 0]) = ??? // error
def g2 =
??? : Elem1[f.Bar, 0] // error

def h: Elem2[f.Bar, 0] = ??? // error

0 comments on commit 46df5a2

Please sign in to comment.