From dba1db21103b38c001699b0fd1d8cfe4aef3de74 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Sun, 2 Oct 2022 17:21:50 -0400 Subject: [PATCH] Util 1.7.2 --- .../sbt/internal/inc/FilteredReporter.scala | 16 ++++---- .../sbt/internal/inc/LoggedReporter.scala | 12 +++--- .../inc/javac/DiagnosticsReporter.scala | 10 ++++- .../scala/sbt/internal/inc/Incremental.scala | 10 ++++- .../src/main/protobuf/schema.proto | 12 ++++++ .../sbt/internal/inc/DiagnosticsUtil.scala | 41 +++++++++++++++++++ .../binary/converters/ProtobufReaders.scala | 39 ++++++++++++++++-- .../binary/converters/ProtobufWriters.scala | 35 ++++++++++++++-- .../inc/text/TextAnalysisFormat.scala | 27 ++++++++++-- project/Dependencies.scala | 2 +- 10 files changed, 179 insertions(+), 25 deletions(-) create mode 100644 internal/zinc-persist/src/main/scala/sbt/internal/inc/DiagnosticsUtil.scala diff --git a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/FilteredReporter.scala b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/FilteredReporter.scala index c8efb99933..292cd47cd4 100644 --- a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/FilteredReporter.scala +++ b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/FilteredReporter.scala @@ -90,14 +90,16 @@ class FilteredReporter( // Even if we don't display, we do want to register the problem import sbt.util.InterfaceUtil val transformedPos: Position = positionMapper(position) - val problem = InterfaceUtil.problem( - category, - transformedPos, - message, - severity, - InterfaceUtil.jo2o(rendered) + val prob = InterfaceUtil.problem( + cat = category, + pos = transformedPos, + msg = message, + sev = severity, + rendered = InterfaceUtil.jo2o(rendered), + diagnosticCode = InterfaceUtil.jo2o(problem.diagnosticCode()), + diagnosticRelatedInforamation = InterfaceUtil.jl2l(problem.diagnosticRelatedInforamation()), ) - allProblems += problem + allProblems += prob () } } diff --git a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/LoggedReporter.scala b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/LoggedReporter.scala index db9e148bee..b3847fa7a2 100644 --- a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/LoggedReporter.scala +++ b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/LoggedReporter.scala @@ -131,14 +131,16 @@ class LoggedReporter( val transformedPos: Position = sourcePositionMapper(position) val transformed = transformedPos.sourceFile != position.sourceFile val problem = InterfaceUtil.problem( - category, - transformedPos, - message, - severity, + cat = category, + pos = transformedPos, + msg = message, + sev = severity, // When the source mapping is performed, // the information based on the `transformedPos` should be displayed // even if the `rendered` is defined. - if (transformed) None else InterfaceUtil.jo2o(rendered) + rendered = if (transformed) None else InterfaceUtil.jo2o(rendered), + diagnosticCode = InterfaceUtil.jo2o(problem0.diagnosticCode()), + diagnosticRelatedInforamation = InterfaceUtil.jl2l(problem0.diagnosticRelatedInforamation()), ) allProblems += problem0 severity match { diff --git a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/DiagnosticsReporter.scala b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/DiagnosticsReporter.scala index e2d25d7da6..8f2180b24c 100644 --- a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/DiagnosticsReporter.scala +++ b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/DiagnosticsReporter.scala @@ -49,7 +49,15 @@ final class DiagnosticsReporter(reporter: Reporter) extends DiagnosticListener[J val msg = d.getMessage(null) val pos: xsbti.Position = PositionImpl(d) if (severity == Severity.Error) errorEncountered = true - reporter.log(problem("", pos, msg, severity, None)) + reporter.log(problem( + cat = "", + pos = pos, + msg = msg, + sev = severity, + rendered = None, + diagnosticCode = None, + diagnosticRelatedInforamation = Nil, + )) } } diff --git a/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala b/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala index cdd13be152..cee4f3ca28 100644 --- a/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala +++ b/internal/zinc-core/src/main/scala/sbt/internal/inc/Incremental.scala @@ -694,7 +694,15 @@ private final class AnalysisCallback( val map = if (reported) reporteds else unreporteds map .getOrElseUpdate(source, new ConcurrentLinkedQueue) - .add(InterfaceUtil.problem(category, pos, msg, severity, None)) + .add(InterfaceUtil.problem( + cat = category, + pos = pos, + msg = msg, + sev = severity, + rendered = None, + diagnosticCode = None, + diagnosticRelatedInforamation = Nil, + )) } } diff --git a/internal/zinc-persist-core/src/main/protobuf/schema.proto b/internal/zinc-persist-core/src/main/protobuf/schema.proto index 7956ad8a19..8294bb3f40 100644 --- a/internal/zinc-persist-core/src/main/protobuf/schema.proto +++ b/internal/zinc-persist-core/src/main/protobuf/schema.proto @@ -79,12 +79,24 @@ message Position { sint32 endColumn = 13; } +message DiagnosticCode { + string code = 1; + string explanation = 2; +} + +message DiagnosticRelatedInformation { + Position position = 1; + string message = 2; +} + message Problem { string category = 1; Severity severity = 2; string message = 3; Position position = 4; string rendered = 5; + DiagnosticCode diagnosticCode = 6; + repeated DiagnosticRelatedInformation diagnosticRelatedInforamation = 7; } message SourceInfo { diff --git a/internal/zinc-persist/src/main/scala/sbt/internal/inc/DiagnosticsUtil.scala b/internal/zinc-persist/src/main/scala/sbt/internal/inc/DiagnosticsUtil.scala new file mode 100644 index 0000000000..21a849e38a --- /dev/null +++ b/internal/zinc-persist/src/main/scala/sbt/internal/inc/DiagnosticsUtil.scala @@ -0,0 +1,41 @@ +/* + * Zinc - The incremental compiler for Scala. + * Copyright Lightbend, Inc. and Mark Harrah + * + * Licensed under Apache License 2.0 + * (http://www.apache.org/licenses/LICENSE-2.0). + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + +package sbt +package internal +package inc + +import java.util.Optional +import sbt.util.InterfaceUtil +import xsbti.{ DiagnosticCode, DiagnosticRelatedInformation, Position } + +object DiagnosticsUtil { + def diagnosticCode(code: String, explanation: Option[String]): DiagnosticCode = + new ConcreteDiagnosticCode(code, explanation) + + def diagnosticRelatedInformation( + position: Position, + message: String + ): DiagnosticRelatedInformation = + new ConcreteDiagnosticRelatedInformation(position, message) + + private class ConcreteDiagnosticCode(code0: String, explanation0: Option[String]) + extends DiagnosticCode { + override val code: String = code0 + override val explanation: Optional[String] = InterfaceUtil.o2jo(explanation0) + } + + private class ConcreteDiagnosticRelatedInformation(position0: Position, message0: String) + extends DiagnosticRelatedInformation { + override val position: Position = position0 + override val message: String = message0 + } +} diff --git a/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufReaders.scala b/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufReaders.scala index c649fc63c2..ec94b825f8 100644 --- a/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufReaders.scala +++ b/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufReaders.scala @@ -12,12 +12,21 @@ package sbt.internal.inc.binary.converters import java.nio.file.{ Path, Paths } -import java.util.{ List => JList, Map => JMap, HashMap => JHashMap } +import java.util.{ List => JList, Map => JMap, HashMap => JHashMap, Optional } import sbt.internal.inc.Relations.ClassDependencies import sbt.internal.inc._ import sbt.internal.inc.binary.converters.ProtobufDefaults.EmptyLazyCompanions import sbt.util.InterfaceUtil -import xsbti.{ Position, Problem, Severity, T2, UseScope, VirtualFileRef } +import xsbti.{ + DiagnosticCode, + DiagnosticRelatedInformation, + Position, + Problem, + Severity, + T2, + UseScope, + VirtualFileRef +} import xsbti.compile.{ CompileOrder, FileHash, MiniOptions, MiniSetup, Output, OutputGroup } import xsbti.compile.analysis.{ Compilation, ReadMapper, SourceInfo, Stamp } import sbt.internal.inc.binary.converters.ProtobufDefaults.Feedback.StringToException @@ -171,6 +180,17 @@ final class ProtobufReaders(mapper: ReadMapper, currentVersion: Schema.Version) private def fromInt(value: Int): Option[Integer] = if (value == MissingInt) None else Some(value) + private def fromDiagnosticCode(diagnosticCode: Schema.DiagnosticCode): DiagnosticCode = + new DiagnosticCode { + override val code: String = diagnosticCode.getCode() + override val explanation: Optional[String] = + InterfaceUtil.o2jo(fromString(diagnosticCode.getExplanation())) + } + + private def fromDiagnosticRelatedInformation(info: Schema.DiagnosticRelatedInformation) + : DiagnosticRelatedInformation = + ??? + def fromProblem(problem: Schema.Problem): Problem = { val category = problem.getCategory val message = problem.getMessage @@ -179,7 +199,20 @@ final class ProtobufReaders(mapper: ReadMapper, currentVersion: Schema.Version) if (problem.hasPosition) fromPosition(problem.getPosition) else ReadersFeedback.ExpectedPositionInProblem.!! val rendered = fromString(problem.getRendered) - InterfaceUtil.problem(category, position, message, severity, rendered) + val diagnosticCode = + if (problem.hasDiagnosticCode) Some(fromDiagnosticCode(problem.getDiagnosticCode())) + else None + val infos = problem.getDiagnosticRelatedInforamationList().asScala.iterator + .map(fromDiagnosticRelatedInformation).toList + InterfaceUtil.problem( + cat = category, + pos = position, + msg = message, + sev = severity, + rendered = rendered, + diagnosticCode = diagnosticCode, + diagnosticRelatedInforamation = infos, + ) } def fromSourceInfo(sourceInfo: Schema.SourceInfo): SourceInfo = { diff --git a/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufWriters.scala b/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufWriters.scala index df88a4dc65..0a4c4e2eed 100644 --- a/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufWriters.scala +++ b/internal/zinc-persist/src/main/scala/sbt/internal/inc/binary/converters/ProtobufWriters.scala @@ -14,8 +14,18 @@ package sbt.internal.inc.binary.converters import java.io.File import java.nio.file.Path +import scala.collection.JavaConverters._ import sbt.internal.inc._ -import xsbti.{ Position, Problem, Severity, T2, UseScope, VirtualFileRef } +import xsbti.{ + DiagnosticCode, + DiagnosticRelatedInformation, + Position, + Problem, + Severity, + T2, + UseScope, + VirtualFileRef +} import xsbti.compile.analysis.{ SourceInfo, Stamp, WriteMapper } import sbt.internal.inc.binary.converters.ProtobufDefaults.Feedback.{ Writers => WritersFeedback } import sbt.internal.inc.binary.converters.ProtobufDefaults.WritersConstants @@ -191,12 +201,31 @@ final class ProtobufWriters(mapper: WriteMapper) { val message = problem.message() val position = toPosition(problem.position()) val severity = toSeverity(problem.severity()) - Schema.Problem.newBuilder + val builder = Schema.Problem.newBuilder .setCategory(category) .setMessage(message) .setPosition(position) .setSeverity(severity) - .build + problem.rendered.toOption.foreach(r => builder.setRendered(r)) + problem.diagnosticCode.toOption.foreach(d => builder.setDiagnosticCode(toDiagnosticCode(d))) + problem.diagnosticRelatedInforamation.asScala + .foreach(d => builder.addDiagnosticRelatedInforamation(toDiagnosticRelatedInformation(d))) + builder.build + } + + def toDiagnosticCode(diagnosticCode: DiagnosticCode): Schema.DiagnosticCode = { + val builder = Schema.DiagnosticCode.newBuilder + .setCode(diagnosticCode.code()) + diagnosticCode.explanation().toOption.foreach(d => builder.setExplanation(d)) + builder.build + } + + def toDiagnosticRelatedInformation(info: DiagnosticRelatedInformation) + : Schema.DiagnosticRelatedInformation = { + val builder = Schema.DiagnosticRelatedInformation.newBuilder + builder.setPosition(toPosition(info.position())) + builder.setMessage(info.message()) + builder.build } def toSourceInfo(sourceInfo: SourceInfo): Schema.SourceInfo = { diff --git a/internal/zinc-persist/src/main/scala/sbt/internal/inc/text/TextAnalysisFormat.scala b/internal/zinc-persist/src/main/scala/sbt/internal/inc/text/TextAnalysisFormat.scala index dca18b300c..8ce87096b8 100644 --- a/internal/zinc-persist/src/main/scala/sbt/internal/inc/text/TextAnalysisFormat.scala +++ b/internal/zinc-persist/src/main/scala/sbt/internal/inc/text/TextAnalysisFormat.scala @@ -16,8 +16,8 @@ import java.nio.file.{ Path, Paths } import sbt.internal.inc._ import sbt.util.InterfaceUtil -import sbt.util.InterfaceUtil.{ jo2o, position, problem } -import xsbti.{ T2, UseScope, VirtualFileRef } +import sbt.util.InterfaceUtil.{ jl2l, jo2o, position, problem } +import xsbti.{ DiagnosticCode, DiagnosticRelatedInformation, T2, UseScope, VirtualFileRef } import xsbti.api._ import xsbti.compile._ import xsbti.compile.analysis.{ ReadWriteMappers, SourceInfo, Stamp } @@ -49,8 +49,6 @@ class TextAnalysisFormat(val mappers: ReadWriteMappers) asProduct3(read)(a => (a.name(), a.scope().name(), a.hash())) } private implicit val companionsFomrat: Format[Companions] = CompanionsFormat - private implicit def problemFormat: Format[Problem] = - asProduct5(problem)(p => (p.category, p.position, p.message, p.severity, jo2o(p.rendered))) private implicit def positionFormat: Format[Position] = asProduct13(position)(p => ( @@ -88,7 +86,28 @@ class TextAnalysisFormat(val mappers: ReadWriteMappers) asProduct2((file: Path, hash: Int) => FileHash.of(file, hash))(h => (h.file, h.hash)) private implicit def seqFormat[T](implicit optionFormat: Format[T]): Format[Seq[T]] = viaSeq[Seq[T], T](x => x) + private implicit def listFormat[A](implicit optionFormat: Format[A]): Format[List[A]] = + wrap[List[A], Seq[A]](_.toSeq, _.toList) private def t2[A1, A2](a1: A1, a2: A2): T2[A1, A2] = InterfaceUtil.t2(a1 -> a2) + private implicit def diagnosticCodeFormat: Format[DiagnosticCode] = + asProduct2(DiagnosticsUtil.diagnosticCode)(d => (d.code(), jo2o(d.explanation()))) + private implicit def diagnosticRelatedInformationFormat: Format[DiagnosticRelatedInformation] = + asProduct2(DiagnosticsUtil.diagnosticRelatedInformation)(d => (d.position(), d.message())) + private implicit def problemFormat: Format[Problem] = { + implicit val ev: Format[List[DiagnosticRelatedInformation]] = + listFormat(diagnosticRelatedInformationFormat) + asProduct7(problem)(p => + ( + p.category, + p.position, + p.message, + p.severity, + jo2o(p.rendered), + jo2o(p.diagnosticCode), + jl2l(p.diagnosticRelatedInforamation), + ) + ) + } // Companions portion of the API info is written in a separate entry later. def write(out: Writer, analysis: CompileAnalysis, setup: MiniSetup): Unit = { diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 76b3e5ded7..2fb66fd486 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -14,7 +14,7 @@ object Dependencies { val scala212_213 = Seq(defaultScalaVersion, scala213) private val ioVersion = nightlyVersion.getOrElse("1.7.0") - private val utilVersion = nightlyVersion.getOrElse("1.7.1") + private val utilVersion = nightlyVersion.getOrElse("1.7.2") private val sbtIO = "org.scala-sbt" %% "io" % ioVersion