Skip to content

Commit 6ad4b25

Browse files
committed
[Constraint system] Address review comments on if patterns in function builders
1 parent 141f3e7 commit 6ad4b25

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

lib/Sema/CSGen.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,18 @@ namespace {
21972197
/// type information with fresh type variables.
21982198
///
21992199
/// \param pattern The pattern.
2200+
///
2201+
/// \param locator The locator to use for generated constraints and
2202+
/// type variables.
2203+
///
2204+
/// \param externalPatternType The type imposed by the enclosing pattern,
2205+
/// if any. This will be non-null in cases where there is, e.g., a
2206+
/// pattern such as "is SubClass".
2207+
///
2208+
/// \param bindPatternVarsOneWay When true, generate fresh type variables
2209+
/// for the types of each variable declared within the pattern, along
2210+
/// with a one-way constraint binding that to the type to which the
2211+
/// variable will be ascribed or inferred.
22002212
Type getTypeForPattern(Pattern *pattern, ConstraintLocatorBuilder locator,
22012213
Type externalPatternType,
22022214
bool bindPatternVarsOneWay) {
@@ -2257,14 +2269,9 @@ namespace {
22572269
if (bindPatternVarsOneWay) {
22582270
oneWayVarType = CS.createTypeVariable(
22592271
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape);
2260-
if (externalPatternType) {
2261-
CS.addConstraint(
2262-
ConstraintKind::OneWayEqual, oneWayVarType,
2263-
externalPatternType, locator);
2264-
} else {
2265-
CS.addConstraint(
2266-
ConstraintKind::OneWayEqual, oneWayVarType, varType, locator);
2267-
}
2272+
CS.addConstraint(
2273+
ConstraintKind::OneWayEqual, oneWayVarType,
2274+
externalPatternType ? externalPatternType : varType, locator);
22682275
}
22692276

22702277
// If there is an externally-imposed type.
@@ -2357,16 +2364,13 @@ namespace {
23572364
case PatternKind::OptionalSome: {
23582365
// Remove an optional from the object type.
23592366
if (externalPatternType) {
2360-
if (Type objType = externalPatternType->getOptionalObjectType()) {
2361-
externalPatternType = objType;
2362-
} else {
2363-
Type objVar = CS.createTypeVariable(
2364-
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape);
2365-
CS.addConstraint(
2366-
ConstraintKind::Equal, OptionalType::get(objVar),
2367-
externalPatternType, locator);
2368-
externalPatternType = objVar;
2369-
}
2367+
Type objVar = CS.createTypeVariable(
2368+
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape);
2369+
CS.addConstraint(
2370+
ConstraintKind::OptionalObject, externalPatternType,
2371+
objVar, locator);
2372+
2373+
externalPatternType = objVar;
23702374
}
23712375

23722376
// The subpattern must have optional type.

0 commit comments

Comments
 (0)