Skip to content

Commit e39d236

Browse files
slavapestovxedin
authored andcommitted
Sema: Simplify for ... in ... type checking
1 parent 7448507 commit e39d236

File tree

3 files changed

+14
-81
lines changed

3 files changed

+14
-81
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ void ConstraintLocator::Profile(llvm::FoldingSetNodeID &id, Expr *anchor,
6464
case ClosureResult:
6565
case ParentType:
6666
case InstanceType:
67-
case SequenceIteratorProtocol:
68-
case GeneratorElementType:
67+
case SequenceElementType:
6968
case AutoclosureResult:
7069
case GenericArgument:
7170
case NamedTupleElement:
@@ -257,8 +256,8 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
257256
out << "function result";
258257
break;
259258

260-
case GeneratorElementType:
261-
out << "generator element type";
259+
case SequenceElementType:
260+
out << "sequence element type";
262261
break;
263262

264263
case GenericArgument:
@@ -301,10 +300,6 @@ void ConstraintLocator::dump(SourceManager *sm, raw_ostream &out) {
301300
out << "rvalue adjustment";
302301
break;
303302

304-
case SequenceIteratorProtocol:
305-
out << "sequence iterator type";
306-
break;
307-
308303
case SubscriptMember:
309304
out << "subscript member";
310305
break;

lib/Sema/ConstraintLocator.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ class ConstraintLocator : public llvm::FoldingSetNode {
9898
ParentType,
9999
/// The instance of a metatype type.
100100
InstanceType,
101-
/// The generic type of a sequence.
102-
SequenceIteratorProtocol,
103-
/// The element type of a generator.
104-
GeneratorElementType,
101+
/// The element type of a sequence in a for ... in ... loop.
102+
SequenceElementType,
105103
/// An argument passed in an autoclosure parameter
106104
/// position, which must match the autoclosure return type.
107105
AutoclosureResult,
@@ -161,8 +159,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
161159
case ClosureResult:
162160
case ParentType:
163161
case InstanceType:
164-
case SequenceIteratorProtocol:
165-
case GeneratorElementType:
162+
case SequenceElementType:
166163
case AutoclosureResult:
167164
case Requirement:
168165
case Witness:
@@ -211,8 +208,7 @@ class ConstraintLocator : public llvm::FoldingSetNode {
211208
case ApplyArgument:
212209
case ApplyFunction:
213210
case ApplyArgToParam:
214-
case SequenceIteratorProtocol:
215-
case GeneratorElementType:
211+
case SequenceElementType:
216212
case ClosureResult:
217213
case ConstructorMember:
218214
case InstanceType:

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,87 +2881,29 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
28812881
return true;
28822882
}
28832883

2884+
auto elementAssocType =
2885+
cast<AssociatedTypeDecl>(
2886+
sequenceProto->lookupDirect(tc.Context.Id_Element).front());
2887+
28842888
SequenceType = cs.createTypeVariable(Locator, TVO_CanBindToNoEscape);
28852889
cs.addConstraint(ConstraintKind::Conversion, cs.getType(expr),
28862890
SequenceType, Locator);
28872891
cs.addConstraint(ConstraintKind::ConformsTo, SequenceType,
28882892
sequenceProto->getDeclaredType(), Locator);
28892893

2890-
auto iteratorLocator =
2891-
cs.getConstraintLocator(Locator,
2892-
ConstraintLocator::SequenceIteratorProtocol);
28932894
auto elementLocator =
2894-
cs.getConstraintLocator(iteratorLocator,
2895-
ConstraintLocator::GeneratorElementType);
2895+
cs.getConstraintLocator(Locator,
2896+
ConstraintLocator::SequenceElementType);
28962897

28972898
// Collect constraints from the element pattern.
28982899
auto pattern = Stmt->getPattern();
28992900
InitType = cs.generateConstraints(pattern, elementLocator);
29002901
if (!InitType)
29012902
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-
}
29622903

29632904
// Add a conversion constraint between the element type of the sequence
29642905
// and the type of the element pattern.
2906+
auto elementType = DependentMemberType::get(SequenceType, elementAssocType);
29652907
cs.addConstraint(ConstraintKind::Conversion, elementType, InitType,
29662908
elementLocator);
29672909

0 commit comments

Comments
 (0)