Skip to content

Commit bea9d06

Browse files
committed
[Constraint system] Set the type on all patterns, not just top-level ones.
1 parent 244a823 commit bea9d06

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

lib/Sema/CSGen.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,21 +2205,31 @@ namespace {
22052205
TVO_CanBindToNoEscape);
22062206
}
22072207

2208+
// Local function that must be called for each "return" throughout this
2209+
// function, to set the type of the pattern.
2210+
auto setType = [&](Type type) {
2211+
CS.setType(pattern, type);
2212+
return type;
2213+
};
2214+
22082215
switch (pattern->getKind()) {
22092216
case PatternKind::Paren:
22102217
// Parentheses don't affect the canonical type, but record them as
22112218
// type sugar.
2212-
return ParenType::get(
2213-
CS.getASTContext(),
2214-
getTypeForPattern(
2215-
cast<ParenPattern>(pattern)->getSubPattern(), locator));
2219+
return setType(
2220+
ParenType::get(
2221+
CS.getASTContext(),
2222+
getTypeForPattern(
2223+
cast<ParenPattern>(pattern)->getSubPattern(), locator)));
22162224
case PatternKind::Var:
22172225
// Var doesn't affect the type.
2218-
return getTypeForPattern(cast<VarPattern>(pattern)->getSubPattern(),
2219-
locator);
2226+
return setType(
2227+
getTypeForPattern(
2228+
cast<VarPattern>(pattern)->getSubPattern(), locator));
22202229
case PatternKind::Any: {
2221-
return CS.createTypeVariable(CS.getConstraintLocator(locator),
2222-
TVO_CanBindToNoEscape);
2230+
return setType(
2231+
CS.createTypeVariable(CS.getConstraintLocator(locator),
2232+
TVO_CanBindToNoEscape));
22232233
}
22242234

22252235
case PatternKind::Named: {
@@ -2233,10 +2243,10 @@ namespace {
22332243
ROK = OA->get();
22342244
switch (optionalityOf(ROK)) {
22352245
case ReferenceOwnershipOptionality::Required:
2236-
return TypeChecker::getOptionalType(var->getLoc(), varType);
2246+
return setType(TypeChecker::getOptionalType(var->getLoc(), varType));
22372247
case ReferenceOwnershipOptionality::Allowed:
22382248
case ReferenceOwnershipOptionality::Disallowed:
2239-
return varType;
2249+
return setType(varType);
22402250
}
22412251
}
22422252

@@ -2249,7 +2259,7 @@ namespace {
22492259

22502260
// For a typed pattern, simply return the opened type of the pattern.
22512261
// FIXME: Error recovery if the type is an error type?
2252-
return openedType;
2262+
return setType(openedType);
22532263
}
22542264

22552265
case PatternKind::Tuple: {
@@ -2263,15 +2273,15 @@ namespace {
22632273
LocatorPathElt::TupleElement(i)));
22642274
tupleTypeElts.push_back(TupleTypeElt(eltTy, tupleElt.getLabel()));
22652275
}
2266-
return TupleType::get(tupleTypeElts, CS.getASTContext());
2276+
return setType(TupleType::get(tupleTypeElts, CS.getASTContext()));
22672277
}
22682278

22692279
case PatternKind::OptionalSome: {
22702280
// The subpattern must have optional type.
22712281
Type subPatternType = getTypeForPattern(
22722282
cast<OptionalSomePattern>(pattern)->getSubPattern(), locator);
22732283

2274-
return OptionalType::get(subPatternType);
2284+
return setType(OptionalType::get(subPatternType));
22752285
}
22762286

22772287
case PatternKind::Is: {
@@ -2288,11 +2298,11 @@ namespace {
22882298
ConstraintKind::CheckedCast, subPatternType, castType, locator);
22892299
}
22902300

2291-
return subPatternType;
2301+
return setType(subPatternType);
22922302
}
22932303

22942304
case PatternKind::Bool:
2295-
return CS.getASTContext().getBoolDecl()->getDeclaredType();
2305+
return setType(CS.getASTContext().getBoolDecl()->getDeclaredType());
22962306

22972307
// Refutable patterns occur when checking the PatternBindingDecls in an
22982308
// if/let or while/let condition. They always require an initial value,
@@ -2301,8 +2311,9 @@ namespace {
23012311
case PatternKind::Expr:
23022312
// TODO: we could try harder here, e.g. for enum elements to provide the
23032313
// enum type.
2304-
return CS.createTypeVariable(CS.getConstraintLocator(locator),
2305-
TVO_CanBindToNoEscape);
2314+
return setType(
2315+
CS.createTypeVariable(
2316+
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape));
23062317
}
23072318

23082319
llvm_unreachable("Unhandled pattern kind");
@@ -3882,11 +3893,7 @@ static bool generateInitPatternConstraints(
38823893
auto locator =
38833894
cs.getConstraintLocator(initializer, LocatorPathElt::ContextualType());
38843895
Type patternType = cs.generateConstraints(pattern, locator);
3885-
if (!patternType)
3886-
return true;
3887-
3888-
// Record the type of this pattern.
3889-
cs.setType(pattern, patternType);
3896+
assert(patternType && "All patterns have a type");
38903897

38913898
if (auto wrappedVar = target.getInitializationWrappedVar()) {
38923899
// Add an equal constraint between the pattern type and the

0 commit comments

Comments
 (0)