Skip to content

Commit

Permalink
Merge pull request #1130 from eed3si9n/wip/1.7.2
Browse files Browse the repository at this point in the history
Util 1.7.2
  • Loading branch information
eed3si9n authored Oct 3, 2022
2 parents fc9e66a + dba1db2 commit 91d6f28
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}
}

Expand Down
12 changes: 12 additions & 0 deletions internal/zinc-persist-core/src/main/protobuf/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 =>
(
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 91d6f28

Please sign in to comment.