Skip to content

Commit 5440dce

Browse files
committed
AST: Make ExpandChildTypeRefinementContextsRequest a side effectful request.
While returning the actual child vector from `ExpandChildTypeRefinementContextsRequest` is a nice idea, it is both inefficient (the vector gets copied in and out) and kind of inaccurate, since the vector remains mutable after the initial expansion and may gain additional children as macros are lazily expanded.
1 parent 3d40a8c commit 5440dce

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4852,23 +4852,22 @@ class InitAccessorReferencedVariablesRequest
48524852
/// Expand the children of the given type refinement context.
48534853
class ExpandChildTypeRefinementContextsRequest
48544854
: public SimpleRequest<ExpandChildTypeRefinementContextsRequest,
4855-
std::vector<TypeRefinementContext *>(
4856-
TypeRefinementContext *),
4855+
evaluator::SideEffect(TypeRefinementContext *),
48574856
RequestFlags::SeparatelyCached> {
48584857
public:
48594858
using SimpleRequest::SimpleRequest;
48604859

48614860
private:
48624861
friend SimpleRequest;
48634862

4864-
std::vector<TypeRefinementContext *>
4865-
evaluate(Evaluator &evaluator, TypeRefinementContext *parentTRC) const;
4863+
evaluator::SideEffect evaluate(Evaluator &evaluator,
4864+
TypeRefinementContext *parentTRC) const;
48664865

48674866
public:
48684867
// Separate caching.
48694868
bool isCached() const { return true; }
4870-
std::optional<std::vector<TypeRefinementContext *>> getCachedResult() const;
4871-
void cacheResult(std::vector<TypeRefinementContext *> children) const;
4869+
std::optional<evaluator::SideEffect> getCachedResult() const;
4870+
void cacheResult(evaluator::SideEffect) const;
48724871
};
48734872

48744873
class SerializeAttrGenericSignatureRequest

lib/AST/TypeRefinementContext.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,21 @@ TypeRefinementContext::findMostRefinedSubContext(SourceLoc Loc,
208208
if (SrcRange.isValid() && !Ctx.SourceMgr.containsTokenLoc(SrcRange, Loc))
209209
return nullptr;
210210

211-
auto expandedChildren = evaluateOrDefault(
212-
Ctx.evaluator, ExpandChildTypeRefinementContextsRequest{this}, {});
211+
(void)evaluateOrDefault(Ctx.evaluator,
212+
ExpandChildTypeRefinementContextsRequest{this}, {});
213+
assert(!getNeedsExpansion());
213214

214215
// Do a binary search to find the first child with a source range that
215216
// ends after the given location.
216217
auto iter = std::lower_bound(
217-
expandedChildren.begin(), expandedChildren.end(), Loc,
218+
Children.begin(), Children.end(), Loc,
218219
[&Ctx](TypeRefinementContext *context, SourceLoc loc) {
219220
return Ctx.SourceMgr.isBefore(context->getSourceRange().End, loc);
220221
});
221222

222223
// Check whether the matching child or any of its descendants contain
223224
// the given location.
224-
if (iter != expandedChildren.end()) {
225+
if (iter != Children.end()) {
225226
if (auto found = (*iter)->findMostRefinedSubContext(Loc, Ctx))
226227
return found;
227228
}
@@ -478,18 +479,17 @@ void swift::simple_display(
478479
out << "TRC @" << trc;
479480
}
480481

481-
std::optional<std::vector<TypeRefinementContext *>>
482+
std::optional<evaluator::SideEffect>
482483
ExpandChildTypeRefinementContextsRequest::getCachedResult() const {
483484
auto *TRC = std::get<0>(getStorage());
484485
if (TRC->getNeedsExpansion())
485486
return std::nullopt;
486-
return TRC->Children;
487+
return evaluator::SideEffect();
487488
}
488489

489490
void ExpandChildTypeRefinementContextsRequest::cacheResult(
490-
std::vector<TypeRefinementContext *> children) const {
491+
evaluator::SideEffect sideEffect) const {
491492
auto *TRC = std::get<0>(getStorage());
492-
TRC->Children = children;
493493
TRC->setNeedsExpansion(false);
494494
}
495495

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,8 +1384,7 @@ TypeChecker::getOrBuildTypeRefinementContext(SourceFile *SF) {
13841384
return TRC;
13851385
}
13861386

1387-
std::vector<TypeRefinementContext *>
1388-
ExpandChildTypeRefinementContextsRequest::evaluate(
1387+
evaluator::SideEffect ExpandChildTypeRefinementContextsRequest::evaluate(
13891388
Evaluator &evaluator, TypeRefinementContext *parentTRC) const {
13901389
assert(parentTRC->getNeedsExpansion());
13911390
if (auto decl = parentTRC->getDeclOrNull()) {
@@ -1394,7 +1393,7 @@ ExpandChildTypeRefinementContextsRequest::evaluate(
13941393
builder.prepareDeclForLazyExpansion(decl);
13951394
builder.build(decl);
13961395
}
1397-
return parentTRC->Children;
1396+
return evaluator::SideEffect();
13981397
}
13991398

14001399
AvailabilityRange TypeChecker::overApproximateAvailabilityAtLocation(

0 commit comments

Comments
 (0)