@@ -7041,9 +7041,8 @@ namespace {
7041
7041
// If this closure had a function builder applied, rewrite it to a
7042
7042
// closure with a single expression body containing the builder
7043
7043
// invocations.
7044
- auto builder =
7045
- Rewriter.solution .builderTransformedClosures .find (closure);
7046
- if (builder != Rewriter.solution .builderTransformedClosures .end ()) {
7044
+ auto builder = Rewriter.solution .functionBuilderTransformed .find (closure);
7045
+ if (builder != Rewriter.solution .functionBuilderTransformed .end ()) {
7047
7046
auto singleExpr = builder->second .singleExpr ;
7048
7047
auto returnStmt = new (ctx) ReturnStmt (
7049
7048
singleExpr->getStartLoc (), singleExpr, /* implicit=*/ true );
@@ -7206,10 +7205,9 @@ bool ConstraintSystem::applySolutionFixes(const Solution &solution) {
7206
7205
7207
7206
// / Apply a given solution to the expression, producing a fully
7208
7207
// / type-checked expression.
7209
- Expr *ConstraintSystem::applySolution (Solution &solution, Expr *expr,
7210
- Type convertType,
7211
- bool discardedExpr,
7212
- bool performingDiagnostics) {
7208
+ llvm::PointerUnion<Expr *, Stmt *> ConstraintSystem::applySolutionImpl (
7209
+ Solution &solution, SolutionApplicationTarget target, Type convertType,
7210
+ bool discardedExpr, bool performingDiagnostics) {
7213
7211
// If any fixes needed to be applied to arrive at this solution, resolve
7214
7212
// them to specific expressions.
7215
7213
if (!solution.Fixes .empty ()) {
@@ -7228,18 +7226,32 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
7228
7226
7229
7227
// If we didn't manage to diagnose anything well, so fall back to
7230
7228
// diagnosing mining the system to construct a reasonable error message.
7231
- diagnoseFailureForExpr (expr );
7229
+ diagnoseFailureFor (target );
7232
7230
return nullptr ;
7233
7231
}
7234
7232
}
7235
7233
7236
7234
ExprRewriter rewriter (*this , solution, shouldSuppressDiagnostics ());
7237
7235
ExprWalker walker (rewriter);
7238
7236
7239
- // Apply the solution to the expression.
7240
- auto result = expr->walk (walker);
7241
- if (!result)
7242
- return nullptr ;
7237
+ // Apply the solution to the target.
7238
+ llvm::PointerUnion<Expr *, Stmt *> result;
7239
+ if (auto expr = target.getAsExpr ()) {
7240
+ result = expr->walk (walker);
7241
+ } else {
7242
+ // FIXME: Implement this!
7243
+ llvm_unreachable (" Not yet implemented" );
7244
+
7245
+ #if false
7246
+ auto fn = *target.getAsFunction ();
7247
+ auto transform = rewriter.getAppliedBuilderTransform (fn);
7248
+ assert (transform && " Not applying builder transform?" );
7249
+ result = walker.applyFunctionBuilderBodyTransform (fn, transform);
7250
+ #endif
7251
+ }
7252
+
7253
+ if (result.isNull ())
7254
+ return result;
7243
7255
7244
7256
// If we're re-typechecking an expression for diagnostics, don't
7245
7257
// visit closures that have non-single expression bodies.
@@ -7261,28 +7273,36 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
7261
7273
return nullptr ;
7262
7274
}
7263
7275
7264
- // We are supposed to use contextual type only if it is present and
7265
- // this expression doesn't represent the implicit return of the single
7266
- // expression function which got deduced to be `Never`.
7267
- auto shouldCoerceToContextualType = [&]() {
7268
- return convertType && !(getType (result)->isUninhabited () &&
7269
- getContextualTypePurpose () == CTP_ReturnSingleExpr);
7270
- };
7276
+ if (auto resultExpr = result.dyn_cast <Expr *>()) {
7277
+ Expr *expr = target.getAsExpr ();
7278
+ assert (expr && " Can't have expression result without expression target" );
7279
+ // We are supposed to use contextual type only if it is present and
7280
+ // this expression doesn't represent the implicit return of the single
7281
+ // expression function which got deduced to be `Never`.
7282
+ auto shouldCoerceToContextualType = [&]() {
7283
+ return convertType &&
7284
+ !(getType (resultExpr)->isUninhabited () &&
7285
+ getContextualTypePurpose () == CTP_ReturnSingleExpr);
7286
+ };
7271
7287
7272
- // If we're supposed to convert the expression to some particular type,
7273
- // do so now.
7274
- if (shouldCoerceToContextualType ()) {
7275
- result = rewriter.coerceToType (result, convertType,
7276
- getConstraintLocator (expr));
7277
- if (!result)
7278
- return nullptr ;
7279
- } else if (getType (result)->hasLValueType () && !discardedExpr) {
7280
- // We referenced an lvalue. Load it.
7281
- result = rewriter.coerceToType (result, getType (result)->getRValueType (),
7282
- getConstraintLocator (expr));
7288
+ // If we're supposed to convert the expression to some particular type,
7289
+ // do so now.
7290
+ if (shouldCoerceToContextualType ()) {
7291
+ result = rewriter.coerceToType (resultExpr, convertType,
7292
+ getConstraintLocator (expr));
7293
+ if (!result)
7294
+ return nullptr ;
7295
+ } else if (getType (resultExpr)->hasLValueType () && !discardedExpr) {
7296
+ // We referenced an lvalue. Load it.
7297
+ result = rewriter.coerceToType (resultExpr,
7298
+ getType (resultExpr)->getRValueType (),
7299
+ getConstraintLocator (expr));
7300
+ }
7301
+
7302
+ if (resultExpr)
7303
+ solution.setExprTypes (resultExpr);
7283
7304
}
7284
7305
7285
- solution.setExprTypes (result);
7286
7306
rewriter.finalize ();
7287
7307
7288
7308
return result;
@@ -7431,3 +7451,14 @@ ArrayRef<Solution> SolutionResult::getAmbiguousSolutions() const {
7431
7451
assert (getKind () == Ambiguous);
7432
7452
return makeArrayRef (solutions, numSolutions);
7433
7453
}
7454
+
7455
+ llvm::PointerUnion<Expr *, Stmt *> SolutionApplicationTarget::walk (
7456
+ ASTWalker &walker) {
7457
+ switch (kind) {
7458
+ case Kind::expression:
7459
+ return getAsExpr ()->walk (walker);
7460
+
7461
+ case Kind::function:
7462
+ return getAsFunction ()->getBody ()->walk (walker);
7463
+ }
7464
+ }
0 commit comments