Skip to content

Commit c1849ba

Browse files
committed
[CS] Refactor visitKeyPathExpr a little
Rather than setting `baseTy` for each component, just set it for the last component in the iteration. In addition, remove the `component` variable, which no longer serves a purpose. Finally, cache all the component types at the end of the loop.
1 parent 92a6302 commit c1849ba

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

lib/Sema/CSApply.cpp

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,13 +4181,6 @@ namespace {
41814181
leafTy = keyPathTy->getGenericArgs()[1];
41824182
}
41834183

4184-
// Updates the constraint system with the type of the last resolved
4185-
// component. We do it this way because we sometimes insert new
4186-
// components.
4187-
auto updateCSWithResolvedComponent = [&]() {
4188-
cs.setType(E, resolvedComponents.size() - 1, baseTy);
4189-
};
4190-
41914184
for (unsigned i : indices(E->getComponents())) {
41924185
auto &origComponent = E->getMutableComponents()[i];
41934186

@@ -4248,7 +4241,6 @@ namespace {
42484241
}
42494242
}
42504243

4251-
KeyPathExpr::Component component;
42524244
switch (kind) {
42534245
case KeyPathExpr::Component::Kind::UnresolvedProperty: {
42544246
// If we couldn't resolve the component, leave it alone.
@@ -4257,19 +4249,16 @@ namespace {
42574249
break;
42584250
}
42594251

4260-
component = buildKeyPathPropertyComponent(
4252+
auto component = buildKeyPathPropertyComponent(
42614253
*foundDecl, origComponent.getLoc(), locator);
42624254

4263-
baseTy = component.getComponentType();
42644255
resolvedComponents.push_back(component);
42654256

42664257
if (shouldForceUnwrapResult(foundDecl->choice, locator)) {
4267-
updateCSWithResolvedComponent();
42684258
auto objectTy = getObjectType(baseTy);
42694259
auto loc = origComponent.getLoc();
4270-
component = KeyPathExpr::Component::forOptionalForce(objectTy, loc);
4271-
baseTy = component.getComponentType();
4272-
resolvedComponents.push_back(component);
4260+
resolvedComponents.push_back(
4261+
KeyPathExpr::Component::forOptionalForce(objectTy, loc));
42734262
}
42744263
break;
42754264
}
@@ -4284,20 +4273,16 @@ namespace {
42844273
if (!isDynamicMember)
42854274
subscriptLabels = origComponent.getSubscriptLabels();
42864275

4287-
component = buildKeyPathSubscriptComponent(
4276+
auto component = buildKeyPathSubscriptComponent(
42884277
*foundDecl, origComponent.getLoc(), origComponent.getIndexExpr(),
42894278
subscriptLabels, locator);
4290-
4291-
baseTy = component.getComponentType();
42924279
resolvedComponents.push_back(component);
42934280

42944281
if (shouldForceUnwrapResult(foundDecl->choice, locator)) {
4295-
updateCSWithResolvedComponent();
42964282
auto objectTy = getObjectType(baseTy);
42974283
auto loc = origComponent.getLoc();
4298-
component = KeyPathExpr::Component::forOptionalForce(objectTy, loc);
4299-
baseTy = component.getComponentType();
4300-
resolvedComponents.push_back(component);
4284+
resolvedComponents.push_back(
4285+
KeyPathExpr::Component::forOptionalForce(objectTy, loc));
43014286
}
43024287
break;
43034288
}
@@ -4312,41 +4297,39 @@ namespace {
43124297
assert(objectTy);
43134298

43144299
auto loc = origComponent.getLoc();
4315-
component = KeyPathExpr::Component::forOptionalChain(objectTy, loc);
4316-
4317-
baseTy = component.getComponentType();
4318-
resolvedComponents.push_back(component);
4300+
resolvedComponents.push_back(
4301+
KeyPathExpr::Component::forOptionalChain(objectTy, loc));
43194302
break;
43204303
}
43214304
case KeyPathExpr::Component::Kind::OptionalForce: {
43224305
auto objectTy = getObjectType(baseTy);
43234306
auto loc = origComponent.getLoc();
4324-
component = KeyPathExpr::Component::forOptionalForce(objectTy, loc);
4325-
baseTy = component.getComponentType();
4326-
resolvedComponents.push_back(component);
4307+
resolvedComponents.push_back(
4308+
KeyPathExpr::Component::forOptionalForce(objectTy, loc));
43274309
break;
43284310
}
4329-
case KeyPathExpr::Component::Kind::Invalid:
4330-
component = origComponent;
4311+
case KeyPathExpr::Component::Kind::Invalid: {
4312+
auto component = origComponent;
43314313
component.setComponentType(leafTy);
4332-
4333-
baseTy = component.getComponentType();
43344314
resolvedComponents.push_back(component);
43354315
break;
4336-
case KeyPathExpr::Component::Kind::Identity:
4337-
component = origComponent;
4316+
}
4317+
case KeyPathExpr::Component::Kind::Identity: {
4318+
auto component = origComponent;
43384319
component.setComponentType(baseTy);
43394320
resolvedComponents.push_back(component);
43404321
break;
4322+
}
43414323
case KeyPathExpr::Component::Kind::Property:
43424324
case KeyPathExpr::Component::Kind::Subscript:
43434325
case KeyPathExpr::Component::Kind::OptionalWrap:
43444326
case KeyPathExpr::Component::Kind::TupleElement:
43454327
llvm_unreachable("already resolved");
43464328
}
43474329

4348-
// By now, "baseTy" is the result type of this component.
4349-
updateCSWithResolvedComponent();
4330+
// Update "baseTy" with the result type of the last component.
4331+
assert(!resolvedComponents.empty());
4332+
baseTy = resolvedComponents.back().getComponentType();
43504333
}
43514334

43524335
// Wrap a non-optional result if there was chaining involved.
@@ -4359,10 +4342,12 @@ namespace {
43594342
auto component = KeyPathExpr::Component::forOptionalWrap(leafTy);
43604343
resolvedComponents.push_back(component);
43614344
baseTy = leafTy;
4362-
updateCSWithResolvedComponent();
43634345
}
4346+
4347+
// Set the resolved components, and cache their types.
43644348
E->resolveComponents(cs.getASTContext(), resolvedComponents);
4365-
4349+
cs.cacheExprTypes(E);
4350+
43664351
// See whether there's an equivalent ObjC key path string we can produce
43674352
// for interop purposes.
43684353
if (cs.getASTContext().LangOpts.EnableObjCInterop) {

0 commit comments

Comments
 (0)