diff --git a/compiler/src/dotty/tools/dotc/cc/Setup.scala b/compiler/src/dotty/tools/dotc/cc/Setup.scala index 3147a0f7bd47..cc456567ce8a 100644 --- a/compiler/src/dotty/tools/dotc/cc/Setup.scala +++ b/compiler/src/dotty/tools/dotc/cc/Setup.scala @@ -18,6 +18,7 @@ import reporting.Message import printing.{Printer, Texts}, Texts.{Text, Str} import collection.mutable import CCState.* +import dotty.tools.dotc.util.NoSourcePosition /** Operations accessed from CheckCaptures */ trait SetupAPI: @@ -323,7 +324,11 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI: val parent2 = stripImpliedCaptureSet(parent1) for tpt <- tptToCheck do checkWellformedLater(parent2, ann.tree, tpt) - CapturingType(parent2, ann.tree.toCaptureSet) + try + CapturingType(parent2, ann.tree.toCaptureSet) + catch case ex: IllegalCaptureRef => + report.error(em"Illegal capture reference: ${ex.getMessage.nn}", tptToCheck.fold(NoSourcePosition)(_.srcPos)) + t else t.derivedAnnotatedType(parent1, ann) case throwsAlias(res, exc) =>