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