Skip to content

Commit 5b195ae

Browse files
committed
[Constraint solver] Pass for-each contextual type information down the solver.
Rather than setting contextual type information twice (once as a dummy, once for real) when type-checking a for-each statement, pass down the appropriate contextual type information from the outset.
1 parent 1e3484c commit 5b195ae

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,12 +2988,6 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
29882988
cs.addConstraint(ConstraintKind::ConformsTo, SequenceType,
29892989
SequenceProto->getDeclaredType(), ContextualLocator);
29902990

2991-
// Since we are using "contextual type" here, it has to be recorded
2992-
// in the constraint system for diagnostics to have access to "purpose".
2993-
cs.setContextualType(
2994-
expr, TypeLoc::withoutLoc(SequenceProto->getDeclaredType()),
2995-
CTP_ForEachStmt);
2996-
29972991
auto elementLocator = cs.getConstraintLocator(
29982992
ContextualLocator, ConstraintLocator::SequenceElementType);
29992993

@@ -3065,10 +3059,6 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
30653059
ElementType = solution.simplifyType(ElementType);
30663060
IteratorType = solution.simplifyType(IteratorType);
30673061

3068-
// Perform any necessary conversions of the sequence (e.g. [T]! -> [T]).
3069-
expr = solution.coerceToType(expr, SequenceType, Locator);
3070-
if (!expr) return nullptr;
3071-
30723062
cs.cacheExprTypes(expr);
30733063
Stmt->setSequence(expr);
30743064
solution.setExprTypes(expr);
@@ -3159,8 +3149,15 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
31593149
Expr *seq = stmt->getSequence();
31603150
assert(seq && "type-checking an uninitialized for-each statement?");
31613151

3152+
auto sequenceProto = TypeChecker::getProtocol(
3153+
dc->getASTContext(), stmt->getForLoc(), KnownProtocolKind::Sequence);
3154+
if (!sequenceProto)
3155+
return true;
3156+
31623157
// Type-check the for-each loop sequence and element pattern.
3163-
auto resultTy = TypeChecker::typeCheckExpression(seq, dc, &listener);
3158+
auto resultTy = TypeChecker::typeCheckExpression(
3159+
seq, dc, TypeLoc::withoutLoc(sequenceProto->getDeclaredType()),
3160+
CTP_ForEachStmt, TypeCheckExprFlags::ConvertTypeIsOnlyAHint, &listener);
31643161
if (!resultTy)
31653162
return true;
31663163
return false;

0 commit comments

Comments
 (0)