@@ -9254,6 +9254,40 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
9254
9254
}
9255
9255
}
9256
9256
9257
+ // Special handling of injected references to `makeIterator` in for-in loops.
9258
+ {
9259
+ auto memberRef = getAsExpr<UnresolvedDotExpr>(locator->getAnchor());
9260
+ if (memberRef && memberRef->isImplicit() &&
9261
+ locator->isLastElement<LocatorPathElt::Member>()) {
9262
+ // Cannot simplify this constraint yet since we don't know whether
9263
+ // the base type is going to be existential or not.
9264
+ if (baseObjTy->isTypeVariableOrMember())
9265
+ return formUnsolved();
9266
+
9267
+ auto *sequenceExpr = memberRef->getBase();
9268
+ // If base type is an existential, member lookup is fine because
9269
+ // it would return a witness.
9270
+ if (!baseObjTy->isExistentialType() &&
9271
+ getContextualTypePurpose(sequenceExpr) == CTP_ForEachSequence) {
9272
+ auto &ctx = getASTContext();
9273
+
9274
+ auto *sequenceProto = cast<ProtocolDecl>(
9275
+ getContextualType(sequenceExpr, /*forConstraint=*/false)
9276
+ ->getAnyNominal());
9277
+ bool isAsync = sequenceProto ==
9278
+ TypeChecker::getProtocol(
9279
+ ctx, SourceLoc(), KnownProtocolKind::AsyncSequence);
9280
+
9281
+ auto *makeIterator = isAsync ? ctx.getAsyncSequenceMakeAsyncIterator()
9282
+ : ctx.getSequenceMakeIterator();
9283
+
9284
+ return simplifyValueWitnessConstraint(
9285
+ ConstraintKind::ValueWitness, baseTy, makeIterator, memberTy, DC,
9286
+ FunctionRefKind::Compound, flags, locator);
9287
+ }
9288
+ }
9289
+ }
9290
+
9257
9291
MemberLookupResult result =
9258
9292
performMemberLookup(kind, member, baseTy, functionRefKind, locator,
9259
9293
/*includeInaccessibleMembers*/ shouldAttemptFixes());
@@ -9690,6 +9724,17 @@ ConstraintSystem::simplifyValueWitnessConstraint(
9690
9724
return SolutionKind::Unsolved;
9691
9725
};
9692
9726
9727
+ auto fail = [&] {
9728
+ // The constraint failed, so mark the member type as a "hole".
9729
+ // We cannot do anything further here.
9730
+ if (!shouldAttemptFixes())
9731
+ return SolutionKind::Error;
9732
+
9733
+ recordAnyTypeVarAsPotentialHole(memberType);
9734
+
9735
+ return SolutionKind::Solved;
9736
+ };
9737
+
9693
9738
// Resolve the base type, if we can. If we can't resolve the base type,
9694
9739
// then we can't solve this constraint.
9695
9740
Type baseObjectType = getFixedTypeRecursive(
@@ -9706,16 +9751,8 @@ ConstraintSystem::simplifyValueWitnessConstraint(
9706
9751
assert(proto && "Value witness constraint for a non-requirement");
9707
9752
auto conformance = useDC->getParentModule()->lookupConformance(
9708
9753
baseObjectType, proto);
9709
- if (!conformance) {
9710
- // The conformance failed, so mark the member type as a "hole". We cannot
9711
- // do anything further here.
9712
- if (!shouldAttemptFixes())
9713
- return SolutionKind::Error;
9714
-
9715
- recordAnyTypeVarAsPotentialHole(memberType);
9716
-
9717
- return SolutionKind::Solved;
9718
- }
9754
+ if (!conformance)
9755
+ return fail();
9719
9756
9720
9757
// Reference the requirement.
9721
9758
Type resolvedBaseType = simplifyType(baseType, flags);
@@ -9725,7 +9762,7 @@ ConstraintSystem::simplifyValueWitnessConstraint(
9725
9762
auto witness =
9726
9763
conformance.getWitnessByName(baseObjectType, requirement->getName());
9727
9764
if (!witness)
9728
- return SolutionKind::Error ;
9765
+ return fail() ;
9729
9766
9730
9767
auto choice = OverloadChoice(resolvedBaseType, witness.getDecl(), functionRefKind);
9731
9768
resolveOverload(getConstraintLocator(locator), memberType, choice,
0 commit comments