@@ -858,14 +858,27 @@ class SpaceEngine(using Context) extends SpaceLogic {
858
858
}
859
859
}.apply(false , tp)
860
860
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
+
861
874
def checkExhaustivity (_match : Match ): Unit = {
862
875
val Match (sel, cases) = _match
863
- val selTyp = sel.tpe.widen.dealias
876
+ debug.println( i " checking exhaustivity of ${_match} " )
864
877
865
878
if (! exhaustivityCheckable(sel)) return
866
879
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" )
869
882
870
883
val patternSpace = Or (cases.foldLeft(List .empty[Space ]) { (acc, x) =>
871
884
val space = if (x.guard.isEmpty) project(x.pat) else Empty
@@ -898,13 +911,14 @@ class SpaceEngine(using Context) extends SpaceLogic {
898
911
&& ! sel.tpe.widen.isRef(defn.QuotedTypeClass )
899
912
900
913
def checkRedundancy (_match : Match ): Unit = {
901
- debug.println(s " ---------------checking redundant patterns ${_match.show}" )
902
-
903
914
val Match (sel, cases) = _match
904
- val selTyp = sel.tpe.widen.dealias
915
+ debug.println( i " checking redundancy in $_match " )
905
916
906
917
if (! redundancyCheckable(sel)) return
907
918
919
+ val selTyp = toUnderlying(sel.tpe).dealias
920
+ debug.println(i " selTyp = $selTyp" )
921
+
908
922
val isNullable = selTyp.classSymbol.isNullableClass
909
923
val targetSpace = if isNullable
910
924
then project(OrType (selTyp, constantNullType, soft = false ))
0 commit comments