@@ -655,12 +655,8 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
655
655
656
656
// "if let" implicitly looks inside of an optional, so wrap it in an
657
657
// 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 ();
664
660
}
665
661
666
662
return P;
@@ -811,9 +807,24 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
811
807
//
812
808
// Refutable patterns occur when checking the PatternBindingDecls in if/let,
813
809
// 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
+
814
826
case PatternKind::Is:
815
827
case PatternKind::EnumElement:
816
- case PatternKind::OptionalSome:
817
828
case PatternKind::Bool:
818
829
case PatternKind::Expr:
819
830
// In a let/else, these always require an initial value to match against.
0 commit comments