Skip to content

Commit 500acd1

Browse files
committed
Sema: Record potential throw sites in the trail
1 parent 39d0eab commit 500acd1

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ CHANGE(RecordedClosureType)
6464
CHANGE(RecordedContextualInfo)
6565
CHANGE(RecordedTarget)
6666
CHANGE(RecordedCaseLabelItemInfo)
67+
CHANGE(RecordedPotentialThrowSite)
6768

68-
LAST_CHANGE(RecordedCaseLabelItemInfo)
69+
LAST_CHANGE(RecordedPotentialThrowSite)
6970

7071
#undef LOCATOR_CHANGE
7172
#undef EXPR_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class SolverTrail {
133133
ClosureExpr *TheClosure;
134134
DeclContext *TheDeclContext;
135135
CaseLabelItem *TheItem;
136+
CatchNode TheCatchNode;
136137
};
137138

138139
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -224,6 +225,9 @@ class SolverTrail {
224225
/// Create a change that recorded a SyntacticElementTarget.
225226
static Change RecordedCaseLabelItemInfo(CaseLabelItem *item);
226227

228+
/// Create a change that recorded a potential throw site.
229+
static Change RecordedPotentialThrowSite(CatchNode catchNode);
230+
227231
/// Undo this change, reverting the constraint graph to the state it
228232
/// had prior to this change.
229233
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ class Solution {
15851585
/// Maps catch nodes to the set of potential throw sites that will be caught
15861586
/// at that location.
15871587

1588-
/// The set of opened types for a given locator.
1588+
/// Keep track of all of the potential throw sites.
15891589
std::vector<std::pair<CatchNode, PotentialThrowSite>>
15901590
potentialThrowSites;
15911591

@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

2858-
/// The length of \c potentialThrowSites.
2859-
unsigned numPotentialThrowSites;
2860-
28612858
/// The length of \c exprPatterns.
28622859
unsigned numExprPatterns;
28632860

@@ -3425,6 +3422,14 @@ class ConstraintSystem {
34253422
PotentialThrowSite::Kind kind, Type type,
34263423
ConstraintLocatorBuilder locator);
34273424

3425+
/// Used by the above to update potentialThrowSites and record a change
3426+
/// in the trail.
3427+
void recordPotentialThrowSite(CatchNode catchNode,
3428+
PotentialThrowSite site);
3429+
3430+
/// Undo the above change.
3431+
void removePotentialThrowSite(CatchNode catchNode);
3432+
34283433
/// Determine the caught error type for the given catch node.
34293434
Type getCaughtErrorType(CatchNode node);
34303435

lib/Sema/CSSolver.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,11 @@ void ConstraintSystem::applySolution(const Solution &solution) {
399399
setCaseLabelItemInfo(info.first, info.second);
400400
}
401401

402-
potentialThrowSites.insert(potentialThrowSites.end(),
403-
solution.potentialThrowSites.begin(),
404-
solution.potentialThrowSites.end());
402+
auto sites = ArrayRef(solution.potentialThrowSites);
403+
ASSERT(sites.size() >= potentialThrowSites.size());
404+
for (const auto &site : sites.slice(potentialThrowSites.size())) {
405+
potentialThrowSites.push_back(site);
406+
}
405407

406408
for (auto param : solution.isolatedParams) {
407409
isolatedParams.insert(param);
@@ -693,7 +695,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
693695
numTypeVariables = cs.TypeVariables.size();
694696
numFixes = cs.Fixes.size();
695697
numKeyPaths = cs.KeyPaths.size();
696-
numPotentialThrowSites = cs.potentialThrowSites.size();
697698
numExprPatterns = cs.exprPatterns.size();
698699
numIsolatedParams = cs.isolatedParams.size();
699700
numPreconcurrencyClosures = cs.preconcurrencyClosures.size();
@@ -736,9 +737,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
736737
/// Remove any key path expressions.
737738
truncate(cs.KeyPaths, numKeyPaths);
738739

739-
// Remove any potential throw sites.
740-
truncate(cs.potentialThrowSites, numPotentialThrowSites);
741-
742740
// Remove any ExprPattern mappings.
743741
truncate(cs.exprPatterns, numExprPatterns);
744742

lib/Sema/CSTrail.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ SolverTrail::Change::RecordedCaseLabelItemInfo(CaseLabelItem *item) {
311311
return result;
312312
}
313313

314+
SolverTrail::Change
315+
SolverTrail::Change::RecordedPotentialThrowSite(CatchNode catchNode) {
316+
Change result;
317+
result.Kind = ChangeKind::RecordedPotentialThrowSite;
318+
result.TheCatchNode = catchNode;
319+
return result;
320+
}
321+
314322
SyntacticElementTargetKey
315323
SolverTrail::Change::getSyntacticElementTargetKey() const {
316324
ASSERT(Kind == ChangeKind::RecordedTarget);
@@ -477,6 +485,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
477485
case ChangeKind::RecordedCaseLabelItemInfo:
478486
cs.removeCaseLabelItemInfo(TheItem);
479487
break;
488+
489+
case ChangeKind::RecordedPotentialThrowSite:
490+
cs.removePotentialThrowSite(TheCatchNode);
491+
break;
480492
}
481493
}
482494

@@ -686,6 +698,11 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
686698
// FIXME: Print something here
687699
out << "(RecordedCaseLabelItemInfo)\n";
688700
break;
701+
702+
case ChangeKind::RecordedPotentialThrowSite:
703+
// FIXME: Print something here
704+
out << "(RecordedPotentialThrowSite)\n";
705+
break;
689706
}
690707
}
691708

