Skip to content

Commit 739815c

Browse files
committed
Fix: Don't add implicit Capability captures to result types of constructors
1 parent 40a1bd3 commit 739815c

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
132132
def mappedInfo =
133133
if toBeUpdated.contains(sym)
134134
then symd.info // don't transform symbols that will anyway be updated
135-
else Fresh.fromCap(transformExplicitType(symd.info), sym)
135+
else Fresh.fromCap(transformExplicitType(symd.info, sym), sym)
136136
if Synthetics.needsTransform(symd) then
137137
Synthetics.transform(symd, mappedInfo)
138138
else if isPreCC(sym) then
@@ -302,7 +302,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
302302
* 5. Schedule deferred well-formed tests for types with retains annotations.
303303
* 6. Perform normalizeCaptures
304304
*/
305-
private def transformExplicitType(tp: Type, tptToCheck: Tree = EmptyTree)(using Context): Type =
305+
private def transformExplicitType(tp: Type, sym: Symbol, tptToCheck: Tree = EmptyTree)(using Context): Type =
306306

307307
def fail(msg: Message) =
308308
if !tptToCheck.isEmpty then report.error(msg, tptToCheck.srcPos)
@@ -387,7 +387,11 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
387387
this(expandThrowsAlias(res, exc, Nil))
388388
case t =>
389389
// Map references to capability classes C to C^
390-
if t.derivesFromCapability && !t.isSingleton && t.typeSymbol != defn.Caps_Exists
390+
if t.derivesFromCapability
391+
&& !t.isSingleton
392+
&& t.typeSymbol != defn.Caps_Exists
393+
&& (!sym.isConstructor || (t ne tp.finalResultType))
394+
// Don't add ^ to result types of class constructors deriving from Capability
391395
then CapturingType(t, defn.universalCSImpliedByCapability, boxed = false)
392396
else normalizeCaptures(mapFollowingAliases(t))
393397
end toCapturing
@@ -460,7 +464,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
460464
var transformed =
461465
if tree.isInferred
462466
then transformInferredType(tree.tpe)
463-
else transformExplicitType(tree.tpe, tptToCheck = tree)
467+
else transformExplicitType(tree.tpe, sym, tptToCheck = tree)
464468
if boxed then transformed = box(transformed)
465469
if sym.is(Param) && (transformed ne tree.tpe) then
466470
paramSigChange += tree
@@ -685,7 +689,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
685689

686690
// Compute new parent types
687691
val ps1 = inContext(ctx.withOwner(cls)):
688-
ps.mapConserve(transformExplicitType(_))
692+
ps.mapConserve(transformExplicitType(_, NoSymbol))
689693

690694
// Install new types and if it is a module class also update module object
691695
if (selfInfo1 ne selfInfo) || (ps1 ne ps) then

tests/neg-custom-args/captures/sep-pairs.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/sep-pairs.scala:32:10 ------------------------------------
22
32 | Pair(Ref(), Ref()) // error // error: universal capability cannot be included in capture set
33
| ^^^^^
4-
| Found: box Ref^{cap.rd, cap}
4+
| Found: box Ref^
55
| Required: box Ref^?
66
|
7-
| Note that the universal capability `cap.rd`
7+
| Note that the universal capability `cap`
88
| cannot be included in capture set ?
99
|
1010
| longer explanation available when compiling with `-explain`
1111
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/sep-pairs.scala:32:17 ------------------------------------
1212
32 | Pair(Ref(), Ref()) // error // error: universal capability cannot be included in capture set
1313
| ^^^^^
14-
| Found: box Ref^{cap.rd, cap}
14+
| Found: box Ref^
1515
| Required: box Ref^?
1616
|
17-
| Note that the universal capability `cap.rd`
17+
| Note that the universal capability `cap`
1818
| cannot be included in capture set ?
1919
|
2020
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)