@@ -858,14 +858,27 @@ class SpaceEngine(using Context) extends SpaceLogic {
858858 }
859859 }.apply(false , tp)
860860
861+ /** Return the underlying type of non-module, non-constant, non-enum case singleton types.
862+ * Also widen ExprType to its result type, and rewrap any annotation wrappers.
863+ * For example, with `val opt = None`, widen `opt.type` to `None.type`. */
864+ def toUnderlying (tp : Type )(using Context ): Type = trace(i " toUnderlying( $tp) " , show = true )(tp match {
865+ case _ : ConstantType => tp
866+ case tp : TermRef if tp.symbol.is(Module ) => tp
867+ case tp : TermRef if tp.symbol.isAllOf(EnumCase ) => tp
868+ case tp : SingletonType => toUnderlying(tp.underlying)
869+ case tp : ExprType => toUnderlying(tp.resultType)
870+ case AnnotatedType (tp, annot) => AnnotatedType (toUnderlying(tp), annot)
871+ case _ => tp
872+ })
873+
861874 def checkExhaustivity (_match : Match ): Unit = {
862875 val Match (sel, cases) = _match
863- val selTyp = sel.tpe.widen.dealias
876+ debug.println( i " checking exhaustivity of ${_match} " )
864877
865878 if (! exhaustivityCheckable(sel)) return
866879
867- debug.println( " checking " + _match.show)
868- debug.println(" selTyp = " + selTyp.show )
880+ val selTyp = toUnderlying(sel.tpe).dealias
881+ debug.println(i " selTyp = $ selTyp" )
869882
870883 val patternSpace = Or (cases.foldLeft(List .empty[Space ]) { (acc, x) =>
871884 val space = if (x.guard.isEmpty) project(x.pat) else Empty
@@ -898,13 +911,14 @@ class SpaceEngine(using Context) extends SpaceLogic {
898911 && ! sel.tpe.widen.isRef(defn.QuotedTypeClass )
899912
900913 def checkRedundancy (_match : Match ): Unit = {
901- debug.println(s " ---------------checking redundant patterns ${_match.show}" )
902-
903914 val Match (sel, cases) = _match
904- val selTyp = sel.tpe.widen.dealias
915+ debug.println( i " checking redundancy in $_match " )
905916
906917 if (! redundancyCheckable(sel)) return
907918
919+ val selTyp = toUnderlying(sel.tpe).dealias
920+ debug.println(i " selTyp = $selTyp" )
921+
908922 val isNullable = selTyp.classSymbol.isNullableClass
909923 val targetSpace = if isNullable
910924 then project(OrType (selTyp, constantNullType, soft = false ))
0 commit comments