Skip to content

Commit 94765e7

Browse files
committed
More refactoring, avoid globally accessible variable
1 parent a6fcc72 commit 94765e7

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import config.Feature
1616
import collection.mutable
1717
import CCState.*
1818
import reporting.Message
19-
import CaptureSet.{VarState, CompareResult}
19+
import CaptureSet.{VarState, CompareResult, CompareFailure}
2020

2121
/** Attachment key for capturing type trees */
2222
private val Captures: Key[CaptureSet] = Key()
@@ -92,16 +92,13 @@ class CCState:
9292
def test(op: => CompareResult): CompareResult =
9393
val saved = notes
9494
notes = Nil
95-
try op.withNotes(notes)
95+
try op match
96+
case res: CompareFailure => res.withNotes(notes)
97+
case res => res
9698
finally notes = saved
9799

98100
def testOK(op: => Boolean): CompareResult =
99-
val saved = notes
100-
notes = Nil
101-
try
102-
if op then CompareResult.OK
103-
else CompareResult.Fail(Nil).withNotes(notes)
104-
finally notes = saved
101+
test(if op then CompareResult.OK else CompareResult.Fail(Nil))
105102

106103
/** Warnings relating to upper approximations of capture sets with
107104
* existentially bound variables.

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

+9-12
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,17 @@ object CaptureSet:
10831083
*/
10841084
case class ExistentialSubsumesFailure(val ex: root.Result, val other: CaptureRef) extends ErrorNote
10851085

1086+
trait CompareFailure:
1087+
private var myErrorNotes: List[ErrorNote] = Nil
1088+
def errorNotes: List[ErrorNote] = myErrorNotes
1089+
def withNotes(notes: List[ErrorNote]): this.type =
1090+
myErrorNotes = notes
1091+
this
1092+
10861093
enum CompareResult extends Showable:
10871094
case OK
1088-
case Fail(trace: List[CaptureSet])
1089-
case LevelError(cs: CaptureSet, elem: CaptureRef) extends CompareResult, ErrorNote
1095+
case Fail(trace: List[CaptureSet]) extends CompareResult, CompareFailure
1096+
case LevelError(cs: CaptureSet, elem: CaptureRef) extends CompareResult, CompareFailure, ErrorNote
10901097

10911098
override def toText(printer: Printer): Text =
10921099
inContext(printer.printerContext):
@@ -1111,16 +1118,6 @@ object CaptureSet:
11111118
case result: LevelError => Some(result)
11121119
case _ => None
11131120

1114-
private var myErrorNotes: List[ErrorNote] = Nil
1115-
1116-
def errorNotes: List[ErrorNote] = myErrorNotes
1117-
1118-
def withNotes(notes: List[ErrorNote]): CompareResult = this match
1119-
case OK => OK
1120-
case _ =>
1121-
myErrorNotes = notes
1122-
this
1123-
11241121
inline def andAlso(op: Context ?=> CompareResult)(using Context): CompareResult =
11251122
if isOK then op else this
11261123

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

+25-23
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import util.{SimpleIdentitySet, EqHashMap, EqHashSet, SrcPos, Property}
1818
import transform.{Recheck, PreRecheck, CapturedVars}
1919
import Recheck.*
2020
import scala.collection.mutable
21-
import CaptureSet.{withCaptureSetsExplained, IdempotentCaptRefMap, CompareResult, ExistentialSubsumesFailure}
21+
import CaptureSet.{withCaptureSetsExplained, IdempotentCaptRefMap, CompareResult, CompareFailure, ExistentialSubsumesFailure}
2222
import CCState.*
2323
import StdNames.nme
2424
import NameKinds.{DefaultGetterName, WildcardParamName, UniqueNameKind}
@@ -352,13 +352,15 @@ class CheckCaptures extends Recheck, SymTransformer:
352352

353353
/** If `res` is not CompareResult.OK, report an error */
354354
def checkOK(res: CompareResult, prefix: => String, added: CaptureRef | CaptureSet, pos: SrcPos, provenance: => String = "")(using Context): Unit =
355-
if !res.isOK then
356-
inContext(root.printContext(added, res.blocking)):
357-
def toAdd: String = errorNotes(res.errorNotes).toAdd.mkString
358-
def descr: String =
359-
val d = res.blocking.description
360-
if d.isEmpty then provenance else ""
361-
report.error(em"$prefix included in the allowed capture set ${res.blocking}$descr$toAdd", pos)
355+
res match
356+
case res: CompareFailure =>
357+
inContext(root.printContext(added, res.blocking)):
358+
def toAdd: String = errorNotes(res.errorNotes).toAdd.mkString
359+
def descr: String =
360+
val d = res.blocking.description
361+
if d.isEmpty then provenance else ""
362+
report.error(em"$prefix included in the allowed capture set ${res.blocking}$descr$toAdd", pos)
363+
case _ =>
362364

363365
/** Check subcapturing `{elem} <: cs`, report error on failure */
364366
def checkElem(elem: CaptureRef, cs: CaptureSet, pos: SrcPos, provenance: => String = "")(using Context) =
@@ -1272,21 +1274,21 @@ class CheckCaptures extends Recheck, SymTransformer:
12721274
if actualBoxed eq actual then
12731275
// Only `addOuterRefs` when there is no box adaptation
12741276
expected1 = addOuterRefs(expected1, actual, tree.srcPos)
1275-
val result = ccState.testOK(isCompatible(actualBoxed, expected1))
1276-
if result.isOK then
1277-
if debugSuccesses then tree match
1278-
case Ident(_) =>
1279-
println(i"SUCCESS $tree for $actual <:< $expected:\n${TypeComparer.explained(_.isSubType(actualBoxed, expected1))}")
1280-
case _ =>
1281-
actualBoxed
1282-
else
1283-
capt.println(i"conforms failed for ${tree}: $actual vs $expected")
1284-
inContext(root.printContext(actualBoxed, expected1)):
1285-
err.typeMismatch(tree.withType(actualBoxed), expected1,
1286-
addApproxAddenda(
1287-
addenda ++ errorNotes(result.errorNotes),
1288-
expected1))
1289-
actual
1277+
ccState.testOK(isCompatible(actualBoxed, expected1)) match
1278+
case CompareResult.OK =>
1279+
if debugSuccesses then tree match
1280+
case Ident(_) =>
1281+
println(i"SUCCESS $tree for $actual <:< $expected:\n${TypeComparer.explained(_.isSubType(actualBoxed, expected1))}")
1282+
case _ =>
1283+
actualBoxed
1284+
case fail: CompareFailure =>
1285+
capt.println(i"conforms failed for ${tree}: $actual vs $expected")
1286+
inContext(root.printContext(actualBoxed, expected1)):
1287+
err.typeMismatch(tree.withType(actualBoxed), expected1,
1288+
addApproxAddenda(
1289+
addenda ++ errorNotes(fail.errorNotes),
1290+
expected1))
1291+
actual
12901292
end checkConformsExpr
12911293

12921294
/** Turn `expected` into a dependent function when `actual` is dependent. */

0 commit comments

Comments
 (0)