Skip to content

Commit 947a6c3

Browse files
committed
[ConstraintSystem] In the solver, only generate constraints for wrapped
parameter attributes for closure parameters. For regular function parameters, these constraints will have already been generated and solved when computing the backing property wrapper type at the function declaration. If the solver also generates these constraints when type checking a function call, it leads to misleading diagnostics about an argument mismatch that will appear at the function declaration instead of the call-site.
1 parent 138c117 commit 947a6c3

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4150,7 +4150,8 @@ ConstraintSystem::applyPropertyWrapperToParameter(
41504150

41514151
initKind = PropertyWrapperInitKind::ProjectedValue;
41524152
} else {
4153-
generateWrappedPropertyTypeConstraints(param, wrapperType, paramType);
4153+
Type wrappedValueType = computeWrappedValueType(param, wrapperType);
4154+
addConstraint(matchKind, paramType, wrappedValueType, locator);
41544155
initKind = PropertyWrapperInitKind::WrappedValue;
41554156
}
41564157

lib/Sema/CSSimplify.cpp

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

8900+
if (!paramDecl->getName().hasDollarPrefix()) {
8901+
generateWrappedPropertyTypeConstraints(paramDecl, backingType,
8902+
param.getParameterType());
8903+
}
8904+
89008905
auto result = applyPropertyWrapperToParameter(backingType, param.getParameterType(),
89018906
paramDecl, paramDecl->getName(),
89028907
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)