lib/Sema/ConstraintSystem.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,18 @@ bool ConstraintSystem::containsIDEInspectionTarget(
435435
Context.SourceMgr);
436436
}
437437

438+
void ConstraintSystem::recordPotentialThrowSite(
439+
CatchNode catchNode, PotentialThrowSite site) {
440+
potentialThrowSites.push_back({catchNode, site});
441+
if (solverState)
442+
recordChange(SolverTrail::Change::RecordedPotentialThrowSite(catchNode));
443+
}
444+
445+
void ConstraintSystem::removePotentialThrowSite(CatchNode catchNode) {
446+
ASSERT(potentialThrowSites.back().first == catchNode);
447+
potentialThrowSites.pop_back();
448+
}
449+
438450
void ConstraintSystem::recordPotentialThrowSite(
439451
PotentialThrowSite::Kind kind, Type type,
440452
ConstraintLocatorBuilder locator) {
@@ -461,9 +473,8 @@ void ConstraintSystem::recordPotentialThrowSite(
461473
// do..catch statements without an explicit `throws` clause do infer
462474
// thrown types.
463475
if (auto doCatch = catchNode.dyn_cast<DoCatchStmt *>()) {
464-
potentialThrowSites.push_back(
465-
{catchNode,
466-
PotentialThrowSite{kind, type, getConstraintLocator(locator)}});
476+
PotentialThrowSite site{kind, type, getConstraintLocator(locator)};
477+
recordPotentialThrowSite(catchNode, site);
467478
return;
468479
}
469480

@@ -476,9 +487,8 @@ void ConstraintSystem::recordPotentialThrowSite(
476487
if (!closureEffects(closure).isThrowing())
477488
return;
478489

479-
potentialThrowSites.push_back(
480-
{catchNode,
481-
PotentialThrowSite{kind, type, getConstraintLocator(locator)}});
490+
PotentialThrowSite site{kind, type, getConstraintLocator(locator)};
491+
recordPotentialThrowSite(catchNode, site);
482492
}
483493

484494
Type ConstraintSystem::getCaughtErrorType(CatchNode catchNode) {

0 commit comments

Comments
 (0)