Skip to content

Commit ec74abb

Browse files
committed
Refactoring: Merge TypeComparer.ErrorNote and ErrorReporting.Note
The merged class is reporting.Message.Note.
1 parent 9744afe commit ec74abb

File tree

10 files changed

+49
-49
lines changed

10 files changed

+49
-49
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureSet.scala

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import annotation.threadUnsafe
1010
import annotation.constructorOnly
1111
import annotation.internal.sharable
1212
import reporting.trace
13+
import reporting.Message.Note
1314
import printing.{Showable, Printer}
1415
import printing.Texts.*
1516
import util.{SimpleIdentitySet, Property, EqHashMap}
1617
import scala.collection.{mutable, immutable}
17-
import TypeComparer.ErrorNote
1818
import CCState.*
1919
import TypeOps.AvoidMap
2020
import compiletime.uninitialized
@@ -160,8 +160,8 @@ sealed abstract class CaptureSet extends Showable:
160160

161161
final def keepAlways: Boolean = this.isInstanceOf[EmptyWithProvenance]
162162

163-
def failWith(fail: TypeComparer.ErrorNote)(using Context): false =
164-
TypeComparer.addErrorNote(fail)
163+
def failWith(note: Note)(using Context): false =
164+
TypeComparer.addErrorNote(note)
165165
false
166166

