@@ -671,44 +671,47 @@ Pattern *TypeChecker::resolvePattern(Pattern *P, DeclContext *DC,
671671 return P;
672672}
673673
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+
681679 // If the pattern declares an opaque type, and applies to a single
682680 // variable binding, then we can bind the opaque return type from the
683681 // property definition.
684682 auto &Context = resolution.getASTContext ();
685- auto *Repr = TL. getTypeRepr ();
683+ auto *Repr = TP-> getTypeRepr ();
686684 if (Repr && isa<OpaqueReturnTypeRepr>(Repr)) {
687685 auto named = dyn_cast<NamedPattern>(
688686 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);
701691 }
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);
704705 }
705706
706- if (hadError) {
707+ auto ty = resolution.resolveType (Repr);
708+ if (!ty || ty->hasError ()) {
707709 return ErrorType::get (Context);
708710 }
709711
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;
712715}
713716
714717Type TypeChecker::typeCheckPattern (ContextualPattern pattern) {
@@ -768,8 +771,7 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
768771 // that type.
769772 case PatternKind::Typed: {
770773 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);
773775 }
774776
775777 // A wildcard or name pattern cannot appear by itself in a context
@@ -824,7 +826,7 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
824826 if (somePat->isImplicit () && isa<TypedPattern>(somePat->getSubPattern ())) {
825827 auto resolution = TypeResolution::forContextual (dc, options);
826828 TypedPattern *TP = cast<TypedPattern>(somePat->getSubPattern ());
827- auto type = validateTypedPattern (resolution, TP, options );
829+ auto type = validateTypedPattern (TP, resolution );
828830 if (type && !type->hasError ()) {
829831 return OptionalType::get (type);
830832 }
0 commit comments