Skip to content

Commit 3e7a7a2

Browse files
committed
[CSApply] Extract keypath property resolution logic
1 parent 84167ed commit 3e7a7a2

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

lib/Sema/CSApply.cpp

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,59 +4237,8 @@ namespace {
42374237
break;
42384238
}
42394239

4240-
if (foundDecl->choice.isDecl()) {
4241-
auto property = foundDecl->choice.getDecl();
4242-
4243-
// Key paths can only refer to properties currently.
4244-
if (!isa<VarDecl>(property)) {
4245-
cs.TC.diagnose(origComponent.getLoc(),
4246-
diag::expr_keypath_not_property,
4247-
property->getDescriptiveKind(),
4248-
property->getFullName());
4249-
} else {
4250-
// Key paths don't work with mutating-get properties.
4251-
auto varDecl = cast<VarDecl>(property);
4252-
if (varDecl->isGetterMutating()) {
4253-
cs.TC.diagnose(origComponent.getLoc(),
4254-
diag::expr_keypath_mutating_getter,
4255-
property->getFullName());
4256-
}
4257-
4258-
// Key paths don't currently support static members.
4259-
if (varDecl->isStatic()) {
4260-
cs.TC.diagnose(origComponent.getLoc(),
4261-
diag::expr_keypath_static_member,
4262-
property->getFullName());
4263-
}
4264-
}
4265-
4266-
cs.TC.requestMemberLayout(property);
4267-
4268-
auto dc = property->getInnermostDeclContext();
4269-
4270-
// Compute substitutions to refer to the member.
4271-
SubstitutionMap subs =
4272-
solution.computeSubstitutions(dc->getGenericSignatureOfContext(),
4273-
locator);
4274-
4275-
auto resolvedTy = foundDecl->openedType;
4276-
resolvedTy = simplifyType(resolvedTy);
4277-
4278-
auto ref = ConcreteDeclRef(property, subs);
4279-
4280-
component = KeyPathExpr::Component::forProperty(ref,
4281-
resolvedTy,
4282-
origComponent.getLoc());
4283-
} else {
4284-
auto fieldIndex = foundDecl->choice.getTupleIndex();
4285-
4286-
auto resolvedTy = foundDecl->openedType;
4287-
resolvedTy = simplifyType(resolvedTy);
4288-
4289-
component = KeyPathExpr::Component::forTupleElement(fieldIndex,
4290-
resolvedTy,
4291-
origComponent.getLoc());
4292-
}
4240+
component = buildKeyPathPropertyComponent(
4241+
*foundDecl, origComponent.getLoc(), locator);
42934242

42944243
baseTy = component.getComponentType();
42954244
resolvedComponents.push_back(component);
@@ -4476,6 +4425,58 @@ namespace {
44764425
return E;
44774426
}
44784427

4428+
KeyPathExpr::Component
4429+
buildKeyPathPropertyComponent(const SelectedOverload &overload,
4430+
SourceLoc componentLoc,
4431+
ConstraintLocator *locator) {
4432+
if (overload.choice.isDecl()) {
4433+
auto property = overload.choice.getDecl();
4434+
4435+
// Key paths can only refer to properties currently.
4436+
if (!isa<VarDecl>(property)) {
4437+
cs.TC.diagnose(componentLoc, diag::expr_keypath_not_property,
4438+
property->getDescriptiveKind(),
4439+
property->getFullName());
4440+
} else {
4441+
// Key paths don't work with mutating-get properties.
4442+
auto varDecl = cast<VarDecl>(property);
4443+
if (varDecl->isGetterMutating()) {
4444+
cs.TC.diagnose(componentLoc, diag::expr_keypath_mutating_getter,
4445+
property->getFullName());
4446+
}
4447+
4448+
// Key paths don't currently support static members.
4449+
if (varDecl->isStatic()) {
4450+
cs.TC.diagnose(componentLoc, diag::expr_keypath_static_member,
4451+
property->getFullName());
4452+
}
4453+
}
4454+
4455+
cs.TC.requestMemberLayout(property);
4456+
4457+
auto dc = property->getInnermostDeclContext();
4458+
4459+
// Compute substitutions to refer to the member.
4460+
SubstitutionMap subs = solution.computeSubstitutions(
4461+
dc->getGenericSignatureOfContext(), locator);
4462+
4463+
auto resolvedTy = overload.openedType;
4464+
resolvedTy = simplifyType(resolvedTy);
4465+
4466+
auto ref = ConcreteDeclRef(property, subs);
4467+
4468+
return KeyPathExpr::Component::forProperty(ref, resolvedTy,
4469+
componentLoc);
4470+
}
4471+
4472+
auto fieldIndex = overload.choice.getTupleIndex();
4473+
auto resolvedTy = overload.openedType;
4474+
resolvedTy = simplifyType(resolvedTy);
4475+
4476+
return KeyPathExpr::Component::forTupleElement(fieldIndex, resolvedTy,
4477+
componentLoc);
4478+
}
4479+
44794480
Expr *visitKeyPathDotExpr(KeyPathDotExpr *E) {
44804481
llvm_unreachable("found KeyPathDotExpr in CSApply");
44814482
}

0 commit comments

Comments
 (0)