@@ -33,6 +33,7 @@ import NameKinds.DefaultGetterName
3333import NameOps ._
3434import SymDenotations .{NoCompleter , NoDenotation }
3535import Applications .unapplyArgs
36+ import Inferencing .isFullyDefined
3637import transform .patmat .SpaceEngine .isIrrefutable
3738import config .Feature
3839import config .Feature .sourceVersion
@@ -1362,6 +1363,21 @@ trait Checking {
13621363 def checkCanThrow (tp : Type , span : Span )(using Context ): Unit =
13631364 if Feature .enabled(Feature .saferExceptions) && tp.isCheckedException then
13641365 ctx.typer.implicitArgTree(defn.CanThrowClass .typeRef.appliedTo(tp), span)
1366+
1367+ /** Check that catch can generate a good CanThrow exception */
1368+ def checkCatch (pat : Tree , guard : Tree )(using Context ): Unit = pat match
1369+ case Typed (_ : Ident , tpt) if isFullyDefined(tpt.tpe, ForceDegree .none) && guard.isEmpty =>
1370+ // OK
1371+ case Bind (_, pat1) =>
1372+ checkCatch(pat1, guard)
1373+ case _ =>
1374+ val req =
1375+ if guard.isEmpty then " for cases of the form `ex: T` where `T` is fully defined"
1376+ else " if no pattern guard is given"
1377+ report.error(
1378+ em """ Implementation restriction: cannot generate CanThrow capability for this kind of catch.
1379+ |CanThrow capabilities can only be generated $req. """ ,
1380+ pat.srcPos)
13651381}
13661382
13671383trait ReChecking extends Checking {
@@ -1375,6 +1391,7 @@ trait ReChecking extends Checking {
13751391 override def checkMatchable (tp : Type , pos : SrcPos , pattern : Boolean )(using Context ): Unit = ()
13761392 override def checkNoModuleClash (sym : Symbol )(using Context ) = ()
13771393 override def checkCanThrow (tp : Type , span : Span )(using Context ): Unit = ()
1394+ override def checkCatch (pat : Tree , guard : Tree )(using Context ): Unit = ()
13781395}
13791396
13801397trait NoChecking extends ReChecking {
0 commit comments