@@ -2050,6 +2050,14 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2050
2050
}
2051
2051
}
2052
2052
2053
+ // For an @autoclosure default parameter, we want to convert to the result
2054
+ // type. Stash the autoclosure default parameter type.
2055
+ FunctionType *autoclosureDefaultParamType = nullptr ;
2056
+ if (convertTypePurpose == CTP_AutoclosureDefaultParameter) {
2057
+ autoclosureDefaultParamType = convertType.getType ()->castTo <FunctionType>();
2058
+ convertType.setType (autoclosureDefaultParamType->getResult ());
2059
+ }
2060
+
2053
2061
// Tell the constraint system what the contextual type is. This informs
2054
2062
// diagnostics and is a hint for various performance optimizations.
2055
2063
// FIXME: Look through LoadExpr. This is an egregious hack due to the
@@ -2073,6 +2081,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2073
2081
allowFreeTypeVariables = FreeTypeVariableBinding::UnresolvedType;
2074
2082
2075
2083
Type convertTo = convertType.getType ();
2084
+
2076
2085
if (options.contains (TypeCheckExprFlags::ExpressionTypeMustBeOptional)) {
2077
2086
assert (!convertTo && " convertType and type check options conflict" );
2078
2087
auto *convertTypeLocator =
@@ -2118,6 +2127,12 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2118
2127
}
2119
2128
result = resultTarget->getAsExpr ();
2120
2129
2130
+ // For an @autoclosure default parameter type, add the autoclosure
2131
+ // conversion.
2132
+ if (convertTypePurpose == CTP_AutoclosureDefaultParameter) {
2133
+ result = cs.buildAutoClosureExpr (result, autoclosureDefaultParamType);
2134
+ }
2135
+
2121
2136
// Notify listener that we've applied the solution.
2122
2137
if (listener)
2123
2138
result = listener->appliedSolution (solution, result);
@@ -2147,32 +2162,9 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
2147
2162
DeclContext *DC, Type paramType,
2148
2163
bool isAutoClosure) {
2149
2164
assert (paramType && !paramType->hasError ());
2150
-
2151
- if (isAutoClosure) {
2152
- class AutoClosureListener : public ExprTypeCheckListener {
2153
- FunctionType *ParamType;
2154
-
2155
- public:
2156
- AutoClosureListener (FunctionType *paramType)
2157
- : ParamType(paramType) {}
2158
-
2159
- Expr *appliedSolution (constraints::Solution &solution,
2160
- Expr *expr) override {
2161
- auto &cs = solution.getConstraintSystem ();
2162
- return cs.buildAutoClosureExpr (expr, ParamType);
2163
- }
2164
- };
2165
-
2166
- auto *fnType = paramType->castTo <FunctionType>();
2167
- AutoClosureListener listener (fnType);
2168
- return typeCheckExpression (defaultValue, DC,
2169
- TypeLoc::withoutLoc (fnType->getResult ()),
2170
- CTP_DefaultParameter, TypeCheckExprOptions (),
2171
- &listener);
2172
- }
2173
-
2174
- return typeCheckExpression (defaultValue, DC, TypeLoc::withoutLoc (paramType),
2175
- CTP_DefaultParameter);
2165
+ return typeCheckExpression (
2166
+ defaultValue, DC, TypeLoc::withoutLoc (paramType),
2167
+ isAutoClosure ? CTP_AutoclosureDefaultParameter : CTP_DefaultParameter);
2176
2168
}
2177
2169
2178
2170
Type TypeChecker::
@@ -3152,30 +3144,7 @@ bool TypeChecker::typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
3152
3144
/* Implicit=*/ true );
3153
3145
3154
3146
// Check the expression as a condition.
3155
- //
3156
- // TODO: Type-check of `~=` operator can't (yet) use `typeCheckCondition`
3157
- // because that utilizes contextual type which interferes with diagnostics.
3158
- // We don't yet have a full access to pattern-matching context in
3159
- // constraint system, which is required to enable these situations
3160
- // to be properly diagnosed.
3161
- struct ConditionListener : public ExprTypeCheckListener {
3162
- // Add the appropriate Boolean constraint.
3163
- bool builtConstraints (ConstraintSystem &cs, Expr *expr) override {
3164
- // Otherwise, the result must be convertible to Bool.
3165
- auto boolDecl = cs.getASTContext ().getBoolDecl ();
3166
- if (!boolDecl)
3167
- return true ;
3168
-
3169
- // Condition must convert to Bool.
3170
- cs.addConstraint (ConstraintKind::Conversion, cs.getType (expr),
3171
- boolDecl->getDeclaredType (),
3172
- cs.getConstraintLocator (expr));
3173
- return false ;
3174
- }
3175
- };
3176
-
3177
- ConditionListener listener;
3178
- bool hadError = !typeCheckExpression (matchCall, DC, &listener);
3147
+ bool hadError = typeCheckCondition (matchCall, DC);
3179
3148
// Save the type-checked expression in the pattern.
3180
3149
EP->setMatchExpr (matchCall);
3181
3150
// Set the type on the pattern.
0 commit comments