@@ -671,44 +671,47 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
671
671
return P;
672
672
}
673
673
674
- static Type validateTypedPattern (TypeResolution resolution,
675
- TypedPattern *TP,
676
- TypeResolutionOptions options) {
677
- TypeLoc TL = TP->getTypeLoc ();
678
-
679
- bool hadError;
680
-
674
+ static Type validateTypedPattern (TypedPattern *TP, TypeResolution resolution) {
675
+ if (TP->hasType ()) {
676
+ return TP->getType ();
677
+ }
678
+
681
679
// If the pattern declares an opaque type, and applies to a single
682
680
// variable binding, then we can bind the opaque return type from the
683
681
// property definition.
684
682
auto &Context = resolution.getASTContext ();
685
- auto *Repr = TL. getTypeRepr ();
683
+ auto *Repr = TP-> getTypeRepr ();
686
684
if (Repr && isa<OpaqueReturnTypeRepr>(Repr)) {
687
685
auto named = dyn_cast<NamedPattern>(
688
686
TP->getSubPattern ()->getSemanticsProvidingPattern ());
689
- if (named) {
690
- auto *var = named->getDecl ();
691
- auto opaqueDecl = var->getOpaqueResultTypeDecl ();
692
- auto opaqueTy = (opaqueDecl
693
- ? opaqueDecl->getDeclaredInterfaceType ()
694
- : ErrorType::get (Context));
695
- TL.setType (named->getDecl ()->getDeclContext ()
696
- ->mapTypeIntoContext (opaqueTy));
697
- hadError = opaqueTy->hasError ();
698
- } else {
699
- Context.Diags .diagnose (TP->getLoc (), diag::opaque_type_unsupported_pattern);
700
- hadError = true ;
687
+ if (!named) {
688
+ Context.Diags .diagnose (TP->getLoc (),
689
+ diag::opaque_type_unsupported_pattern);
690
+ return ErrorType::get (Context);
701
691
}
702
- } else {
703
- hadError = TypeChecker::validateType (TL, resolution);
692
+
693
+ auto *var = named->getDecl ();
694
+ auto opaqueDecl = var->getOpaqueResultTypeDecl ();
695
+ if (!opaqueDecl) {
696
+ return ErrorType::get (Context);
697
+ }
698
+
699
+ auto opaqueTy = opaqueDecl->getDeclaredInterfaceType ();
700
+ if (opaqueTy->hasError ()) {
701
+ return ErrorType::get (Context);
702
+ }
703
+
704
+ return named->getDecl ()->getDeclContext ()->mapTypeIntoContext (opaqueTy);
704
705
}
705
706
706
- if (hadError) {
707
+ auto ty = resolution.resolveType (Repr);
708
+ if (!ty || ty->hasError ()) {
707
709
return ErrorType::get (Context);
708
710
}
709
711
710
- assert (!dyn_cast_or_null<SpecifierTypeRepr>(Repr));
711
- return TL.getType ();
712
+ assert (!dyn_cast_or_null<SpecifierTypeRepr>(Repr) &&
713
+ " Didn't resolve invalid type to error type!" );
714
+ return ty;
712
715
}
713
716
714
717
Type TypeChecker::typeCheckPattern (ContextualPattern pattern) {
@@ -768,8 +771,7 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
768
771
// that type.
769
772
case PatternKind::Typed: {
770
773
auto resolution = TypeResolution::forContextual (dc, options);
771
- TypedPattern *TP = cast<TypedPattern>(P);
772
- return validateTypedPattern (resolution, TP, options);
774
+ return validateTypedPattern (cast<TypedPattern>(P), resolution);
773
775
}
774
776
775
777
// A wildcard or name pattern cannot appear by itself in a context
@@ -824,7 +826,7 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
824
826
if (somePat->isImplicit () && isa<TypedPattern>(somePat->getSubPattern ())) {
825
827
auto resolution = TypeResolution::forContextual (dc, options);
826
828
TypedPattern *TP = cast<TypedPattern>(somePat->getSubPattern ());
827
- auto type = validateTypedPattern (resolution, TP, options );
829
+ auto type = validateTypedPattern (TP, resolution );
828
830
if (type && !type->hasError ()) {
829
831
return OptionalType::get (type);
830
832
}
0 commit comments