@@ -2679,44 +2679,24 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2679
2679
2680
2680
if (isa<OptionalSomePattern>(pattern)) {
2681
2681
flags |= TypeCheckExprFlags::ExpressionTypeMustBeOptional;
2682
- } else if (patternType && !patternType->isEqual (Context.TheUnresolvedType )) {
2683
- contextualType = TypeLoc::withoutLoc (patternType);
2684
-
2685
- // If we already had an error, don't repeat the problem.
2686
- if (contextualType.getType ()->hasError ())
2687
- return true ;
2688
-
2689
- // Allow the initializer expression to establish the underlying type of an
2690
- // opaque type.
2691
- if (auto opaqueType = patternType->getAs <OpaqueTypeArchetypeType>()){
2692
- flags |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
2693
- }
2694
-
2695
- // Only provide a TypeLoc if it makes sense to allow diagnostics.
2696
- if (auto *typedPattern = dyn_cast<TypedPattern>(pattern)) {
2697
- const Pattern *inner = typedPattern->getSemanticsProvidingPattern ();
2698
- if (isa<NamedPattern>(inner) || isa<AnyPattern>(inner)) {
2699
- contextualType = typedPattern->getTypeLoc ();
2700
- if (!contextualType.getType ())
2701
- contextualType.setType (patternType);
2702
- }
2703
- }
2704
2682
}
2705
2683
2706
2684
// Type-check the initializer.
2707
- auto resultTy = typeCheckExpression (initializer, DC, contextualType,
2708
- contextualPurpose, flags, &listener);
2685
+ auto target = SolutionApplicationTarget::forInitialization (
2686
+ initializer, patternType, pattern);
2687
+ bool unresolvedTypeExprs = false ;
2688
+ auto resultTarget = typeCheckExpression (target, DC, unresolvedTypeExprs,
2689
+ flags, &listener);
2690
+
2691
+ if (resultTarget) {
2692
+ initializer = resultTarget->getAsExpr ();
2709
2693
2710
- if (resultTy) {
2711
2694
TypeResolutionOptions options =
2712
2695
isa<EditorPlaceholderExpr>(initializer->getSemanticsProvidingExpr ())
2713
2696
? TypeResolverContext::EditorPlaceholderExpr
2714
2697
: TypeResolverContext::InExpression;
2715
2698
options |= TypeResolutionFlags::OverrideType;
2716
2699
2717
- // FIXME: initTy should be the same as resultTy; now that typeCheckExpression()
2718
- // returns a Type and not bool, we should be able to simplify the listener
2719
- // implementation here.
2720
2700
auto initTy = listener.getPatternInitType (nullptr );
2721
2701
if (initTy->hasDependentMember ())
2722
2702
return true ;
@@ -2730,15 +2710,17 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2730
2710
} else {
2731
2711
return true ;
2732
2712
}
2713
+ } else {
2714
+ initializer = target.getAsExpr ();
2733
2715
}
2734
2716
2735
- if (!resultTy && !initializer->getType ())
2717
+ if (!resultTarget && !initializer->getType ())
2736
2718
initializer->setType (ErrorType::get (Context));
2737
2719
2738
2720
// If the type of the pattern is inferred, assign error types to the pattern
2739
2721
// and its variables, to prevent it from being referenced by the constraint
2740
2722
// system.
2741
- if (!resultTy &&
2723
+ if (!resultTarget &&
2742
2724
(patternType->hasUnresolvedType () ||
2743
2725
patternType->hasUnboundGenericType ())) {
2744
2726
pattern->setType (ErrorType::get (Context));
@@ -2754,7 +2736,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2754
2736
});
2755
2737
}
2756
2738
2757
- return !resultTy ;
2739
+ return !resultTarget ;
2758
2740
}
2759
2741
2760
2742
bool TypeChecker::typeCheckPatternBinding (PatternBindingDecl *PBD,
0 commit comments