Skip to content

Commit 6d2d490

Browse files
committed
bugfix: Also save infos in semanticdb (scala#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 [Cherry-picked 00d19df]
1 parent 595660d commit 6d2d490

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
@@ -1980,6 +1980,89 @@ Occurrences:
19801980
Diagnostics:
19811981
[0:26..0:34): [warning] unused import
19821982

1983+
expect/InfoMacro.scala
1984+
----------------------
1985+
1986+
Summary:
1987+
Schema => SemanticDB v4
1988+
Uri => InfoMacro.scala
1989+
Text => empty
1990+
Language => Scala
1991+
Symbols => 7 entries
1992+
Occurrences => 23 entries
1993+
Synthetics => 6 entries
1994+
1995+
Symbols:
1996+
_empty_/InfoMacro. => final object InfoMacro extends Object { self: InfoMacro.type => +3 decls }
1997+
_empty_/InfoMacro.reportInfo(). => inline macro reportInfo (param msg: String): Unit
1998+
_empty_/InfoMacro.reportInfo().(msg) => param msg: String
1999+
_empty_/InfoMacro.reportInfoMacro(). => method reportInfoMacro (param msg: Expr[String])(implicit given param x$2: Quotes): Expr[Unit]
2000+
_empty_/InfoMacro.reportInfoMacro().(msg) => param msg: Expr[String]
2001+
_empty_/InfoMacro.reportInfoMacro().(x$2) => implicit given param x$2: Quotes
2002+
local0 => implicit given param contextual$1: Quotes
2003+
2004+
Occurrences:
2005+
[0:7..0:12): scala -> scala/
2006+
[0:13..0:19): quoted -> scala/quoted/
2007+
[2:7..2:16): InfoMacro <- _empty_/InfoMacro.
2008+
[3:13..3:23): reportInfo <- _empty_/InfoMacro.reportInfo().
2009+
[3:24..3:27): msg <- _empty_/InfoMacro.reportInfo().(msg)
2010+
[3:29..3:35): String -> scala/Predef.String#
2011+
[3:38..3:42): Unit -> scala/Unit#
2012+
[3:48..3:63): reportInfoMacro -> _empty_/InfoMacro.reportInfoMacro().
2013+
[5:6..5:21): reportInfoMacro <- _empty_/InfoMacro.reportInfoMacro().
2014+
[5:22..5:25): msg <- _empty_/InfoMacro.reportInfoMacro().(msg)
2015+
[5:27..5:31): Expr -> scala/quoted/Expr#
2016+
[5:32..5:38): String -> scala/Predef.String#
2017+
[5:47..5:53): Quotes -> scala/quoted/Quotes#
2018+
[5:56..5:60): Expr -> scala/quoted/Expr#
2019+
[5:61..5:65): Unit -> scala/Unit#
2020+
[6:11..6:17): quotes -> scala/quoted/Quotes$package.quotes().
2021+
[6:18..6:25): reflect -> scala/quoted/Quotes#reflect.
2022+
[6:26..6:32): report -> scala/quoted/Quotes#reflectModule#report.
2023+
[9:4..9:10): report -> scala/quoted/Quotes#reflectModule#report.
2024+
[9:11..9:15): info -> scala/quoted/Quotes#reflectModule#reportModule#info().
2025+
[9:16..9:17): s -> scala/StringContext#s().
2026+
[9:37..9:40): msg -> _empty_/InfoMacro.reportInfoMacro().(msg)
2027+
[9:41..9:53): valueOrAbort -> scala/quoted/Quotes#valueOrAbort().
2028+
2029+
Synthetics:
2030+
[3:48..3:69):reportInfoMacro('msg) => *(contextual$1)
2031+
[3:64..3:68):'msg => orig()(contextual$1)
2032+
[6:11..6:17):quotes => *(x$2)
2033+
[9:37..9:53):msg.valueOrAbort => *(StringFromExpr[String])
2034+
[9:41..9:53):valueOrAbort => *[String]
2035+
[11:4..11:11):'{ () } => orig(())(x$2)
2036+
2037+
expect/InfoMacroTest.scala
2038+
--------------------------
2039+
2040+
Summary:
2041+
Schema => SemanticDB v4
2042+
Uri => InfoMacroTest.scala
2043+
Text => empty
2044+
Language => Scala
2045+
Symbols => 2 entries
2046+
Occurrences => 7 entries
2047+
Diagnostics => 2 entries
2048+
2049+
Symbols:
2050+
_empty_/InfoMacroTest. => final object InfoMacroTest extends Object { self: InfoMacroTest.type => +2 decls }
2051+
_empty_/InfoMacroTest.main(). => method main (): Unit
2052+
2053+
Occurrences:
2054+
[1:7..1:20): InfoMacroTest <- _empty_/InfoMacroTest.
2055+
[2:6..2:10): main <- _empty_/InfoMacroTest.main().
2056+
[2:14..2:18): Unit -> scala/Unit#
2057+
[3:4..3:13): InfoMacro -> _empty_/InfoMacro.
2058+
[3:14..3:24): reportInfo -> _empty_/InfoMacro.reportInfo().
2059+
[4:4..4:13): InfoMacro -> _empty_/InfoMacro.
2060+
[4:14..4:24): reportInfo -> _empty_/InfoMacro.reportInfo().
2061+
2062+
Diagnostics:
2063+
[3:4..3:55): [info] Info from macro: This is a test info message
2064+
[4:4..4:48): [info] Info from macro: Another info message
2065+
19832066
expect/InstrumentTyper.scala
19842067
----------------------------
19852068

0 commit comments

Comments
 (0)