Skip to content

Commit c12e0a1

Browse files
authored
Merge pull request #78470 from slavapestov/fix-rdar141961300
Sema: Fix local property wrappers on constructor
2 parents 986e8b5 + c170443 commit c12e0a1

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,13 @@ class Solution {
16321632
/// A map from argument expressions to their applied property wrapper expressions.
16331633
llvm::DenseMap<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
16341634

1635+
ArrayRef<AppliedPropertyWrapper> getAppliedPropertyWrappers(ASTNode anchor) {
1636+
auto found = appliedPropertyWrappers.find(anchor);
1637+
if (found != appliedPropertyWrappers.end())
1638+
return found->second;
1639+
return ArrayRef<AppliedPropertyWrapper>();
1640+
}
1641+
16351642
/// A mapping from the constraint locators for references to various
16361643
/// names (e.g., member references, normal name references, possible
16371644
/// constructions) to the argument lists for the call to that locator.
@@ -5137,7 +5144,8 @@ class ConstraintSystem {
51375144
/// property wrapper type by applying the property wrapper.
51385145
TypeMatchResult applyPropertyWrapperToParameter(
51395146
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
5140-
ConstraintKind matchKind, ConstraintLocatorBuilder locator);
5147+
ConstraintKind matchKind, ConstraintLocator *locator,
5148+
ConstraintLocator *calleeLocator);
51415149

51425150
/// Used by applyPropertyWrapperToParameter() to update appliedPropertyWrappers
51435151
/// and record a change in the trail.

lib/Sema/CSApply.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,8 +1190,8 @@ namespace {
11901190
calleeFnTy = calleeFnTy->getResult()->castTo<FunctionType>();
11911191
}
11921192

1193-
const auto &appliedPropertyWrappers =
1194-
solution.appliedPropertyWrappers[locator.getAnchor()];
1193+
auto appliedPropertyWrappers =
1194+
solution.getAppliedPropertyWrappers(locator.getAnchor());
11951195
const auto calleeDeclRef = resolveConcreteDeclRef(
11961196
dyn_cast<AbstractFunctionDecl>(declOrClosure), locator);
11971197

@@ -2328,8 +2328,8 @@ namespace {
23282328
->castTo<FunctionType>();
23292329
auto fullSubscriptTy = openedFullFnType->getResult()
23302330
->castTo<FunctionType>();
2331-
auto &appliedWrappers =
2332-
solution.appliedPropertyWrappers[memberLoc->getAnchor()];
2331+
auto appliedWrappers =
2332+
solution.getAppliedPropertyWrappers(memberLoc->getAnchor());
23332333
args = coerceCallArguments(
23342334
args, fullSubscriptTy, subscriptRef, nullptr,
23352335
locator.withPathElement(ConstraintLocator::ApplyArgument),
@@ -6290,6 +6290,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
62906290
auto *paramDecl = getParameterAt(callee, paramIdx);
62916291
assert(paramDecl);
62926292

6293+
ASSERT(appliedWrapperIndex < appliedPropertyWrappers.size());
62936294
auto appliedWrapper = appliedPropertyWrappers[appliedWrapperIndex++];
62946295
auto wrapperType = solution.simplifyType(appliedWrapper.wrapperType);
62956296
auto initKind = appliedWrapper.initKind;
@@ -8195,7 +8196,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
81958196
// Resolve into a DynamicTypeExpr.
81968197
auto args = apply->getArgs();
81978198

8198-
auto &appliedWrappers = solution.appliedPropertyWrappers[calleeLocator.getAnchor()];
8199+
auto appliedWrappers = solution.getAppliedPropertyWrappers(
8200+
calleeLocator.getAnchor());
81998201
auto fnType = cs.getType(fn)->getAs<FunctionType>();
82008202
args = coerceCallArguments(
82018203
args, fnType, declRef, apply,
@@ -8391,7 +8393,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
83918393
// For function application, convert the argument to the input type of
83928394
// the function.
83938395
if (auto fnType = cs.getType(fn)->getAs<FunctionType>()) {
8394-
auto &appliedWrappers = solution.appliedPropertyWrappers[calleeLocator.getAnchor()];
8396+
auto appliedWrappers = solution.getAppliedPropertyWrappers(
8397+
calleeLocator.getAnchor());
8398+
83958399
args = coerceCallArguments(
83968400
args, fnType, callee, apply,
83978401
locator.withPathElement(ConstraintLocator::ApplyArgument),

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,11 +4209,9 @@ void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
42094209
ConstraintSystem::TypeMatchResult
42104210
ConstraintSystem::applyPropertyWrapperToParameter(
42114211
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
4212-
ConstraintKind matchKind, ConstraintLocatorBuilder locator) {
4213-
Expr *anchor = getAsExpr(locator.getAnchor());
4214-
if (auto *apply = dyn_cast<ApplyExpr>(anchor)) {
4215-
anchor = apply->getFn();
4216-
}
4212+
ConstraintKind matchKind, ConstraintLocator *locator,
4213+
ConstraintLocator *calleeLocator) {
4214+
Expr *anchor = getAsExpr(calleeLocator->getAnchor());
42174215

42184216
auto recordPropertyWrapperFix = [&](ConstraintFix *fix) -> TypeMatchResult {
42194217
if (!shouldAttemptFixes())

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,9 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
18501850
assert(param);
18511851
if (cs.applyPropertyWrapperToParameter(paramTy, argTy,
18521852
const_cast<ParamDecl *>(param),
1853-
wrapperArgLabel, subKind, loc)
1853+
wrapperArgLabel, subKind,
1854+
cs.getConstraintLocator(loc),
1855+
calleeLocator)
18541856
.isFailure()) {
18551857
return cs.getTypeMatchFailure(loc);
18561858
}
@@ -11883,6 +11885,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1188311885
auto result = applyPropertyWrapperToParameter(backingType, param.getParameterType(),
1188411886
paramDecl, paramDecl->getName(),
1188511887
ConstraintKind::Equal,
11888+
getConstraintLocator(closure),
1188611889
getConstraintLocator(closure));
1188711890
if (result.isFailure())
1188811891
return false;

lib/Sema/TypeOfReference.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,14 +750,15 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl *
750750
continue;
751751
}
752752

753-
auto *wrappedType = cs.createTypeVariable(cs.getConstraintLocator(locator), 0);
753+
auto *loc = cs.getConstraintLocator(locator);
754+
auto *wrappedType = cs.createTypeVariable(loc, 0);
754755
auto paramType = paramTypes[i].getParameterType();
755756
auto paramLabel = paramTypes[i].getLabel();
756757
auto paramInternalLabel = paramTypes[i].getInternalLabel();
757758
adjustedParamTypes.push_back(AnyFunctionType::Param(
758759
wrappedType, paramLabel, ParameterTypeFlags(), paramInternalLabel));
759760
cs.applyPropertyWrapperToParameter(paramType, wrappedType, paramDecl, argLabel,
760-
ConstraintKind::Equal, locator);
761+
ConstraintKind::Equal, loc, loc);
761762
}
762763

763764
return FunctionType::get(adjustedParamTypes, functionType->getResult(),

test/Sema/property_wrapper_parameter.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,28 @@ func takesWrapperClosure<T>(_: ProjectionWrapper<[S<T>]>, closure: (ProjectionWr
223223
func testGenericPropertyWrapper<U>(@ProjectionWrapper wrappers: [S<U>]) {
224224
takesWrapperClosure($wrappers) { $wrapper in }
225225
}
226+
227+
@propertyWrapper
228+
struct Binding<Value> {
229+
var wrappedValue: Value
230+
231+
init(wrappedValue: Value) {
232+
self.wrappedValue = wrappedValue
233+
}
234+
235+
public var projectedValue: Binding<Value> {
236+
return self
237+
}
238+
239+
public init(projectedValue: Binding<Value>) {
240+
self = projectedValue
241+
}
242+
}
243+
244+
struct Widget {
245+
init(@ProjectionWrapper w: Int) {}
246+
}
247+
248+
func buildWidget(_ w: ProjectionWrapper<Int>) -> Widget {
249+
Widget($w: w)
250+
}

0 commit comments

Comments
 (0)