Skip to content

Commit 00d19df

Browse files
authored
bugfix: Also save infos in semanticdb (#23587)
Previously, when user changed a warning to info, we would not save it and miss information if that warning was about unused. Now, we also save infos. scalacenter/scalafix#2269
1 parent 405e2bd commit 00d19df

File tree

7 files changed

+139
-6
lines changed

7 files changed

+139
-6
lines changed

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ abstract class Reporter extends interfaces.ReporterResult {
9595

9696
private var _errorCount = 0
9797
private var _warningCount = 0
98+
private var _infoCount = 0
9899

99100
/** The number of errors reported by this reporter (ignoring outer reporters) */
100101
def errorCount: Int = _errorCount
@@ -112,12 +113,17 @@ abstract class Reporter extends interfaces.ReporterResult {
112113

113114
private var warnings: List[Warning] = Nil
114115

116+
private var infos: List[Info] = Nil
117+
115118
/** All errors reported by this reporter (ignoring outer reporters) */
116119
def allErrors: List[Error] = errors
117120

118121
/** All warnings reported by this reporter (ignoring outer reporters) */
119122
def allWarnings: List[Warning] = warnings
120123

124+
/** All infos reported by this reporter (ignoring outer reporters) */
125+
def allInfos: List[Info] = infos
126+
121127
/** Were sticky errors reported? Overridden in StoreReporter. */
122128
def hasStickyErrors: Boolean = false
123129

@@ -171,7 +177,9 @@ abstract class Reporter extends interfaces.ReporterResult {
171177
_errorCount += 1
172178
if ctx.typerState.isGlobalCommittable then
173179
ctx.base.errorsToBeReported = true
174-
case _: Info => // nothing to do here
180+
case i: Info =>
181+
infos = i :: infos
182+
_infoCount += 1
175183
// match error if d is something else
176184
}
177185
markReported(dia)

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode) extends
6363

6464
private def computeDiagnostics(
6565
sourceRoot: String,
66-
warnings: Map[SourceFile, List[Warning]],
66+
warnings: Map[SourceFile, List[dotty.tools.dotc.reporting.Diagnostic]],
6767
append: ((Path, List[Diagnostic])) => Unit)(using Context): Boolean = monitor(phaseName) {
6868
val unit = ctx.compilationUnit
6969
warnings.get(unit.source).foreach { ws =>
@@ -104,14 +104,14 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode) extends
104104
val appendDiagnostics = phaseMode == ExtractSemanticDB.PhaseMode.AppendDiagnostics
105105
val unitContexts = units.map(ctx.fresh.setCompilationUnit(_).withRootImports)
106106
if (appendDiagnostics)
107-
val warnings = ctx.reporter.allWarnings.groupBy(w => w.pos.source)
107+
val warningsAndInfos = (ctx.reporter.allWarnings ++ ctx.reporter.allInfos).groupBy(w => w.pos.source)
108108
val buf = mutable.ListBuffer.empty[(Path, Seq[Diagnostic])]
109109
val units0 =
110-
for unitCtx <- unitContexts if computeDiagnostics(sourceRoot, warnings, buf += _)(using unitCtx)
110+
for unitCtx <- unitContexts if computeDiagnostics(sourceRoot, warningsAndInfos, buf += _)(using unitCtx)
111111
yield unitCtx.compilationUnit
112112
cancellable {
113-
buf.toList.asJava.parallelStream().forEach { case (out, warnings) =>
114-
ExtractSemanticDB.appendDiagnostics(warnings, out)
113+
buf.toList.asJava.parallelStream().forEach { case (out, diagnostics) =>
114+
ExtractSemanticDB.appendDiagnostics(diagnostics, out)
115115
}
116116
}
117117
units0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
object InfoMacro/*<-_empty_::InfoMacro.*/ {
4+
inline def reportInfo/*<-_empty_::InfoMacro.reportInfo().*/(msg/*<-_empty_::InfoMacro.reportInfo().(msg)*/: String/*->scala::Predef.String#*/): Unit/*->scala::Unit#*/ = ${ reportInfoMacro/*->_empty_::InfoMacro.reportInfoMacro().*/('msg) }
5+
6+
def reportInfoMacro/*<-_empty_::InfoMacro.reportInfoMacro().*/(msg/*<-_empty_::InfoMacro.reportInfoMacro().(msg)*/: Expr/*->scala::quoted::Expr#*/[String/*->scala::Predef.String#*/])(using Quotes/*->scala::quoted::Quotes#*/): Expr/*->scala::quoted::Expr#*/[Unit/*->scala::Unit#*/] = {
7+
import quotes/*->scala::quoted::Quotes$package.quotes().*/.reflect/*->scala::quoted::Quotes#reflect.*/.report/*->scala::quoted::Quotes#reflectModule#report.*/
8+
9+
// Report an info diagnostic
10+
report/*->scala::quoted::Quotes#reflectModule#report.*/.info/*->scala::quoted::Quotes#reflectModule#reportModule#info().*/(s/*->scala::StringContext#s().*/"Info from macro: ${msg/*->_empty_::InfoMacro.reportInfoMacro().(msg)*/.valueOrAbort/*->scala::quoted::Quotes#valueOrAbort().*/}")
11+
12+
'{ () }
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
object InfoMacro {
4+
inline def reportInfo(msg: String): Unit = ${ reportInfoMacro('msg) }
5+
6+
def reportInfoMacro(msg: Expr[String])(using Quotes): Expr[Unit] = {
7+
import quotes.reflect.report
8+
9+
// Report an info diagnostic
10+
report.info(s"Info from macro: ${msg.valueOrAbort}")
11+
12+
'{ () }
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
object InfoMacroTest/*<-_empty_::InfoMacroTest.*/ {
3+
def main/*<-_empty_::InfoMacroTest.main().*/(): Unit/*->scala::Unit#*/ = {
4+
InfoMacro/*->_empty_::InfoMacro.*/.reportInfo/*->_empty_::InfoMacro.reportInfo().*/("This is a test info message")
5+
InfoMacro/*->_empty_::InfoMacro.*/.reportInfo/*->_empty_::InfoMacro.reportInfo().*/("Another info message")
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
object InfoMacroTest {
3+
def main(): Unit = {
4+
InfoMacro.reportInfo("This is a test info message")
5+
InfoMacro.reportInfo("Another info message")
6+
}
7+
}

tests/semanticdb/metac.expect

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,89 @@ Occurrences:
20072007
Diagnostics:
20082008
[0:26..0:34): [warning] unused import
20092009

2010+
expect/InfoMacro.scala
2011+
----------------------
2012+
2013+
Summary:
2014+
Schema => SemanticDB v4
2015+
Uri => InfoMacro.scala
2016+
Text => empty
2017+
Language => Scala
2018+
Symbols => 7 entries
2019+
Occurrences => 23 entries
2020+
Synthetics => 6 entries
2021+
2022+
Symbols:
2023+
_empty_/InfoMacro. => final object InfoMacro extends Object { self: InfoMacro.type => +3 decls }
2024+
_empty_/InfoMacro.reportInfo(). => inline macro reportInfo (param msg: String): Unit
2025+
_empty_/InfoMacro.reportInfo().(msg) => param msg: String
2026+
_empty_/InfoMacro.reportInfoMacro(). => method reportInfoMacro (param msg: Expr[String])(implicit given param x$2: Quotes): Expr[Unit]
2027+
_empty_/InfoMacro.reportInfoMacro().(msg) => param msg: Expr[String]
2028+
_empty_/InfoMacro.reportInfoMacro().(x$2) => implicit given param x$2: Quotes
2029+
local0 => implicit given param contextual$1: Quotes
2030+
2031+
Occurrences:
2032+
[0:7..0:12): scala -> scala/
2033+
[0:13..0:19): quoted -> scala/quoted/
2034+
[2:7..2:16): InfoMacro <- _empty_/InfoMacro.
2035+
[3:13..3:23): reportInfo <- _empty_/InfoMacro.reportInfo().
2036+
[3:24..3:27): msg <- _empty_/InfoMacro.reportInfo().(msg)
2037+
[3:29..3:35): String -> scala/Predef.String#
2038+
[3:38..3:42): Unit -> scala/Unit#
2039+
[3:48..3:63): reportInfoMacro -> _empty_/InfoMacro.reportInfoMacro().
2040+
[5:6..5:21): reportInfoMacro <- _empty_/InfoMacro.reportInfoMacro().
2041+
[5:22..5:25): msg <- _empty_/InfoMacro.reportInfoMacro().(msg)
2042+
[5:27..5:31): Expr -> scala/quoted/Expr#
2043+
[5:32..5:38): String -> scala/Predef.String#
2044+
[5:47..5:53): Quotes -> scala/quoted/Quotes#
2045+
[5:56..5:60): Expr -> scala/quoted/Expr#
2046+
[5:61..5:65): Unit -> scala/Unit#
2047+
[6:11..6:17): quotes -> scala/quoted/Quotes$package.quotes().
2048+
[6:18..6:25): reflect -> scala/quoted/Quotes#reflect.
2049+
[6:26..6:32): report -> scala/quoted/Quotes#reflectModule#report.
2050+
[9:4..9:10): report -> scala/quoted/Quotes#reflectModule#report.
2051+
[9:11..9:15): info -> scala/quoted/Quotes#reflectModule#reportModule#info().
2052+
[9:16..9:17): s -> scala/StringContext#s().
2053+
[9:37..9:40): msg -> _empty_/InfoMacro.reportInfoMacro().(msg)
2054+
[9:41..9:53): valueOrAbort -> scala/quoted/Quotes#valueOrAbort().
2055+
2056+
Synthetics:
2057+
[3:48..3:69):reportInfoMacro('msg) => *(contextual$1)
2058+
[3:64..3:68):'msg => orig()(contextual$1)
2059+
[6:11..6:17):quotes => *(x$2)
2060+
[9:37..9:53):msg.valueOrAbort => *(StringFromExpr[String])
2061+
[9:41..9:53):valueOrAbort => *[String]
2062+
[11:4..11:11):'{ () } => orig(())(x$2)
2063+
2064+
expect/InfoMacroTest.scala
2065+
--------------------------
2066+
2067+
Summary:
2068+
Schema => SemanticDB v4
2069+
Uri => InfoMacroTest.scala
2070+
Text => empty
2071+
Language => Scala
2072+
Symbols => 2 entries
2073+
Occurrences => 7 entries
2074+
Diagnostics => 2 entries
2075+
2076+
Symbols:
2077+
_empty_/InfoMacroTest. => final object InfoMacroTest extends Object { self: InfoMacroTest.type => +2 decls }
2078+
_empty_/InfoMacroTest.main(). => method main (): Unit
2079+
2080+
Occurrences:
2081+
[1:7..1:20): InfoMacroTest <- _empty_/InfoMacroTest.
2082+
[2:6..2:10): main <- _empty_/InfoMacroTest.main().
2083+
[2:14..2:18): Unit -> scala/Unit#
2084+
[3:4..3:13): InfoMacro -> _empty_/InfoMacro.
2085+
[3:14..3:24): reportInfo -> _empty_/InfoMacro.reportInfo().
2086+
[4:4..4:13): InfoMacro -> _empty_/InfoMacro.
2087+
[4:14..4:24): reportInfo -> _empty_/InfoMacro.reportInfo().
2088+
2089+
Diagnostics:
2090+
[3:4..3:55): [info] Info from macro: This is a test info message
2091+
[4:4..4:48): [info] Info from macro: Another info message
2092+
20102093
expect/InstrumentTyper.scala
20112094
----------------------------
20122095

0 commit comments

Comments
 (0)