Skip to content

Commit bc622f5

Browse files
committed
Dont apply Fresh.FromCap to inferred types
They are already inferred with Fresh.Cap instances, so adding another map just confuses things.
1 parent 739815c commit bc622f5

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ object CaptureSet:
480480
end Fluid
481481

482482
/** The subclass of captureset variables with given initial elements */
483-
class Var(override val owner: Symbol = NoSymbol, initialElems: Refs = emptyRefs, val level: Level = undefinedLevel, underBox: Boolean = false)(using @constructorOnly ictx: Context) extends CaptureSet:
483+
class Var(initialOwner: Symbol = NoSymbol, initialElems: Refs = emptyRefs, val level: Level = undefinedLevel, underBox: Boolean = false)(using @constructorOnly ictx: Context) extends CaptureSet:
484+
485+
override def owner = initialOwner
484486

485487
/** A unique identification number for diagnostics */
486488
val id =
@@ -951,9 +953,14 @@ object CaptureSet:
951953
* which are already subject through snapshotting and rollbacks in VarState.
952954
* It's advantageous if we don't need to deal with other pieces of state there.
953955
*/
954-
class HiddenSet(owner: Symbol, initialHidden: Refs = emptyRefs)(using @constructorOnly ictx: Context)
955-
extends Var(owner, initialHidden):
956+
class HiddenSet(initialOwner: Symbol, initialHidden: Refs = emptyRefs)(using @constructorOnly ictx: Context)
957+
extends Var(initialOwner, initialHidden):
956958
var owningCap: AnnotatedType = uninitialized
959+
var givenOwner: Symbol = initialOwner
960+
961+
override def owner = givenOwner
962+
963+
// assert(id != 34, i"$initialHidden")
957964

958965
private def aliasRef: AnnotatedType | Null =
959966
if myElems.size == 1 then

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,33 @@ class CheckCaptures extends Recheck, SymTransformer:
326326
case _ =>
327327
traverseChildren(t)
328328

329+
/* Also set any previously unset owners of toplevel Fresh.Cap instances to improve
330+
* error diagnostics in separation checking.
331+
*/
332+
private def anchorCaps(sym: Symbol)(using Context) = new TypeTraverser:
333+
override def traverse(t: Type) =
334+
if variance > 0 then
335+
t match
336+
case t @ CapturingType(parent, refs) =>
337+
for ref <- refs.elems do
338+
ref match
339+
case Fresh.Cap(hidden) if !hidden.givenOwner.exists =>
340+
hidden.givenOwner = sym
341+
case _ =>
342+
traverse(parent)
343+
case t @ defn.RefinedFunctionOf(rinfo) =>
344+
traverse(rinfo)
345+
case _ =>
346+
traverseChildren(t)
347+
329348
/** If `tpt` is an inferred type, interpolate capture set variables appearing contra-
330-
* variantly in it.
349+
* variantly in it. Also anchor Fresh.Cap instances with anchorCaps.
331350
*/
332-
private def interpolateVarsIn(tpt: Tree)(using Context): Unit =
351+
private def interpolateVarsIn(tpt: Tree, sym: Symbol)(using Context): Unit =
333352
if tpt.isInstanceOf[InferredTypeTree] then
334353
interpolator().traverse(tpt.nuType)
335354
.showing(i"solved vars in ${tpt.nuType}", capt)
355+
anchorCaps(sym).traverse(tpt.nuType)
336356
for msg <- ccState.approxWarnings do
337357
report.warning(msg, tpt.srcPos)
338358
ccState.approxWarnings.clear()
@@ -952,7 +972,7 @@ class CheckCaptures extends Recheck, SymTransformer:
952972
// for more info from the context, so we cannot interpolate. Note that we cannot
953973
// expect to have all necessary info available at the point where the anonymous
954974
// function is compiled since we do not propagate expected types into blocks.
955-
interpolateVarsIn(tree.tpt)
975+
interpolateVarsIn(tree.tpt, sym)
956976

957977
/** Recheck method definitions:
958978
* - check body in a nested environment that tracks uses, in a nested level,
@@ -998,7 +1018,7 @@ class CheckCaptures extends Recheck, SymTransformer:
9981018
if !sym.isAnonymousFunction then
9991019
// Anonymous functions propagate their type to the enclosing environment
10001020
// so it is not in general sound to interpolate their types.
1001-
interpolateVarsIn(tree.tpt)
1021+
interpolateVarsIn(tree.tpt, sym)
10021022
curEnv = saved
10031023
end recheckDefDef
10041024

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
471471
tree.setNuType(
472472
if boxed then transformed
473473
else if sym.hasAnnotation(defn.UncheckedCapturesAnnot) then makeUnchecked(transformed)
474+
else if tree.isInferred then transformed
474475
else Fresh.fromCap(transformed, sym))
475476

476477
/** Transform the type of a val or var or the result type of a def */

tests/neg-custom-args/captures/outer-var.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
| Required: () ->{p} Unit
3333
|
3434
| Note that reference (q : () => Unit), defined in method inner
35-
| cannot be included in outer capture set {p}
35+
| cannot be included in outer capture set {p} of variable y
3636
|
3737
| longer explanation available when compiling with `-explain`
3838
-- Error: tests/neg-custom-args/captures/outer-var.scala:17:57 ---------------------------------------------------------

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
-- Error: tests/neg-custom-args/captures/vars.scala:24:14 --------------------------------------------------------------
22
24 | a = x => g(x) // error
33
| ^^^^
4-
| reference (cap3 : CC^) is not included in the allowed capture set {cap1}
5-
| of an enclosing function literal with expected type (x$0: String) ->{cap1} String
4+
| reference (cap3 : CC^) is not included in the allowed capture set {cap1} of variable a
65
|
76
| Note that reference (cap3 : CC^), defined in method scope
8-
| cannot be included in outer capture set {cap1}
7+
| cannot be included in outer capture set {cap1} of variable a
98
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/vars.scala:25:8 ------------------------------------------
109
25 | a = g // error
1110
| ^
1211
| Found: (x: String) ->{cap3} String
1312
| Required: (x$0: String) ->{cap1} String
1413
|
1514
| Note that reference (cap3 : CC^), defined in method scope
16-
| cannot be included in outer capture set {cap1}
15+
| cannot be included in outer capture set {cap1} of variable a
1716
|
1817
| longer explanation available when compiling with `-explain`
1918
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/vars.scala:27:12 -----------------------------------------

0 commit comments

Comments
 (0)