22
22
#include " MiscDiagnostics.h"
23
23
#include " SolutionResult.h"
24
24
#include " TypeCheckProtocol.h"
25
+ #include " TypeCheckType.h"
25
26
#include " swift/AST/ASTVisitor.h"
26
27
#include " swift/AST/ASTWalker.h"
27
28
#include " swift/AST/ExistentialLayout.h"
@@ -7230,12 +7231,13 @@ bool ConstraintSystem::applySolutionFixes(const Solution &solution) {
7230
7231
// / Apply the given solution to the initialization target.
7231
7232
// /
7232
7233
// / \returns the resulting initialiation expression.
7233
- static Expr *applySolutionToInitialization (
7234
- Solution &solution, SolutionApplicationTarget target, Expr *expr) {
7234
+ static Optional<SolutionApplicationTarget> applySolutionToInitialization (
7235
+ Solution &solution, SolutionApplicationTarget target,
7236
+ Expr *initializer) {
7235
7237
auto wrappedVar = target.getInitializationWrappedVar ();
7236
7238
Type initType;
7237
7239
if (wrappedVar) {
7238
- initType = solution.getType (expr );
7240
+ initType = solution.getType (initializer );
7239
7241
} else {
7240
7242
initType = solution.getType (target.getInitializationPattern ());
7241
7243
}
@@ -7249,10 +7251,13 @@ static Expr *applySolutionToInitialization(
7249
7251
// Convert the initializer to the type of the pattern.
7250
7252
auto &cs = solution.getConstraintSystem ();
7251
7253
auto locator =
7252
- cs.getConstraintLocator (expr, LocatorPathElt::ContextualType ());
7253
- expr = solution.coerceToType (expr, initType, locator);
7254
- if (!expr)
7255
- return nullptr ;
7254
+ cs.getConstraintLocator (initializer, LocatorPathElt::ContextualType ());
7255
+ initializer = solution.coerceToType (initializer, initType, locator);
7256
+ if (!initializer)
7257
+ return None;
7258
+
7259
+ SolutionApplicationTarget resultTarget = target;
7260
+ resultTarget.setExpr (initializer);
7256
7261
7257
7262
// Record the property wrapper type and note that the initializer has
7258
7263
// been subsumed by the backing property.
@@ -7264,10 +7269,40 @@ static Expr *applySolutionToInitialization(
7264
7269
7265
7270
// Record the semantic initializer on the outermost property wrapper.
7266
7271
wrappedVar->getAttachedPropertyWrappers ().front ()
7267
- ->setSemanticInit (expr );
7272
+ ->setSemanticInit (initializer );
7268
7273
}
7269
7274
7270
- return expr;
7275
+ // Coerce the pattern to the type of the initializer.
7276
+ TypeResolutionOptions options =
7277
+ isa<EditorPlaceholderExpr>(initializer->getSemanticsProvidingExpr ())
7278
+ ? TypeResolverContext::EditorPlaceholderExpr
7279
+ : TypeResolverContext::InExpression;
7280
+ options |= TypeResolutionFlags::OverrideType;
7281
+
7282
+ // Determine the type of the pattern.
7283
+ Type finalPatternType = initializer->getType ();
7284
+ if (wrappedVar) {
7285
+ if (!finalPatternType->hasError () && !finalPatternType->is <TypeVariableType>())
7286
+ finalPatternType = computeWrappedValueType (wrappedVar, finalPatternType);
7287
+ }
7288
+
7289
+ if (finalPatternType->hasDependentMember ())
7290
+ return None;
7291
+
7292
+ finalPatternType = finalPatternType->reconstituteSugar (/* recursive =*/ false );
7293
+
7294
+ // Apply the solution to the pattern as well.
7295
+ auto contextualPattern =
7296
+ ContextualPattern::forRawPattern (target.getInitializationPattern (),
7297
+ target.getDeclContext ());
7298
+ if (auto coercedPattern = TypeChecker::coercePatternToType (
7299
+ contextualPattern, finalPatternType, options)) {
7300
+ resultTarget.setPattern (coercedPattern);
7301
+ } else {
7302
+ return None;
7303
+ }
7304
+
7305
+ return resultTarget;
7271
7306
}
7272
7307
7273
7308
// / Apply a given solution to the expression, producing a fully
@@ -7310,13 +7345,15 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
7310
7345
7311
7346
// / Handle application for initializations.
7312
7347
if (target.getExprContextualTypePurpose () == CTP_Initialization) {
7313
- rewrittenExpr = applySolutionToInitialization (
7348
+ auto initResultTarget = applySolutionToInitialization (
7314
7349
solution, target, rewrittenExpr);
7315
- if (!rewrittenExpr )
7350
+ if (!initResultTarget )
7316
7351
return None;
7317
- }
7318
7352
7319
- result.setExpr (rewrittenExpr);
7353
+ result = *initResultTarget;
7354
+ } else {
7355
+ result.setExpr (rewrittenExpr);
7356
+ }
7320
7357
} else {
7321
7358
auto fn = *target.getAsFunction ();
7322
7359
0 commit comments