@@ -1269,10 +1269,10 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12691269 case PatternKind::Expr: {
12701270 assert (cast<ExprPattern>(P)->isResolved ()
12711271 && " coercing unresolved expr pattern!" );
1272+ auto *EP = cast<ExprPattern>(P);
12721273 if (type->isBool ()) {
12731274 // The type is Bool.
12741275 // Check if the pattern is a Bool literal
1275- auto EP = cast<ExprPattern>(P);
12761276 if (auto *BLE = dyn_cast<BooleanLiteralExpr>(
12771277 EP->getSubExpr ()->getSemanticsProvidingExpr ())) {
12781278 P = new (Context) BoolPattern (BLE->getLoc (), BLE->getValue ());
@@ -1282,20 +1282,27 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
12821282 }
12831283
12841284 // case nil is equivalent to .none when switching on Optionals.
1285- if (type->getOptionalObjectType ()) {
1286- auto EP = cast<ExprPattern>(P);
1287- if (auto *NLE = dyn_cast<NilLiteralExpr>(EP->getSubExpr ())) {
1285+ if (auto *NLE = dyn_cast<NilLiteralExpr>(EP->getSubExpr ())) {
1286+ if (type->getOptionalObjectType ()) {
12881287 auto *NoneEnumElement = Context.getOptionalNoneDecl ();
12891288 auto *BaseTE = TypeExpr::createImplicit (type, Context);
12901289 P = new (Context) EnumElementPattern (
12911290 BaseTE, NLE->getLoc (), DeclNameLoc (NLE->getLoc ()),
12921291 NoneEnumElement->createNameRef (), NoneEnumElement, nullptr );
12931292 return TypeChecker::coercePatternToType (
12941293 pattern.forSubPattern (P, /* retainTopLevel=*/ true ), type, options);
1294+ } else {
1295+ // ...but for non-optional types it can never match! Diagnose it.
1296+ diags.diagnose (NLE->getLoc (),
1297+ diag::value_type_comparison_with_nil_illegal, type)
1298+ .warnUntilSwiftVersion (6 );
1299+
1300+ if (type->getASTContext ().isSwiftVersionAtLeast (6 ))
1301+ return nullptr ;
12951302 }
12961303 }
12971304
1298- if (TypeChecker::typeCheckExprPattern (cast<ExprPattern>(P) , dc, type))
1305+ if (TypeChecker::typeCheckExprPattern (EP , dc, type))
12991306 return nullptr ;
13001307
13011308 return P;
0 commit comments