Skip to content

Commit

Permalink
Run AppendDiagnostics phase after crossVersionChecks
Browse files Browse the repository at this point in the history
So that we can add deprecation warnings to SemanticDB
  • Loading branch information
tanishiking committed Jul 19, 2023
1 parent d2b54c2 commit 56c5909
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 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 @@ -38,7 +38,7 @@ class Compiler {
List(new CheckUnused.PostTyper) :: // Check for unused elements
List(new YCheckPositions) :: // YCheck positions
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
List(new semanticdb.ExtractSemanticDB.PostTyper) :: // Extract info and attach to the tree of the unit file
List(new semanticdb.ExtractSemanticDB.ExtractSemanticInfo) :: // Extract info and attach to the tree of the unit file
List(new PostTyper) :: // Additional checks and cleanups after type checking
List(new sjs.PrepJSInterop) :: // Additional checks and transformations for Scala.js (Scala.js only)
List(new sbt.ExtractAPI) :: // Sends a representation of the API of classes to sbt via callbacks
Expand All @@ -51,7 +51,6 @@ class Compiler {
List(new Inlining) :: // Inline and execute macros
List(new PostInlining) :: // Add mirror support for inlined code
List(new CheckUnused.PostInlining) :: // Check for unused elements
List(new semanticdb.ExtractSemanticDB.PostInlining) :: // Attach warnings to extracted SemanticDB and write to .semanticdb file
List(new Staging) :: // Check staging levels and heal staged types
List(new Splicing) :: // Replace level 1 splices with holes
List(new PickleQuotes) :: // Turn quoted trees into explicit run-time data structures
Expand All @@ -72,6 +71,7 @@ class Compiler {
new ExpandSAMs, // Expand single abstract method closures to anonymous classes
new ElimRepeated, // Rewrite vararg parameters and arguments
new RefChecks) :: // Various checks mostly related to abstract members and overriding
List(new semanticdb.ExtractSemanticDB.AppendDiagnostics) :: // Attach warnings to extracted SemanticDB and write to .semanticdb file
List(new init.Checker) :: // Check initialization of objects
List(new ProtectedAccessors, // Add accessors for protected members
new ExtensionMethods, // Expand methods of value classes with extension methods
Expand Down
20 changes: 10 additions & 10 deletions compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode, suffix:
override def isCheckable: Boolean = false

override def runOn(units: List[CompilationUnit])(using ctx: Context): List[CompilationUnit] = {
val appendWarnings = phaseMode == ExtractSemanticDB.PhaseMode.PostInlining
val appendDiagnostics = phaseMode == ExtractSemanticDB.PhaseMode.AppendDiagnostics
val warnings =
if (appendWarnings)
if (appendDiagnostics)
ctx.reporter.allWarnings.groupBy(w => w.pos.source)
else Map.empty

units.asJava.parallelStream().map { unit =>
units.asJava.parallelStream().forEach { unit =>
val unitCtx = ctx.fresh.setCompilationUnit(unit).withRootImports
if (appendWarnings)
if (appendDiagnostics)
warnings.get(unit.source).foreach { ws =>
ExtractSemanticDB.appendDiagnostics(unit.source, ws.map(_.toSemanticDiagnostic))
}
else
val extractor = ExtractSemanticDB.Extractor()
extractor.extract(unit.tpdTree)
ExtractSemanticDB.write(unit.source, extractor.occurrences.toList, extractor.symbolInfos.toList, extractor.synthetics.toList)
unit
}.toList().asScala.toList
}
units
}

def run(using Context): Unit = unsupported("run")
Expand All @@ -94,12 +94,12 @@ object ExtractSemanticDB:
val description: String = "extract info into .semanticdb files"

enum PhaseMode:
case PostTyper
case PostInlining
case ExtractSemanticInfo
case AppendDiagnostics

class PostTyper extends ExtractSemanticDB(PhaseMode.PostTyper, "PostTyper")
class ExtractSemanticInfo extends ExtractSemanticDB(PhaseMode.ExtractSemanticInfo, "ExtractSemanticInfo")

class PostInlining extends ExtractSemanticDB(PhaseMode.PostInlining, "PostInlining")
class AppendDiagnostics extends ExtractSemanticDB(PhaseMode.AppendDiagnostics, "AppendDiagnostics")

private def semanticdbTarget(using Context): Option[Path] =
Option(ctx.settings.semanticdbTarget.value)
Expand Down
4 changes: 4 additions & 0 deletions tests/semanticdb/expect/Deprecated.expect.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Deprecated/*<-_empty_::Deprecated.*/ {
@deprecated/*->scala::deprecated#*/ def deprecatedMethod/*<-_empty_::Deprecated.deprecatedMethod().*/ = ???/*->scala::Predef.`???`().*/
def main/*<-_empty_::Deprecated.main().*/ = deprecatedMethod/*->_empty_::Deprecated.deprecatedMethod().*/
}
4 changes: 4 additions & 0 deletions tests/semanticdb/expect/Deprecated.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Deprecated {
@deprecated def deprecatedMethod = ???
def main = deprecatedMethod
}
28 changes: 28 additions & 0 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,34 @@ Synthetics:
[51:16..51:27):List(1).map => *[Int]
[51:16..51:20):List => *.apply[Int]

expect/Deprecated.scala
-----------------------

Summary:
Schema => SemanticDB v4
Uri => Deprecated.scala
Text => empty
Language => Scala
Symbols => 3 entries
Occurrences => 6 entries
Diagnostics => 1 entries

Symbols:
_empty_/Deprecated. => final object Deprecated extends Object { self: Deprecated.type => +3 decls }
_empty_/Deprecated.deprecatedMethod(). => @deprecated method deprecatedMethod => Nothing
_empty_/Deprecated.main(). => method main => Nothing

Occurrences:
[0:7..0:17): Deprecated <- _empty_/Deprecated.
[1:3..1:13): deprecated -> scala/deprecated#
[1:18..1:34): deprecatedMethod <- _empty_/Deprecated.deprecatedMethod().
[1:37..1:40): ??? -> scala/Predef.`???`().
[2:6..2:10): main <- _empty_/Deprecated.main().
[2:13..2:29): deprecatedMethod -> _empty_/Deprecated.deprecatedMethod().

Diagnostics:
[2:13..2:29): [warning] method deprecatedMethod in object Deprecated is deprecated

expect/Empty.scala
------------------

Expand Down

0 comments on commit 56c5909

Please sign in to comment.