Skip to content

Commit 7d950b7

Browse files
committed
Better error message for capture errors involving self types
1 parent 9d31fb2 commit 7d950b7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,12 @@ class CheckCaptures extends Recheck, SymTransformer:
263263
pos, provenance)
264264

265265
/** Check subcapturing `cs1 <: cs2`, report error on failure */
266-
def checkSubset(cs1: CaptureSet, cs2: CaptureSet, pos: SrcPos, provenance: => String = "")(using Context) =
266+
def checkSubset(cs1: CaptureSet, cs2: CaptureSet, pos: SrcPos,
267+
provenance: => String = "", cs1description: String = "")(using Context) =
267268
checkOK(
268269
cs1.subCaptures(cs2, frozen = false),
269-
if cs1.elems.size == 1 then i"reference ${cs1.elems.toList.head} is not"
270-
else i"references $cs1 are not all",
270+
if cs1.elems.size == 1 then i"reference ${cs1.elems.toList.head}$cs1description is not"
271+
else i"references $cs1$cs1description are not all",
271272
pos, provenance)
272273

273274
/** The current environment */
@@ -683,9 +684,15 @@ class CheckCaptures extends Recheck, SymTransformer:
683684
if !param.hasAnnotation(defn.ConstructorOnlyAnnot) then
684685
checkSubset(param.termRef.captureSet, thisSet, param.srcPos) // (3)
685686
for pureBase <- cls.pureBaseClass do // (4)
687+
def selfType = impl.body
688+
.collect:
689+
case TypeDef(tpnme.SELF, rhs) => rhs
690+
.headOption
691+
.getOrElse(tree)
692+
.orElse(tree)
686693
checkSubset(thisSet,
687694
CaptureSet.empty.withDescription(i"of pure base class $pureBase"),
688-
tree.srcPos)
695+
selfType.srcPos, cs1description = " captured by this self type")
689696
super.recheckClassDef(tree, impl, cls)
690697
finally
691698
curEnv = saved

0 commit comments

Comments
 (0)