Skip to content

Commit 21348b1

Browse files
committed
Refactor error reporting when checking references
1 parent acacb13 commit 21348b1

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ object CaptureSet:
495495
val res = optional:
496496
(SimpleIdentitySet[CaptureRef]() /: elems): (acc, elem) =>
497497
if levelOK(elem) then acc + elem
498-
else if elem.isRootCapability then break()
499498
else
500499
val saved = triedElem
501500
triedElem = triedElem.orElse(Some(elem))
501+
if elem.isRootCapability then break()
502502
val res = acc ++ widenCaptures(elem.captureSetOfInfo.elems).?
503503
triedElem = saved // reset only in case of success, leave as is on error
504504
res

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import transform.SymUtils.*
1818
import transform.{Recheck, PreRecheck}
1919
import Recheck.*
2020
import scala.collection.mutable
21-
import CaptureSet.{withCaptureSetsExplained, IdempotentCaptRefMap}
21+
import CaptureSet.{withCaptureSetsExplained, IdempotentCaptRefMap, CompareResult}
2222
import StdNames.nme
2323
import NameKinds.DefaultGetterName
2424
import reporting.trace
@@ -262,21 +262,25 @@ class CheckCaptures extends Recheck, SymTransformer:
262262
def assertSub(cs1: CaptureSet, cs2: CaptureSet)(using Context) =
263263
assert(cs1.subCaptures(cs2, frozen = false).isOK, i"$cs1 is not a subset of $cs2")
264264

265+
def checkOK(res: CompareResult, prefix: => String, pos: SrcPos)(using Context): Unit =
266+
if !res.isOK then
267+
def toAdd: String = CaptureSet.levelErrors.toAdd.mkString
268+
report.error(em"$prefix included in the allowed capture set ${res.blocking}$toAdd", pos)
269+
265270
/** Check subcapturing `{elem} <: cs`, report error on failure */
266271
def checkElem(elem: CaptureRef, cs: CaptureSet, pos: SrcPos)(using Context) =
267-
val res = elem.singletonCaptureSet.subCaptures(cs, frozen = false)
268-
if !res.isOK then
269-
report.error(em"$elem cannot be referenced here; it is not included in the allowed capture set ${res.blocking}", pos)
272+
checkOK(
273+
elem.singletonCaptureSet.subCaptures(cs, frozen = false),
274+
i"$elem cannot be referenced here; it is not",
275+
pos)
270276

271277
/** Check subcapturing `cs1 <: cs2`, report error on failure */
272278
def checkSubset(cs1: CaptureSet, cs2: CaptureSet, pos: SrcPos)(using Context) =
273-
val res = cs1.subCaptures(cs2, frozen = false)
274-
if !res.isOK then
275-
def header =
279+
checkOK(
280+
cs1.subCaptures(cs2, frozen = false),
276281
if cs1.elems.size == 1 then i"reference ${cs1.elems.toList}%, % is not"
277-
else i"references $cs1 are not all"
278-
def toAdd: String = CaptureSet.levelErrors.toAdd.mkString
279-
report.error(em"$header included in allowed capture set ${res.blocking}$toAdd", pos)
282+
else i"references $cs1 are not all",
283+
pos)
280284

281285
/** The current environment */
282286
private var curEnv: Env = inContext(ictx):

0 commit comments

Comments
 (0)