Skip to content

Commit 61ac253

Browse files
committed
[Type checker] Make coercePatternToType return the new Pattern.
Rather than mutating the parameter pattern in place and separately return whether an error occurred, return the new pattern or NULL if an error occurred. While here, switch over to ContextualPattern for the input. And get rid of that infernal "goto".
1 parent cc11fc6 commit 61ac253

File tree

7 files changed

+189
-112
lines changed

7 files changed

+189
-112
lines changed

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,7 @@ namespace {
22592259
// or exhaustive catches.
22602260
class FindInnerThrows : public ASTWalker {
22612261
ConstraintSystem &CS;
2262+
DeclContext *DC;
22622263
bool FoundThrow = false;
22632264

22642265
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
@@ -2344,12 +2345,12 @@ namespace {
23442345
Type exnType = CS.getASTContext().getErrorDecl()->getDeclaredType();
23452346
if (!exnType)
23462347
return false;
2347-
if (TypeChecker::coercePatternToType(pattern,
2348-
TypeResolution::forContextual(CS.DC),
2349-
exnType,
2350-
TypeResolverContext::InExpression)) {
2348+
auto contextualPattern =
2349+
ContextualPattern::forRawPattern(pattern, DC);
2350+
pattern = TypeChecker::coercePatternToType(
2351+
contextualPattern, exnType, TypeResolverContext::InExpression);
2352+
if (!pattern)
23512353
return false;
2352-
}
23532354

23542355
clause->setErrorPattern(pattern);
23552356
return clause->isSyntacticallyExhaustive();
@@ -2385,7 +2386,8 @@ namespace {
23852386
}
23862387

23872388
public:
2388-
FindInnerThrows(ConstraintSystem &cs) : CS(cs) {}
2389+
FindInnerThrows(ConstraintSystem &cs, DeclContext *dc)
2390+
: CS(cs), DC(dc) {}
23892391

23902392
bool foundThrow() { return FoundThrow; }
23912393
};
@@ -2398,7 +2400,7 @@ namespace {
23982400
if (!body)
23992401
return false;
24002402

2401-
auto tryFinder = FindInnerThrows(CS);
2403+
auto tryFinder = FindInnerThrows(CS, expr);
24022404
body->walk(tryFinder);
24032405
return tryFinder.foundThrow();
24042406
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,8 +2801,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
28012801
? TypeResolverContext::EditorPlaceholderExpr
28022802
: TypeResolverContext::InExpression;
28032803
options |= TypeResolutionFlags::OverrideType;
2804-
options |= TypeResolutionFlags::AllowUnspecifiedTypes;
2805-
options |= TypeResolutionFlags::AllowUnboundGenerics;
28062804

28072805
// FIXME: initTy should be the same as resultTy; now that typeCheckExpression()
28082806
// returns a Type and not bool, we should be able to simplify the listener
@@ -2812,8 +2810,12 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
28122810
return true;
28132811

28142812
// Apply the solution to the pattern as well.
2815-
if (coercePatternToType(pattern, TypeResolution::forContextual(DC), initTy,
2816-
options, TypeLoc())) {
2813+
auto contextualPattern =
2814+
ContextualPattern::forRawPattern(pattern, DC);
2815+
if (auto coercedPattern = TypeChecker::coercePatternToType(
2816+
contextualPattern, initTy, options)) {
2817+
pattern = coercedPattern;
2818+
} else {
28172819
return true;
28182820
}
28192821
}
@@ -3063,13 +3065,11 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
30633065
Pattern *pattern = Stmt->getPattern();
30643066
TypeResolutionOptions options(TypeResolverContext::ForEachStmt);
30653067
options |= TypeResolutionFlags::OverrideType;
3066-
options |= TypeResolutionFlags::AllowUnboundGenerics;
3067-
options |= TypeResolutionFlags::AllowUnspecifiedTypes;
3068-
if (TypeChecker::coercePatternToType(pattern,
3069-
TypeResolution::forContextual(DC),
3070-
InitType, options)) {
3068+
auto contextualPattern = ContextualPattern::forRawPattern(pattern, DC);
3069+
pattern = TypeChecker::coercePatternToType(contextualPattern,
3070+
InitType, options);
3071+
if (!pattern)
30713072
return nullptr;
3072-
}
30733073
Stmt->setPattern(pattern);
30743074

30753075
// Get the conformance of the sequence type to the Sequence protocol.
@@ -3189,6 +3189,7 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
31893189
hadAnyFalsable = true;
31903190
continue;
31913191
}
3192+
assert(elt.getKind() != StmtConditionElement::CK_Boolean);
31923193

31933194
// This is cleanup goop run on the various paths where type checking of the
31943195
// pattern binding fails.

0 commit comments

Comments
 (0)