@@ -1603,14 +1603,32 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16031603 typedMatchFinish(tree, sel1, selType, tree.cases, pt)
16041604 }
16051605
1606+ /** Are some form of brackets necessary to annotate the tree `sel` as `@unchecked`?
1607+ * If so, return a Some(opening bracket, closing bracket), otherwise None.
1608+ */
1609+ def uncheckedBrackets (sel : untpd.Tree ): Option [(String , String )] = sel match
1610+ case _ : untpd.If
1611+ | _ : untpd.Match
1612+ | _ : untpd.ForYield
1613+ | _ : untpd.ParsedTry
1614+ | _ : untpd.Try => Some (" (" , " )" )
1615+ case _ : untpd.Block => Some (" {" , " }" )
1616+ case _ => None
1617+
16061618 result match {
16071619 case result @ Match (sel, CaseDef (pat, _, _) :: _) =>
16081620 tree.selector.removeAttachment(desugar.CheckIrrefutable ) match {
16091621 case Some (checkMode) if ! sel.tpe.hasAnnotation(defn.UncheckedAnnot ) =>
16101622 val isPatDef = checkMode == desugar.MatchCheck .IrrefutablePatDef
1611- if (! checkIrrefutable(sel, pat, isPatDef) && sourceVersion == `future-migration`)
1612- if (isPatDef) patch(Span (tree.selector.span.end), " : @unchecked" )
1613- else patch(Span (pat.span.start), " case " )
1623+ if ! checkIrrefutable(sel, pat, isPatDef) && sourceVersion == `future-migration` then
1624+ if isPatDef then uncheckedBrackets(tree.selector) match
1625+ case None =>
1626+ patch(Span (tree.selector.span.end), " : @unchecked" )
1627+ case Some (bl, br) =>
1628+ patch(Span (tree.selector.span.start), s " $bl" )
1629+ patch(Span (tree.selector.span.end), s " $br: @unchecked " )
1630+ else
1631+ patch(Span (tree.span.start), " case " )
16141632
16151633 // skip exhaustivity check in later phase
16161634 // TODO: move the check above to patternMatcher phase
0 commit comments