@@ -655,12 +655,8 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
655655
656656 // "if let" implicitly looks inside of an optional, so wrap it in an
657657 // OptionalSome pattern.
658- InnerP = new (Context) OptionalSomePattern (InnerP, InnerP->getEndLoc (),
659- true );
660- if (auto *TP = dyn_cast<TypedPattern>(P))
661- TP->setSubPattern (InnerP);
662- else
663- P = InnerP;
658+ P = new (Context) OptionalSomePattern (P, P->getEndLoc (), /* implicit*/ true );
659+ P->setImplicit ();
664660 }
665661
666662 return P;
@@ -811,9 +807,24 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
811807 //
812808 // Refutable patterns occur when checking the PatternBindingDecls in if/let,
813809 // while/let, and let/else conditions.
810+ case PatternKind::OptionalSome: {
811+ // Annotated if-let patterns are rewritten by TypeChecker::resolvePattern
812+ // to have an enclosing implicit (...)? pattern. If we can resolve the inner
813+ // typed pattern, the resulting pattern must have optional type.
814+ auto somePat = cast<OptionalSomePattern>(P);
815+ if (somePat->isImplicit () && isa<TypedPattern>(somePat->getSubPattern ())) {
816+ auto resolution = TypeResolution::forContextual (dc);
817+ TypedPattern *TP = cast<TypedPattern>(somePat->getSubPattern ());
818+ auto type = validateTypedPattern (resolution, TP, options);
819+ if (type && !type->hasError ()) {
820+ return OptionalType::get (type);
821+ }
822+ }
823+ LLVM_FALLTHROUGH;
824+ }
825+
814826 case PatternKind::Is:
815827 case PatternKind::EnumElement:
816- case PatternKind::OptionalSome:
817828 case PatternKind::Bool:
818829 case PatternKind::Expr:
819830 // In a let/else, these always require an initial value to match against.
0 commit comments