@@ -2881,87 +2881,29 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
2881
2881
return true ;
2882
2882
}
2883
2883
2884
+ auto elementAssocType =
2885
+ cast<AssociatedTypeDecl>(
2886
+ sequenceProto->lookupDirect (tc.Context .Id_Element ).front ());
2887
+
2884
2888
SequenceType = cs.createTypeVariable (Locator, TVO_CanBindToNoEscape);
2885
2889
cs.addConstraint (ConstraintKind::Conversion, cs.getType (expr),
2886
2890
SequenceType, Locator);
2887
2891
cs.addConstraint (ConstraintKind::ConformsTo, SequenceType,
2888
2892
sequenceProto->getDeclaredType (), Locator);
2889
2893
2890
- auto iteratorLocator =
2891
- cs.getConstraintLocator (Locator,
2892
- ConstraintLocator::SequenceIteratorProtocol);
2893
2894
auto elementLocator =
2894
- cs.getConstraintLocator (iteratorLocator ,
2895
- ConstraintLocator::GeneratorElementType );
2895
+ cs.getConstraintLocator (Locator ,
2896
+ ConstraintLocator::SequenceElementType );
2896
2897
2897
2898
// Collect constraints from the element pattern.
2898
2899
auto pattern = Stmt->getPattern ();
2899
2900
InitType = cs.generateConstraints (pattern, elementLocator);
2900
2901
if (!InitType)
2901
2902
return true ;
2902
-
2903
- // Manually search for the iterator witness. If no iterator/element pair
2904
- // exists, solve for them.
2905
- Type iteratorType;
2906
- Type elementType;
2907
-
2908
- NameLookupOptions lookupOptions = defaultMemberTypeLookupOptions;
2909
- if (isa<AbstractFunctionDecl>(cs.DC ))
2910
- lookupOptions |= NameLookupFlags::KnownPrivate;
2911
-
2912
- auto sequenceType = cs.getType (expr)->getRValueType ();
2913
- if (auto *selfType = sequenceType->getAs <DynamicSelfType>())
2914
- sequenceType = selfType->getSelfType ();
2915
-
2916
- // Look through one level of optional; this improves recovery but doesn't
2917
- // change the result.
2918
- if (auto sequenceObjectType = sequenceType->getOptionalObjectType ())
2919
- sequenceType = sequenceObjectType;
2920
-
2921
- // If the sequence type is an existential, we should not attempt to
2922
- // look up the member type at all, since we cannot represent associated
2923
- // types of existentials.
2924
- //
2925
- // We will diagnose it later.
2926
- if (!sequenceType->isExistentialType () &&
2927
- (sequenceType->mayHaveMembers () ||
2928
- sequenceType->isTypeVariableOrMember ())) {
2929
- ASTContext &ctx = tc.Context ;
2930
- auto iteratorAssocType =
2931
- cast<AssociatedTypeDecl>(
2932
- sequenceProto->lookupDirect (ctx.Id_Iterator ).front ());
2933
-
2934
- auto subs = sequenceType->getContextSubstitutionMap (
2935
- cs.DC ->getParentModule (),
2936
- sequenceProto);
2937
- iteratorType = iteratorAssocType->getDeclaredInterfaceType ()
2938
- .subst (subs);
2939
-
2940
- if (iteratorType) {
2941
- auto iteratorProto =
2942
- tc.getProtocol (Stmt->getForLoc (),
2943
- KnownProtocolKind::IteratorProtocol);
2944
- if (!iteratorProto)
2945
- return true ;
2946
-
2947
- auto elementAssocType =
2948
- cast<AssociatedTypeDecl>(
2949
- iteratorProto->lookupDirect (ctx.Id_Element ).front ());
2950
-
2951
- elementType = iteratorType->getTypeOfMember (
2952
- cs.DC ->getParentModule (),
2953
- elementAssocType,
2954
- elementAssocType->getDeclaredInterfaceType ());
2955
- }
2956
- }
2957
-
2958
- if (elementType.isNull ()) {
2959
- elementType = cs.createTypeVariable (elementLocator,
2960
- TVO_CanBindToNoEscape);
2961
- }
2962
2903
2963
2904
// Add a conversion constraint between the element type of the sequence
2964
2905
// and the type of the element pattern.
2906
+ auto elementType = DependentMemberType::get (SequenceType, elementAssocType);
2965
2907
cs.addConstraint (ConstraintKind::Conversion, elementType, InitType,
2966
2908
elementLocator);
2967
2909
0 commit comments