167167
/** Try to include an element in this capture set.
@@ -1304,16 +1304,18 @@ object CaptureSet:
13041304
* when a subsumes check decides that an existential variable `ex` cannot be
13051305
* instantiated to the other capability `other`.
13061306
*/
1307-
case class ExistentialSubsumesFailure(val ex: ResultCap, val other: Capability) extends ErrorNote:
1308-
def description(using Context): String =
1307+
case class ExistentialSubsumesFailure(val ex: ResultCap, val other: Capability) extends Note:
1308+
def render(using Context): String =
13091309
def reason =
13101310
if other.isTerminalCapability then ""
13111311
else " since that capability is not a `Sharable` capability"
1312-
i"""the existential capture root in ${ex.originalBinder.resType}
1312+
i"""
1313+
|
1314+
|Note that the existential capture root in ${ex.originalBinder.resType}
13131315
|cannot subsume the capability $other$reason."""
13141316

13151317
/** Failure indicating that `elem` cannot be included in `cs` */
1316-
case class IncludeFailure(cs: CaptureSet, elem: Capability, levelError: Boolean = false) extends ErrorNote, Showable:
1318+
case class IncludeFailure(cs: CaptureSet, elem: Capability, levelError: Boolean = false) extends Note, Showable:
13171319
private var myTrace: List[CaptureSet] = cs :: Nil
13181320

13191321
def trace: List[CaptureSet] = myTrace
@@ -1322,7 +1324,12 @@ object CaptureSet:
13221324
res.myTrace = cs1 :: this.myTrace
13231325
res
13241326

1325-
def description(using Context): String =
1327+
def render(using Context) =
1328+
i"""
1329+
|
1330+
|Note that $description."""
1331+
1332+
private def description(using Context): String =
13261333
def why =
13271334
val reasons = cs.elems.toList.collect:
13281335
case c: FreshCap if !c.acceptsLevelOf(elem) =>
@@ -1371,11 +1378,13 @@ object CaptureSet:
13711378
* @param lo the lower type of the orginal type comparison, or NoType if not known
13721379
* @param hi the upper type of the orginal type comparison, or NoType if not known
13731380
*/
1374-
case class MutAdaptFailure(cs: CaptureSet, lo: Type = NoType, hi: Type = NoType) extends ErrorNote:
1375-
def description(using Context): String =
1381+
case class MutAdaptFailure(cs: CaptureSet, lo: Type = NoType, hi: Type = NoType) extends Note:
1382+
def render(using Context): String =
13761383
def ofType(tp: Type) = if tp.exists then i"of the mutable type $tp" else "of a mutable type"
1377-
i"""$cs is an exclusive capture set ${ofType(hi)},
1378-
|it cannot subsume a read-only capture set ${ofType(lo)}"""
1384+
i"""
1385+
|
1386+
|Note that $cs is an exclusive capture set ${ofType(hi)},
1387+
|it cannot subsume a read-only capture set ${ofType(lo)}."""
13791388

13801389
/** A VarState serves as a snapshot mechanism that can undo
13811390
* additions of elements or super sets if an operation fails

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import typer.ForceDegree
1414
import typer.Inferencing.isFullyDefined
1515
import typer.RefChecks.{checkAllOverrides, checkSelfAgainstParents, OverridingPairsChecker}
1616
import typer.Checking.{checkBounds, checkAppliedTypesIn}
17-
import typer.ErrorReporting.{Note, err}
17+
import typer.ErrorReporting.err
1818
import typer.ProtoTypes.{LhsProto, WildcardSelectionProto, SelectionProto}
1919
import util.{SimpleIdentitySet, EqHashMap, EqHashSet, SrcPos, Property}
2020
import util.chaining.tap
@@ -26,6 +26,7 @@ import CCState.*
2626
import StdNames.nme
2727
import NameKinds.{DefaultGetterName, WildcardParamName, UniqueNameKind}
2828
import reporting.{trace, Message, OverrideError}
29+
import reporting.Message.Note
2930
import Annotations.Annotation
3031
import Capabilities.*
3132
import Mutability.*
@@ -399,7 +400,7 @@ class CheckCaptures extends Recheck, SymTransformer:
399400
case (fail: IncludeFailure) :: _ => fail.cs
400401
case _ => target
401402
def msg(provisional: Boolean) =
402-
def toAdd: String = errorNotes(otherNotes).map(_.render).mkString
403+
def toAdd: String = otherNotes.map(_.render).mkString
403404
def descr: String =
404405
val d = realTarget.description
405406
if d.isEmpty then provenance else ""
@@ -1444,13 +1445,6 @@ class CheckCaptures extends Recheck, SymTransformer:
14441445

14451446
type BoxErrors = mutable.ListBuffer[Message] | Null
14461447

1447-
private def errorNotes(notes: List[TypeComparer.ErrorNote])(using Context): List[Note] =
1448-
notes.map: note =>
1449-
Note:
1450-
i"""
1451-
|
1452-
|Note that ${note.description}."""
1453-
14541448
/** Addendas for error messages that show where we have under-approximated by
14551449
* mapping of a capability in contravariant position to the empty set because
14561450
* the original result type of the map was not itself a capability.
@@ -1544,10 +1538,10 @@ class CheckCaptures extends Recheck, SymTransformer:
15441538
}
15451539

15461540
TypeComparer.compareResult(tryCurrentType || tryWidenNamed) match
1547-
case TypeComparer.CompareResult.Fail(errNotes) =>
1541+
case TypeComparer.CompareResult.Fail(cmpNotes) =>
15481542
capt.println(i"conforms failed for ${tree}: $actual vs $expected")
15491543
if falseDeps then expected1 = unalignFunction(expected1)
1550-
val toAdd0 = notes ++ errorNotes(errNotes)
1544+
val toAdd0 = notes ++ cmpNotes
15511545
val toAdd1 = addApproxAddenda(toAdd0, expected1)
15521546
fail(tree.withType(actualBoxed), expected1, toAdd1)
15531547
actual

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import cc.*
2626
import Capabilities.Capability
2727
import NameKinds.WildcardParamName
2828
import MatchTypes.isConcrete
29+
import reporting.Message.Note
2930
import scala.util.boundary, boundary.break
3031

3132
/** Provides methods to compare types.
@@ -61,7 +62,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
6162
private var monitored = false
6263

6364
private var maxErrorLevel: Int = -1
64-
protected var errorNotes: List[(Int, ErrorNote)] = Nil
65+
protected var errorNotes: List[(Int, Note)] = Nil
6566

6667
val undoLog = mutable.ArrayBuffer[() => Unit]()
6768

@@ -3323,10 +3324,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
33233324
def reduceMatchWith[T](op: MatchReducer => T)(using Context): T =
33243325
inSubComparer(matchReducer)(op)
33253326

3326-
/** Add given ErrorNote note, provided there is not yet an error note with
3327+
/** Add given note, provided there is not yet an error note with
33273328
* the same class as `note`.
33283329
*/
3329-
def addErrorNote(note: ErrorNote): Unit =
3330+
def addErrorNote(note: Note): Unit =
33303331
if errorNotes.forall(_._2.getClass != note.getClass) then
33313332
errorNotes = (recCount, note) :: errorNotes
33323333
assert(maxErrorLevel <= recCount)
@@ -3355,15 +3356,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
33553356

33563357
object TypeComparer {
33573358

3358-
/** A base trait for data producing addenda to error messages */
3359-
trait ErrorNote:
3360-
def description(using Context): String
3361-
end ErrorNote
3362-
33633359
/** A richer compare result, returned by `testSubType` and `test`. */
33643360
enum CompareResult:
33653361
case OK, OKwithGADTUsed, OKwithOpaquesUsed
3366-
case Fail(errorNotes: List[ErrorNote])
3362+
case Fail(errorNotes: List[Note])
33673363

33683364
/** Class for unification variables used in `natValue`. */
33693365
private class AnyConstantType extends UncachedGroundType with ValueType {
@@ -3541,10 +3537,10 @@ object TypeComparer {
35413537
def inNestedLevel(op: => Boolean)(using Context): Boolean =
35423538
currentComparer.inNestedLevel(op)
35433539

3544-
def addErrorNote(note: ErrorNote)(using Context): Unit =
3540+
def addErrorNote(note: Note)(using Context): Unit =
35453541
currentComparer.addErrorNote(note)
35463542

3547-
def updateErrorNotes(f: PartialFunction[ErrorNote, ErrorNote])(using Context): Unit =
3543+
def updateErrorNotes(f: PartialFunction[Note, Note])(using Context): Unit =
35483544
currentComparer.errorNotes = currentComparer.errorNotes.mapConserve: p =>
35493545
val (level, note) = p
35503546
if f.isDefinedAt(note) then (level, f(note)) else p
@@ -3958,7 +3954,7 @@ class ExplainingTypeComparer(initctx: Context, short: Boolean) extends TypeCompa
39583954
private val b = new StringBuilder
39593955
private var lastForwardGoal: String | Null = null
39603956

3961-
private def appendFailure(notes: List[ErrorNote]) =
3957+
private def appendFailure(notes: List[Note]) =
39623958
if lastForwardGoal != null then // last was deepest goal that failed
39633959
b.append(s" = false")
39643960
for case note: printing.Showable <- notes do

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ object Message:
4141
i"\n$what can be rewritten automatically under -rewrite $optionStr."
4242
else ""
4343

44+
/** A note can produce an added string for an error message */
45+
abstract class Note(val prefix: Boolean = false):
46+
def render(using Context): String
47+
48+
object Note:
49+
def apply(msg: Context ?=> String, prefix: Boolean = false) = new Note(prefix):
50+
def render(using Context) = msg
51+
4452
enum Disambiguation:
4553
case All
4654
case AllExcept(strs: List[String])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import ast.desugar
1818
import config.{Feature, MigrationVersion, ScalaVersion}
1919
import transform.patmat.Space
2020
import transform.patmat.SpaceEngine
21-
import typer.ErrorReporting.{err, matchReductionAddendum, substitutableTypeSymbolsInScope, Note}
21+
import typer.ErrorReporting.{err, matchReductionAddendum, substitutableTypeSymbolsInScope}
2222
import typer.ProtoTypes.{ViewProto, SelectionProto, FunProto}
2323
import typer.Implicits.*
2424
import typer.Inferencing
@@ -38,7 +38,7 @@ import scala.jdk.CollectionConverters.*
3838
import dotty.tools.dotc.util.SourceFile
3939
import dotty.tools.dotc.config.SourceVersion
4040
import DidYouMean.*
41-
import Message.Disambiguation
41+
import Message.{Disambiguation, Note}
4242

4343
/** Messages
4444
* ========

compiler/src/dotty/tools/dotc/transform/Recheck.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import typer.ErrorReporting.err
1515
import typer.ProtoTypes.{AnySelectionProto, LhsProto}
1616
import typer.TypeAssigner.seqLitType
1717
import typer.ConstFold
18-
import typer.ErrorReporting.Note
18+
import reporting.Message.Note
1919
import config.Printers.recheckr
2020
import util.Property
2121
import StdNames.nme

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import util.Spans.NoSpan
1212
import util.SrcPos
1313
import config.Feature
1414
import reporting.*
15+
import Message.Note
1516
import collection.mutable
1617

17-
1818
object ErrorReporting {
1919

2020
import tpd.*
@@ -71,14 +71,6 @@ object ErrorReporting {
7171
case _ => foldOver(s, tp)
7272
tps.foldLeft("")(collectMatchTrace)
7373

74-
/** A note can produce an added string for an error message */
75-
abstract class Note(val prefix: Boolean = false):
76-
def render(using Context): String
77-
78-
object Note:
79-
def apply(msg: Context ?=> String, prefix: Boolean = false) = new Note(prefix):
80-
def render(using Context) = msg
81-
8274
class Errors(using Context) {
8375

8476
/** An explanatory note to be added to error messages

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Feature.{migrateTo3, sourceVersion}
3131
import config.Printers.{implicits, implicitsDetailed}
3232
import collection.mutable
3333
import reporting.*
34+
import Message.Note
3435
import transform.Splicer
3536
import annotation.tailrec
3637

tests/neg-custom-args/captures/heal-tparam-cs.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| Note that capability x$0 is not included in capture set {cap}.
2121
|
2222
| Note that the existential capture root in () =>² Unit
23-
| cannot subsume the capability (x$0 : Capp^'s4) since that capability is not a `Sharable` capability..
23+
| cannot subsume the capability (x$0 : Capp^'s4) since that capability is not a `Sharable` capability.
2424
|
2525
| where: => refers to a root capability associated with the result type of (c: Capp^): () => Unit
2626
| =>² and ^ refer to the universal root capability

tests/neg-custom-args/captures/i16226.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
|Note that capability f1 is not included in capture set {cap}.
2626
|
2727
|Note that the existential capture root in LazyRef[B]^²
28-
|cannot subsume the capability (f1 : A^'s15 ->'s16 B^'s17) since that capability is not a `Sharable` capability..
28+
|cannot subsume the capability (f1 : A^'s15 ->'s16 B^'s17) since that capability is not a `Sharable` capability.
2929
|
3030
|where: => and cap refer to a root capability associated with the result type of (ref1: LazyRef[A^'s11]{val elem: () ->'s12 A^'s13}^'s14, f1: A^'s15 ->'s16 B^'s17):
3131
| LazyRef[B^'s19]{val elem: () => B^'s20}^{f1, ref1}

0 commit comments

Comments
 (0)