Skip to content

Commit a79f3a6

Browse files
committed
[NFC][Property Wrappers] Simplify property wrapper initializer type checking.
1 parent 5e68204 commit a79f3a6

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,30 +2502,26 @@ static VarDecl *synthesizePropertyWrapperProjectionVar(
25022502
return property;
25032503
}
25042504

2505-
static void typeCheckSynthesizedWrapperInitializer(
2506-
PatternBindingDecl *pbd, VarDecl *backingVar, PatternBindingDecl *parentPBD,
2507-
Expr *&initializer) {
2505+
static void typeCheckSynthesizedWrapperInitializer(VarDecl *wrappedVar,
2506+
Expr *&initializer) {
25082507
// Figure out the context in which the initializer was written.
2508+
auto *parentPBD = wrappedVar->getParentPatternBinding();
2509+
auto i = parentPBD->getPatternEntryIndexForVarDecl(wrappedVar);
25092510
DeclContext *originalDC = parentPBD->getDeclContext();
25102511
if (!originalDC->isLocalContext()) {
25112512
auto initContext =
2512-
cast_or_null<PatternBindingInitializer>(parentPBD->getInitContext(0));
2513+
cast_or_null<PatternBindingInitializer>(parentPBD->getInitContext(i));
25132514
if (initContext)
25142515
originalDC = initContext;
25152516
}
25162517

25172518
// Type-check the initialization.
2518-
{
2519-
auto *wrappedVar = backingVar->getOriginalWrappedProperty();
2520-
auto i = parentPBD->getPatternEntryIndexForVarDecl(wrappedVar);
2521-
auto *pattern = parentPBD->getPattern(i);
2522-
TypeChecker::typeCheckBinding(pattern, initializer, originalDC,
2523-
wrappedVar->getType(), parentPBD, i);
2524-
}
2519+
auto *pattern = parentPBD->getPattern(i);
2520+
TypeChecker::typeCheckBinding(pattern, initializer, originalDC,
2521+
wrappedVar->getType(), parentPBD, i);
25252522

2526-
const auto i = pbd->getPatternEntryIndexForVarDecl(backingVar);
25272523
if (auto initializerContext =
2528-
dyn_cast_or_null<Initializer>(pbd->getInitContext(i))) {
2524+
dyn_cast_or_null<Initializer>(parentPBD->getInitContext(i))) {
25292525
TypeChecker::contextualizeInitializer(initializerContext, initializer);
25302526
}
25312527
}
@@ -2807,8 +2803,7 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
28072803
// FIXME: Record this expression somewhere so that DI can perform the
28082804
// initialization itself.
28092805
Expr *initializer = nullptr;
2810-
typeCheckSynthesizedWrapperInitializer(pbd, backingVar, parentPBD,
2811-
initializer);
2806+
typeCheckSynthesizedWrapperInitializer(var, initializer);
28122807
pbd->setInit(0, initializer);
28132808
pbd->setInitializerChecked(0);
28142809
} else if (var->hasObservers() && !dc->isTypeContext()) {
@@ -2846,28 +2841,20 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
28462841
}
28472842
}
28482843

2849-
// If no initial wrapped value was provided via '=' and either:
2850-
// 1. Not all of the attached property wrappers have init(wrappedValue:), or
2851-
// 2. An initializer has already been synthesized from arguments in the
2852-
// property wrapper attribute,
2853-
// then this property wrapper cannot be initialized out-of-line with a wrapped
2854-
// value.
2855-
if (!wrappedValue && (!var->allAttachedPropertyWrappersHaveWrappedValueInit() ||
2856-
initializer)) {
2857-
return PropertyWrapperBackingPropertyInfo(backingVar, projectionVar, nullptr,
2858-
projectedValueInit);
2859-
}
2860-
28612844
// Form the initialization of the backing property from a value of the
28622845
// original property's type.
2863-
Expr *wrappedValueInit = initializer;
2864-
if (!wrappedValueInit) {
2846+
Expr *wrappedValueInit = nullptr;
2847+
if (wrappedValue) {
2848+
wrappedValueInit = initializer;
2849+
} else if (!initializer &&
2850+
var->allAttachedPropertyWrappersHaveWrappedValueInit()) {
28652851
wrappedValueInit = PropertyWrapperValuePlaceholderExpr::create(
28662852
ctx, var->getSourceRange(), var->getType(), /*wrappedValue=*/nullptr);
28672853

28682854
if (auto *param = dyn_cast<ParamDecl>(var)) {
2869-
wrappedValueInit = buildPropertyWrapperInitCall(var, backingVar->getType(), wrappedValueInit,
2870-
PropertyWrapperInitKind::WrappedValue);
2855+
wrappedValueInit = buildPropertyWrapperInitCall(
2856+
var, backingVar->getType(), wrappedValueInit,
2857+
PropertyWrapperInitKind::WrappedValue);
28712858
TypeChecker::typeCheckExpression(wrappedValueInit, dc);
28722859

28732860
// Check initializer effects.
@@ -2877,8 +2864,7 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
28772864
checkInitializerActorIsolation(initContext, wrappedValueInit);
28782865
TypeChecker::checkInitializerEffects(initContext, wrappedValueInit);
28792866
} else {
2880-
typeCheckSynthesizedWrapperInitializer(
2881-
pbd, backingVar, var->getParentPatternBinding(), wrappedValueInit);
2867+
typeCheckSynthesizedWrapperInitializer(var, wrappedValueInit);
28822868
}
28832869
}
28842870

0 commit comments

Comments
 (0)