Skip to content

Commit adfb39f

Browse files
committed
Better error message for "annot have an inferred type"
1 parent 7ca48e3 commit adfb39f

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,18 +1120,23 @@ class CheckCaptures extends Recheck, SymTransformer:
11201120
}
11211121
if !canUseInferred then
11221122
val inferred = t.tpt.knownType
1123-
def checkPure(tp: Type) = tp match
1124-
case CapturingType(_, refs)
1125-
if !refs.elems.filter(isNotPureThis).isEmpty =>
1126-
val resultStr = if t.isInstanceOf[DefDef] then " result" else ""
1127-
report.error(
1128-
em"""Non-local $sym cannot have an inferred$resultStr type
1129-
|$inferred
1130-
|with non-empty capture set $refs.
1131-
|The type needs to be declared explicitly.""".withoutDisambiguation(),
1132-
t.srcPos)
1133-
case _ =>
1134-
inferred.foreachPart(checkPure, StopAt.Static)
1123+
val checkPure = new TypeTraverser:
1124+
def traverse(tp: Type) = tp match
1125+
case CapturingType(_, refs)
1126+
if !refs.elems.filter(isNotPureThis).isEmpty =>// && variance >= 0 =>
1127+
val resultStr = if t.isInstanceOf[DefDef] then " result" else ""
1128+
val partStr =
1129+
if tp eq inferred then "with non-empty capture set"
1130+
else i"since its part $tp has non-empty capture set"
1131+
report.error(
1132+
em"""Non-local $sym cannot have an inferred$resultStr type
1133+
|$inferred
1134+
|$partStr $refs.
1135+
|The type needs to be declared explicitly.""".withoutDisambiguation(),
1136+
t.srcPos)
1137+
case _ =>
1138+
traverseChildren(tp)
1139+
checkPure.traverse(inferred)
11351140
case t @ TypeApply(fun, args) =>
11361141
fun.knownType.widen match
11371142
case tl: PolyType =>

0 commit comments

Comments
 (0)