Skip to content

Commit cd6ba35

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 00d00b1 + 09f2ca7 commit cd6ba35

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,6 +4329,19 @@ class ConstraintSystem {
43294329
llvm::function_ref<ConstraintFix *(unsigned, const OverloadChoice &)>
43304330
getFix = [](unsigned, const OverloadChoice &) { return nullptr; });
43314331

4332+
/// Generate constraints for the given property that has an
4333+
/// attached property wrapper.
4334+
///
4335+
/// \param wrappedVar The property that has a property wrapper.
4336+
/// \param initializerType The type of the initializer for the
4337+
/// backing storage variable.
4338+
/// \param propertyType The type of the wrapped property.
4339+
///
4340+
/// \returns true if there is an error.
4341+
bool generateWrappedPropertyTypeConstraints(VarDecl *wrappedVar,
4342+
Type initializerType,
4343+
Type propertyType);
4344+
43324345
/// Propagate constraints in an effort to enforce local
43334346
/// consistency to reduce the time to solve the system.
43344347
///

lib/Sema/CSGen.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,16 +3602,8 @@ static Expr *generateConstraintsFor(ConstraintSystem &cs, Expr *expr,
36023602
return result;
36033603
}
36043604

3605-
/// Generate constraints to produce the wrapped value type given the property
3606-
/// that has an attached property wrapper.
3607-
///
3608-
/// \param initializerType The type of the adjusted initializer, which
3609-
/// initializes the underlying storage variable.
3610-
/// \param wrappedVar The property that has a property wrapper.
3611-
/// \returns the type of the property.
3612-
static bool generateWrappedPropertyTypeConstraints(
3613-
ConstraintSystem &cs, Type initializerType, VarDecl *wrappedVar,
3614-
Type propertyType) {
3605+
bool ConstraintSystem::generateWrappedPropertyTypeConstraints(
3606+
VarDecl *wrappedVar, Type initializerType, Type propertyType) {
36153607
auto dc = wrappedVar->getInnermostDeclContext();
36163608

36173609
Type wrappedValueType;
@@ -3630,33 +3622,33 @@ static bool generateWrappedPropertyTypeConstraints(
36303622

36313623
if (!wrappedValueType) {
36323624
// Equate the outermost wrapper type to the initializer type.
3633-
auto *locator = cs.getConstraintLocator(typeExpr);
3625+
auto *locator = getConstraintLocator(typeExpr);
36343626
wrapperType =
3635-
cs.replaceInferableTypesWithTypeVars(rawWrapperType, locator);
3627+
replaceInferableTypesWithTypeVars(rawWrapperType, locator);
36363628
if (initializerType)
3637-
cs.addConstraint(ConstraintKind::Equal, wrapperType, initializerType, locator);
3629+
addConstraint(ConstraintKind::Equal, wrapperType, initializerType, locator);
36383630
} else {
36393631
// The former wrappedValue type must be equal to the current wrapper type
3640-
auto *locator = cs.getConstraintLocator(
3632+
auto *locator = getConstraintLocator(
36413633
typeExpr, LocatorPathElt::WrappedValue(wrapperType));
36423634
wrapperType =
3643-
cs.replaceInferableTypesWithTypeVars(rawWrapperType, locator);
3644-
cs.addConstraint(ConstraintKind::Equal, wrapperType, wrappedValueType, locator);
3635+
replaceInferableTypesWithTypeVars(rawWrapperType, locator);
3636+
addConstraint(ConstraintKind::Equal, wrapperType, wrappedValueType, locator);
36453637
}
36463638

3647-
cs.setType(typeExpr, wrapperType);
3639+
setType(typeExpr, wrapperType);
36483640

36493641
wrappedValueType = wrapperType->getTypeOfMember(
36503642
dc->getParentModule(), wrapperInfo.valueVar);
36513643
}
36523644

36533645
// The property type must be equal to the wrapped value type
3654-
cs.addConstraint(
3646+
addConstraint(
36553647
ConstraintKind::Equal, propertyType, wrappedValueType,
3656-
cs.getConstraintLocator(
3648+
getConstraintLocator(
36573649
wrappedVar, LocatorPathElt::ContextualType(CTP_WrappedProperty)));
3658-
cs.setContextualType(wrappedVar, TypeLoc::withoutLoc(wrappedValueType),
3659-
CTP_WrappedProperty);
3650+
setContextualType(wrappedVar, TypeLoc::withoutLoc(wrappedValueType),
3651+
CTP_WrappedProperty);
36603652
return false;
36613653
}
36623654

@@ -3675,8 +3667,8 @@ static bool generateInitPatternConstraints(
36753667
return true;
36763668

36773669
if (auto wrappedVar = target.getInitializationWrappedVar())
3678-
return generateWrappedPropertyTypeConstraints(
3679-
cs, cs.getType(target.getAsExpr()), wrappedVar, patternType);
3670+
return cs.generateWrappedPropertyTypeConstraints(
3671+
wrappedVar, cs.getType(target.getAsExpr()), patternType);
36803672

36813673
if (!patternType->is<OpaqueTypeArchetypeType>()) {
36823674
// Add a conversion constraint between the types.
@@ -3971,7 +3963,7 @@ bool ConstraintSystem::generateConstraints(
39713963
return true;
39723964

39733965
return generateWrappedPropertyTypeConstraints(
3974-
*this, /*initializerType=*/Type(), wrappedVar, propertyType);
3966+
wrappedVar, /*initializerType=*/Type(), propertyType);
39753967
} else {
39763968
auto pattern = target.getAsUninitializedVar();
39773969
auto locator = getConstraintLocator(
@@ -4161,7 +4153,8 @@ ConstraintSystem::applyPropertyWrapperToParameter(
41614153

41624154
initKind = PropertyWrapperInitKind::ProjectedValue;
41634155
} else {
4164-
generateWrappedPropertyTypeConstraints(*this, wrapperType, param, paramType);
4156+
Type wrappedValueType = computeWrappedValueType(param, wrapperType);
4157+
addConstraint(matchKind, paramType, wrappedValueType, locator);
41654158
initKind = PropertyWrapperInitKind::WrappedValue;
41664159
}
41674160

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8920,6 +8920,11 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
89208920
setType(projection, computeProjectedValueType(paramDecl, backingType));
89218921
}
89228922

8923+
if (!paramDecl->getName().hasDollarPrefix()) {
8924+
generateWrappedPropertyTypeConstraints(paramDecl, backingType,
8925+
param.getParameterType());
8926+
}
8927+
89238928
auto result = applyPropertyWrapperToParameter(backingType, param.getParameterType(),
89248929
paramDecl, paramDecl->getName(),
89258930
ConstraintKind::Equal,

test/Sema/property_wrapper_parameter_invalid.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct S {
8686
subscript(@Wrapper position: Int) -> Int { 0 }
8787
}
8888

89-
func testInvalidArgLabel() {
89+
func testInvalidArgLabel(projection: Projection<Int>) {
9090
// expected-note@+1 2 {{parameter 'argLabel' does not have an attached property wrapper}}
9191
func noWrappers(argLabel: Int) {}
9292

@@ -95,6 +95,11 @@ func testInvalidArgLabel() {
9595

9696
// expected-error@+1 {{cannot use property wrapper projection argument}}
9797
noWrappers($argLabel: 10)
98+
99+
func takesWrapper(@Wrapper argLabel: Int) {}
100+
101+
// expected-error@+1 {{cannot convert value of type 'Projection<Int>' to expected argument type 'Int'}}
102+
takesWrapper(argLabel: projection)
98103
}
99104

100105
protocol P {

0 commit comments

Comments
 (0)