Skip to content

Commit f2da113

Browse files
authored
Merge pull request swiftlang#78528 from slavapestov/fix-rdar141961300-6.1
Sema: Fix local property wrappers on constructor [6.1]
2 parents 5c6128f + 7e0e0ec commit f2da113

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

include/swift/Sema/ConstraintSystem.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,13 @@ class Solution {
16131613
/// A map from argument expressions to their applied property wrapper expressions.
16141614
llvm::DenseMap<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
16151615

1616+
ArrayRef<AppliedPropertyWrapper> getAppliedPropertyWrappers(ASTNode anchor) {
1617+
auto found = appliedPropertyWrappers.find(anchor);
1618+
if (found != appliedPropertyWrappers.end())
1619+
return found->second;
1620+
return ArrayRef<AppliedPropertyWrapper>();
1621+
}
1622+
16161623
/// A mapping from the constraint locators for references to various
16171624
/// names (e.g., member references, normal name references, possible
16181625
/// constructions) to the argument lists for the call to that locator.
@@ -5214,7 +5221,8 @@ class ConstraintSystem {
52145221
/// property wrapper type by applying the property wrapper.
52155222
TypeMatchResult applyPropertyWrapperToParameter(
52165223
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
5217-
ConstraintKind matchKind, ConstraintLocatorBuilder locator);
5224+
ConstraintKind matchKind, ConstraintLocator *locator,
5225+
ConstraintLocator *calleeLocator);
52185226

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

lib/Sema/CSApply.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,8 @@ namespace {
11891189
calleeFnTy = calleeFnTy->getResult()->castTo<FunctionType>();
11901190
}
11911191

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

@@ -2334,8 +2334,8 @@ namespace {
23342334
->castTo<FunctionType>();
23352335
auto fullSubscriptTy = openedFullFnType->getResult()
23362336
->castTo<FunctionType>();
2337-
auto &appliedWrappers =
2338-
solution.appliedPropertyWrappers[memberLoc->getAnchor()];
2337+
auto appliedWrappers =
2338+
solution.getAppliedPropertyWrappers(memberLoc->getAnchor());
23392339
args = coerceCallArguments(
23402340
args, fullSubscriptTy, subscriptRef, nullptr,
23412341
locator.withPathElement(ConstraintLocator::ApplyArgument),
@@ -6338,6 +6338,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
63386338
auto *paramDecl = getParameterAt(callee, paramIdx);
63396339
assert(paramDecl);
63406340

6341+
ASSERT(appliedWrapperIndex < appliedPropertyWrappers.size());
63416342
auto appliedWrapper = appliedPropertyWrappers[appliedWrapperIndex++];
63426343
auto wrapperType = solution.simplifyType(appliedWrapper.wrapperType);
63436344
auto initKind = appliedWrapper.initKind;
@@ -8240,7 +8241,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
82408241
// Resolve into a DynamicTypeExpr.
82418242
auto args = apply->getArgs();
82428243

8243-
auto &appliedWrappers = solution.appliedPropertyWrappers[calleeLocator.getAnchor()];
8244+
auto appliedWrappers = solution.getAppliedPropertyWrappers(
8245+
calleeLocator.getAnchor());
82448246
auto fnType = cs.getType(fn)->getAs<FunctionType>();
82458247
args = coerceCallArguments(
82468248
args, fnType, declRef, apply,
@@ -8436,7 +8438,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
84368438
// For function application, convert the argument to the input type of
84378439
// the function.
84388440
if (auto fnType = cs.getType(fn)->getAs<FunctionType>()) {
8439-
auto &appliedWrappers = solution.appliedPropertyWrappers[calleeLocator.getAnchor()];
8441+
auto appliedWrappers = solution.getAppliedPropertyWrappers(
8442+
calleeLocator.getAnchor());
8443+
84408444
args = coerceCallArguments(
84418445
args, fnType, callee, apply,
84428446
locator.withPathElement(ConstraintLocator::ApplyArgument),

lib/Sema/CSGen.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -5057,11 +5057,9 @@ void ConstraintSystem::removePropertyWrapper(Expr *anchor) {
50575057
ConstraintSystem::TypeMatchResult
50585058
ConstraintSystem::applyPropertyWrapperToParameter(
50595059
Type wrapperType, Type paramType, ParamDecl *param, Identifier argLabel,
5060-
ConstraintKind matchKind, ConstraintLocatorBuilder locator) {
5061-
Expr *anchor = getAsExpr(locator.getAnchor());
5062-
if (auto *apply = dyn_cast<ApplyExpr>(anchor)) {
5063-
anchor = apply->getFn();
5064-
}
5060+
ConstraintKind matchKind, ConstraintLocator *locator,
5061+
ConstraintLocator *calleeLocator) {
5062+
Expr *anchor = getAsExpr(calleeLocator->getAnchor());
50655063

50665064
if (argLabel.hasDollarPrefix() && (!param || !param->hasExternalPropertyWrapper())) {
50675065
if (!shouldAttemptFixes())

lib/Sema/CSSimplify.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,9 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
18201820
assert(param);
18211821
if (cs.applyPropertyWrapperToParameter(paramTy, argTy,
18221822
const_cast<ParamDecl *>(param),
1823-
argLabel, subKind, loc)
1823+
argLabel, subKind,
1824+
cs.getConstraintLocator(loc),
1825+
calleeLocator)
18241826
.isFailure()) {
18251827
return cs.getTypeMatchFailure(loc);
18261828
}
@@ -11920,6 +11922,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1192011922
auto result = applyPropertyWrapperToParameter(backingType, param.getParameterType(),
1192111923
paramDecl, paramDecl->getName(),
1192211924
ConstraintKind::Equal,
11925+
getConstraintLocator(closure),
1192311926
getConstraintLocator(closure));
1192411927
if (result.isFailure())
1192511928
return false;

lib/Sema/TypeOfReference.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -741,14 +741,15 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl *
741741
continue;
742742
}
743743

744-
auto *wrappedType = cs.createTypeVariable(cs.getConstraintLocator(locator), 0);
744+
auto *loc = cs.getConstraintLocator(locator);
745+
auto *wrappedType = cs.createTypeVariable(loc, 0);
745746
auto paramType = paramTypes[i].getParameterType();
746747
auto paramLabel = paramTypes[i].getLabel();
747748
auto paramInternalLabel = paramTypes[i].getInternalLabel();
748749
adjustedParamTypes.push_back(AnyFunctionType::Param(
749750
wrappedType, paramLabel, ParameterTypeFlags(), paramInternalLabel));
750751
cs.applyPropertyWrapperToParameter(paramType, wrappedType, paramDecl, argLabel,
751-
ConstraintKind::Equal, locator);
752+
ConstraintKind::Equal, loc, loc);
752753
}
753754

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

test/Sema/property_wrapper_parameter.swift

+25
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,28 @@ func takesWrapperClosure<T>(_: ProjectionWrapper<[S<T>]>, closure: (ProjectionWr
189189
func testGenericPropertyWrapper<U>(@ProjectionWrapper wrappers: [S<U>]) {
190190
takesWrapperClosure($wrappers) { $wrapper in }
191191
}
192+
193+
@propertyWrapper
194+
struct Binding<Value> {
195+
var wrappedValue: Value
196+
197+
init(wrappedValue: Value) {
198+
self.wrappedValue = wrappedValue
199+
}
200+
201+
public var projectedValue: Binding<Value> {
202+
return self
203+
}
204+
205+
public init(projectedValue: Binding<Value>) {
206+
self = projectedValue
207+
}
208+
}
209+
210+
struct Widget {
211+
init(@ProjectionWrapper w: Int) {}
212+
}
213+
214+
func buildWidget(_ w: ProjectionWrapper<Int>) -> Widget {
215+
Widget($w: w)
216+
}

0 commit comments

Comments
 (0)