@@ -16,6 +16,8 @@ import ProtoTypes._
1616import Scopes ._
1717import CheckRealizable ._
1818import ErrorReporting .errorTree
19+ import rewrites .Rewrites .patch
20+ import util .Spans .Span
1921
2022import util .SourcePosition
2123import transform .SymUtils ._
@@ -594,6 +596,25 @@ trait Checking {
594596 ctx.error(ex " $cls cannot be instantiated since it ${rstatus.msg}" , pos)
595597 }
596598
599+ /** Check that pattern definition is either marked @unchecked or has a right
600+ * hand side with a type that conforms to the pattern's type.
601+ */
602+ def checkPatDefMatch (tree : Tree , pt : Type )(implicit ctx : Context ): Unit = tree match {
603+ case Match (_, CaseDef (pat, _, _) :: _)
604+ if ! pat.tpe.widen.hasAnnotation(defn.UncheckedAnnot ) && ! (pt <:< pat.tpe) =>
605+ val pt1 = pt match {
606+ case AnnotatedType (pt1, annot) if annot.matches(defn.UncheckedAnnot ) => pt1
607+ case _ => pt
608+ }
609+ ctx.errorOrMigrationWarning(
610+ ex """ pattern's type ${pat.tpe.widen} is more specialized than the right hand side expression's type $pt1
611+ |
612+ |If the narrowing is intentional, this can be communicated by writing `: @unchecked` after the pattern. ${err.rewriteNotice}""" ,
613+ pat.sourcePos)
614+ if (ctx.scala2Mode) patch(Span (pat.span.end), " : @unchecked" )
615+ case _ =>
616+ }
617+
597618 /** Check that `path` is a legal prefix for an import or export clause */
598619 def checkLegalImportPath (path : Tree )(implicit ctx : Context ): Unit = {
599620 checkStable(path.tpe, path.sourcePos)
0 commit comments