@@ -120,11 +120,11 @@ object CheckCaptures:
120
120
report.error(em " $elem: $tpe is not a legal element of a capture set " , ann.srcPos)
121
121
ann.retainedSet.retainedElementsRaw.foreach(check)
122
122
123
- /** Under the sealed policy, report an error if some part of `tp` contains the
124
- * root capability in its capture set or if it refers to a type parameter that
125
- * could possibly be instantiated with cap in a way that's visible at the type .
123
+ /** Disallow bad roots anywhere in type `tp``.
124
+ * @param upto controls up to which owner local fresh capabilities should be disallowed.
125
+ * See disallowBadRoots for details .
126
126
*/
127
- private def disallowRootCapabilitiesIn (tp : Type , upto : Symbol , what : String , have : String , addendum : String , pos : SrcPos )(using Context ) =
127
+ private def disallowBadRootsIn (tp : Type , upto : Symbol , what : => String , have : => String , addendum : => String , pos : SrcPos )(using Context ) =
128
128
val check = new TypeTraverser :
129
129
130
130
private val seen = new EqHashSet [TypeRef ]
@@ -151,7 +151,7 @@ object CheckCaptures:
151
151
case CapturingType (parent, refs) =>
152
152
if variance >= 0 then
153
153
val openScopes = openExistentialScopes
154
- refs.disallowRootCapability (upto): () =>
154
+ refs.disallowBadRoots (upto): () =>
155
155
def part =
156
156
if t eq tp then " "
157
157
else
@@ -179,7 +179,7 @@ object CheckCaptures:
179
179
case t =>
180
180
traverseChildren(t)
181
181
check.traverse(tp)
182
- end disallowRootCapabilitiesIn
182
+ end disallowBadRootsIn
183
183
184
184
trait CheckerAPI :
185
185
/** Complete symbol info of a val or a def */
@@ -534,8 +534,9 @@ class CheckCaptures extends Recheck, SymTransformer:
534
534
// Under deferredReaches, don't propagate out of methods inside terms.
535
535
// The use set of these methods will be charged when that method is called.
536
536
537
- recur(cs, curEnv, null )
538
- useInfos += ((tree, cs, curEnv))
537
+ if ! cs.isAlwaysEmpty then
538
+ recur(cs, curEnv, null )
539
+ useInfos += ((tree, cs, curEnv))
539
540
end markFree
540
541
541
542
/** If capability `c` refers to a parameter that is not @use declared, report an error.
@@ -570,16 +571,16 @@ class CheckCaptures extends Recheck, SymTransformer:
570
571
case _ =>
571
572
tp
572
573
573
- /** Under the sealed policy, disallow the root capability in type arguments.
574
- * Type arguments come either from a TypeApply node or from an AppliedType
574
+ /** Type arguments come either from a TypeApply node or from an AppliedType
575
575
* which represents a trait parent in a template.
576
- * Also, if a corresponding formal type parameter is declared or implied @use,
577
- * charge the deep capture set of the argument to the environent.
576
+ * - Disallow global cap and result caps in such arguments.
577
+ * - If a corresponding formal type parameter is declared or implied @use,
578
+ * charge the deep capture set of the argument to the environent.
578
579
* @param fn the type application, of type TypeApply or TypeTree
579
580
* @param sym the constructor symbol (could be a method or a val or a class)
580
581
* @param args the type arguments
581
582
*/
582
- def disallowCapInTypeArgs (fn : Tree , sym : Symbol , args : List [Tree ])(using Context ): Unit =
583
+ def markFreeTypeArgs (fn : Tree , sym : Symbol , args : List [Tree ])(using Context ): Unit =
583
584
def isExempt = sym.isTypeTestOrCast || defn.capsErasedValueMethods.contains(sym)
584
585
if ! isExempt then
585
586
val paramNames = atPhase(thisPhase.prev):
@@ -592,17 +593,17 @@ class CheckCaptures extends Recheck, SymTransformer:
592
593
593
594
for case (arg : TypeTree , pname) <- args.lazyZip(paramNames) do
594
595
def where = if sym.exists then i " in an argument of $sym" else " "
595
- val ( addendum, errTree) =
596
+ def addendum =
596
597
if arg.isInferred
597
- then ( i " \n This is often caused by a local capability $where\n leaking as part of its result. " , fn)
598
- else if arg.span.exists then ( " " , arg)
599
- else ( " " , fn)
600
- disallowRootCapabilitiesIn (arg.nuType, NoSymbol ,
598
+ then i " \n This is often caused by a local capability $where\n leaking as part of its result. "
599
+ else " "
600
+ def errTree = if ! arg.isInferred && arg.span.exists then arg else fn
601
+ disallowBadRootsIn (arg.nuType, NoSymbol ,
601
602
i " Type variable $pname of $sym" , " be instantiated to" , addendum, errTree.srcPos)
602
603
603
604
val param = fn.symbol.paramNamed(pname)
604
605
if param.isUseParam then markFree(arg.nuType.deepCaptureSet, errTree)
605
- end disallowCapInTypeArgs
606
+ end markFreeTypeArgs
606
607
607
608
/** Rechecking idents involves:
608
609
* - adding call captures for idents referring to methods
@@ -870,7 +871,7 @@ class CheckCaptures extends Recheck, SymTransformer:
870
871
case fun @ Select (qual, nme.apply) => qual.symbol.orElse(fun.symbol)
871
872
case fun => fun.symbol
872
873
def methDescr = if meth.exists then i " $meth's type " else " "
873
- disallowCapInTypeArgs (tree.fun, meth, tree.args)
874
+ markFreeTypeArgs (tree.fun, meth, tree.args)
874
875
val funType = super .recheckTypeApply(tree, pt)
875
876
val res = resultToFresh(funType, Origin .ResultInstance (funType, meth))
876
877
includeCallCaptures(tree.symbol, res, tree)
@@ -986,7 +987,7 @@ class CheckCaptures extends Recheck, SymTransformer:
986
987
i " \n\n Note that $sym does not count as local since it is captured by $enclStr"
987
988
case _ =>
988
989
" "
989
- disallowRootCapabilitiesIn (
990
+ disallowBadRootsIn (
990
991
tree.tpt.nuType, NoSymbol , i " Mutable $sym" , " have type" , addendum, sym.srcPos)
991
992
checkInferredResult(super .recheckValDef(tree, sym), tree)
992
993
finally
@@ -1169,7 +1170,7 @@ class CheckCaptures extends Recheck, SymTransformer:
1169
1170
for case tpt : TypeTree <- impl.parents do
1170
1171
tpt.tpe match
1171
1172
case AppliedType (fn, args) =>
1172
- disallowCapInTypeArgs (tpt, fn.typeSymbol, args.map(TypeTree (_)))
1173
+ markFreeTypeArgs (tpt, fn.typeSymbol, args.map(TypeTree (_)))
1173
1174
case _ =>
1174
1175
ccState.inNestedLevelUnless(cls.is(Module )):
1175
1176
super .recheckClassDef(tree, impl, cls)
@@ -1202,7 +1203,7 @@ class CheckCaptures extends Recheck, SymTransformer:
1202
1203
recheck(tree.expr, pt)
1203
1204
val tp = recheckTryRest(bodyType, tree.cases, tree.finalizer, pt)
1204
1205
if Feature .enabled(Feature .saferExceptions) then
1205
- disallowRootCapabilitiesIn (tp, ctx.owner,
1206
+ disallowBadRootsIn (tp, ctx.owner,
1206
1207
" The result of `try`" , " have type" ,
1207
1208
" \n This is often caused by a locally generated exception capability leaking as part of its result." ,
1208
1209
tree.srcPos)
@@ -1946,7 +1947,7 @@ class CheckCaptures extends Recheck, SymTransformer:
1946
1947
if ! (pos.span.isSynthetic && ctx.reporter.errorsReported)
1947
1948
&& ! arg.typeSymbol.name.is(WildcardParamName )
1948
1949
then
1949
- CheckCaptures .disallowRootCapabilitiesIn (arg, NoSymbol ,
1950
+ CheckCaptures .disallowBadRootsIn (arg, NoSymbol ,
1950
1951
" Array" , " have element type" , " " ,
1951
1952
pos)
1952
1953
traverseChildren(t)
0 commit comments