@@ -2107,6 +2107,14 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2107
2107
}
2108
2108
}
2109
2109
2110
+ // For an @autoclosure default parameter, we want to convert to the result
2111
+ // type. Stash the autoclosure default parameter type.
2112
+ FunctionType *autoclosureDefaultParamType = nullptr ;
2113
+ if (convertTypePurpose == CTP_AutoclosureDefaultParameter) {
2114
+ autoclosureDefaultParamType = convertType.getType ()->castTo <FunctionType>();
2115
+ convertType.setType (autoclosureDefaultParamType->getResult ());
2116
+ }
2117
+
2110
2118
// Tell the constraint system what the contextual type is. This informs
2111
2119
// diagnostics and is a hint for various performance optimizations.
2112
2120
// FIXME: Look through LoadExpr. This is an egregious hack due to the
@@ -2130,6 +2138,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2130
2138
allowFreeTypeVariables = FreeTypeVariableBinding::UnresolvedType;
2131
2139
2132
2140
Type convertTo = convertType.getType ();
2141
+
2133
2142
if (options.contains (TypeCheckExprFlags::ExpressionTypeMustBeOptional)) {
2134
2143
assert (!convertTo && " convertType and type check options conflict" );
2135
2144
auto *convertTypeLocator =
@@ -2175,6 +2184,12 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
2175
2184
}
2176
2185
result = resultTarget->getAsExpr ();
2177
2186
2187
+ // For an @autoclosure default parameter type, add the autoclosure
2188
+ // conversion.
2189
+ if (convertTypePurpose == CTP_AutoclosureDefaultParameter) {
2190
+ result = cs.buildAutoClosureExpr (result, autoclosureDefaultParamType);
2191
+ }
2192
+
2178
2193
// Notify listener that we've applied the solution.
2179
2194
if (listener)
2180
2195
result = listener->appliedSolution (solution, result);
@@ -2204,32 +2219,9 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
2204
2219
DeclContext *DC, Type paramType,
2205
2220
bool isAutoClosure) {
2206
2221
assert (paramType && !paramType->hasError ());
2207
-
2208
- if (isAutoClosure) {
2209
- class AutoClosureListener : public ExprTypeCheckListener {
2210
- FunctionType *ParamType;
2211
-
2212
- public:
2213
- AutoClosureListener (FunctionType *paramType)
2214
- : ParamType(paramType) {}
2215
-
2216
- Expr *appliedSolution (constraints::Solution &solution,
2217
- Expr *expr) override {
2218
- auto &cs = solution.getConstraintSystem ();
2219
- return cs.buildAutoClosureExpr (expr, ParamType);
2220
- }
2221
- };
2222
-
2223
- auto *fnType = paramType->castTo <FunctionType>();
2224
- AutoClosureListener listener (fnType);
2225
- return typeCheckExpression (defaultValue, DC,
2226
- TypeLoc::withoutLoc (fnType->getResult ()),
2227
- CTP_DefaultParameter, TypeCheckExprOptions (),
2228
- &listener);
2229
- }
2230
-
2231
- return typeCheckExpression (defaultValue, DC, TypeLoc::withoutLoc (paramType),
2232
- CTP_DefaultParameter);
2222
+ return typeCheckExpression (
2223
+ defaultValue, DC, TypeLoc::withoutLoc (paramType),
2224
+ isAutoClosure ? CTP_AutoclosureDefaultParameter : CTP_DefaultParameter);
2233
2225
}
2234
2226
2235
2227
Type TypeChecker::
0 commit comments