Skip to content

Commit c632780

Browse files
committed
Use same logic for disallowBadRoots and checkAddedElems
1 parent 956c6b6 commit c632780

File tree

2 files changed

+10
-31
lines changed

2 files changed

+10
-31
lines changed

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,9 @@ sealed abstract class CaptureSet extends Showable:
482482
* Fresh instances count as good as long as their ccOwner is outside `upto`.
483483
* If `upto` is NoSymbol, all Fresh instances are admitted.
484484
*/
485-
def disallowBadRoots(upto: Symbol)(handler: () => Context ?=> Unit)(using Context): this.type =
486-
if elems.exists(isBadRoot(upto, _)) then handler()
487-
this
485+
def disallowBadRoots(upto: Symbol)(handler: () => Context ?=> Unit)(using Context): Unit =
486+
checkAddedElems: elem =>
487+
if isBadRoot(upto, elem) then handler()
488488

489489
/** Invoke handler for each element currently in the set and each element
490490
* added to it in the future.
@@ -738,12 +738,8 @@ object CaptureSet:
738738
elems -= empty
739739
this
740740

741-
/** A handler to be invoked if the root reference `cap` is added to this set */
742-
var rootAddedHandler: () => Context ?=> Unit = () => ()
743-
744-
/** A handler to be invoked when a new element is added to this set */
745-
var checkAddedElemHandler: Capability => Context ?=> Unit =
746-
elem => ()
741+
/** A list of handlers to be invoked when a new element is added to this set */
742+
var newElemAddedHandlers: List[Capability => Context ?=> Unit] = Nil
747743

748744
/** The limit deciding which capture roots are bad (i.e. cannot be contained in this set).
749745
* @see isBadRoot for details.
@@ -772,9 +768,6 @@ object CaptureSet:
772768
|| super.tryClassifyAs(cls)
773769
&& { narrowClassifier(cls); true }
774770

775-
/** A handler to be invoked when new elems are added to this set */
776-
var newElemAddedHandler: Capability => Context ?=> Unit = _ => ()
777-
778771
var description: String = ""
779772

780773
private var myRepr: Name | Null = null
@@ -827,9 +820,7 @@ object CaptureSet:
827820
catch case ex: AssertionError =>
828821
println(i"error for incl $elem in $this, ${summon[VarState].toString}")
829822
throw ex
830-
checkAddedElemHandler(elem)
831-
if isBadRoot(elem) then
832-
rootAddedHandler()
823+
newElemAddedHandlers.foreach(_(elem))
833824
val normElem = if isMaybeSet then elem else elem.stripMaybe
834825
// assert(id != 5 || elems.size != 3, this)
835826
val res = deps.forall: dep =>
@@ -877,23 +868,14 @@ object CaptureSet:
877868
|| isConst
878869
|| varState.canRecord && { includeDep(cs); true }
879870

880-
override def disallowBadRoots(upto: Symbol)(handler: () => Context ?=> Unit)(using Context): this.type =
871+
override def disallowBadRoots(upto: Symbol)(handler: () => Context ?=> Unit)(using Context): Unit =
881872
rootLimit = upto
882-
rootAddedHandler = handler
883873
super.disallowBadRoots(upto)(handler)
884874

885875
override def checkAddedElems(handler: Capability => Context ?=> Unit)(using Context): Unit =
886-
val prevHandler = checkAddedElemHandler
887-
checkAddedElemHandler =
888-
elem =>
889-
prevHandler(elem)
890-
handler(elem)
876+
newElemAddedHandlers = handler :: newElemAddedHandlers
891877
super.checkAddedElems(handler)
892878

893-
override def ensureWellformed(handler: Capability => (Context) ?=> Unit)(using Context): this.type =
894-
newElemAddedHandler = handler
895-
super.ensureWellformed(handler)
896-
897879
private var computingApprox = false
898880

899881
/** Roughly: the intersection of all constant known supersets of this set.
@@ -1032,8 +1014,7 @@ object CaptureSet:
10321014
* Test case: Without that tweak, logger.scala would not compile.
10331015
*/
10341016
override def disallowBadRoots(upto: Symbol)(handler: () => Context ?=> Unit)(using Context) =
1035-
if isRefining then this
1036-
else super.disallowBadRoots(upto)(handler)
1017+
if !isRefining then super.disallowBadRoots(upto)(handler)
10371018

10381019
end ProperVar
10391020

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,9 +2103,7 @@ class CheckCaptures extends Recheck, SymTransformer:
21032103
if !(pos.span.isSynthetic && ctx.reporter.errorsReported)
21042104
&& !arg.typeSymbol.name.is(WildcardParamName)
21052105
then
2106-
CheckCaptures.disallowBadRootsIn(arg, NoSymbol,
2107-
"Array", "have element type", "",
2108-
pos)
2106+
disallowBadRootsIn(arg, NoSymbol, "Array", "have element type", "", pos)
21092107
traverseChildren(t)
21102108
case defn.RefinedFunctionOf(rinfo: MethodType) =>
21112109
traverse(rinfo)

0 commit comments

Comments
 (0)