Skip to content

Commit ddff9c4

Browse files
committed
[CS] Store argument list mappings on solutions
Roll back argument list mappings in the constraint system at the end of solver scopes, and copy argument list mappings into solutions.
1 parent 632bf41 commit ddff9c4

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,11 @@ class Solution {
11981198
/// A map from argument expressions to their applied property wrapper expressions.
11991199
llvm::MapVector<ASTNode, SmallVector<AppliedPropertyWrapper, 2>> appliedPropertyWrappers;
12001200

1201+
/// A mapping from the constraint locators for references to various
1202+
/// names (e.g., member references, normal name references, possible
1203+
/// constructions) to the argument lists for the call to that locator.
1204+
llvm::MapVector<ConstraintLocator *, ArgumentList *> argumentLists;
1205+
12011206
/// Record a new argument matching choice for given locator that maps a
12021207
/// single argument to a single parameter.
12031208
void recordSingleArgMatchingChoice(ConstraintLocator *locator);
@@ -1332,6 +1337,10 @@ class Solution {
13321337
/// expression type map.
13331338
bool isStaticallyDerivedMetatype(Expr *E) const;
13341339

1340+
/// Retrieve the argument list that is associated with a call at the given
1341+
/// locator.
1342+
ArgumentList *getArgumentList(ConstraintLocator *locator) const;
1343+
13351344
SWIFT_DEBUG_DUMP;
13361345

13371346
/// Dump this solution.
@@ -2409,7 +2418,7 @@ class ConstraintSystem {
24092418
/// A mapping from the constraint locators for references to various
24102419
/// names (e.g., member references, normal name references, possible
24112420
/// constructions) to the argument lists for the call to that locator.
2412-
llvm::DenseMap<ConstraintLocator *, ArgumentList *> ArgumentLists;
2421+
llvm::MapVector<ConstraintLocator *, ArgumentList *> ArgumentLists;
24132422

24142423
public:
24152424
/// A map from argument expressions to their applied property wrapper expressions.
@@ -2899,6 +2908,9 @@ class ConstraintSystem {
28992908
/// The length of \c ImplicitValueConversions.
29002909
unsigned numImplicitValueConversions;
29012910

2911+
/// The length of \c ArgumentLists.
2912+
unsigned numArgumentLists;
2913+
29022914
/// The previous score.
29032915
Score PreviousScore;
29042916

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Expr *FailureDiagnostic::findParentExpr(const Expr *subExpr) const {
112112

113113
ArgumentList *
114114
FailureDiagnostic::getArgumentListFor(ConstraintLocator *locator) const {
115-
return getConstraintSystem().getArgumentList(locator);
115+
return S.getArgumentList(locator);
116116
}
117117

118118
Expr *FailureDiagnostic::getBaseExprFor(const Expr *anchor) const {

lib/Sema/CSSolver.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ Solution ConstraintSystem::finalize() {
199199
solution.ImplicitValueConversions.push_back(valueConversion);
200200
}
201201

202+
// Remember argument lists.
203+
for (const auto &argListMapping : ArgumentLists) {
204+
solution.argumentLists.insert(argListMapping);
205+
}
206+
202207
return solution;
203208
}
204209

@@ -297,6 +302,11 @@ void ConstraintSystem::applySolution(const Solution &solution) {
297302
ImplicitValueConversions.push_back(valueConversion);
298303
}
299304

305+
// Register the argument lists.
306+
for (auto &argListMapping : solution.argumentLists) {
307+
ArgumentLists.insert(argListMapping);
308+
}
309+
300310
// Register any fixes produced along this path.
301311
Fixes.append(solution.Fixes.begin(), solution.Fixes.end());
302312
}
@@ -510,6 +520,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
510520
numSolutionApplicationTargets = cs.solutionApplicationTargets.size();
511521
numCaseLabelItems = cs.caseLabelItems.size();
512522
numImplicitValueConversions = cs.ImplicitValueConversions.size();
523+
numArgumentLists = cs.ArgumentLists.size();
513524

514525
PreviousScore = cs.CurrentScore;
515526

@@ -619,6 +630,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
619630
// Remove any implicit value conversions.
620631
truncate(cs.ImplicitValueConversions, numImplicitValueConversions);
621632

633+
// Remove any argument lists no longer in scope.
634+
truncate(cs.ArgumentLists, numArgumentLists);
635+
622636
// Reset the previous score.
623637
cs.CurrentScore = PreviousScore;
624638

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,10 +3713,11 @@ static bool diagnoseAmbiguity(
37133713
/*isApplication=*/false, decl->getDescriptiveKind(),
37143714
name.isSpecial(), name.getBaseName());
37153715
} else {
3716-
bool isApplication =
3717-
llvm::any_of(cs.ArgumentLists, [&](const auto &pair) {
3716+
bool isApplication = llvm::any_of(solutions, [&](const auto &S) {
3717+
return llvm::any_of(S.argumentLists, [&](const auto &pair) {
37183718
return pair.first->getAnchor() == commonAnchor;
37193719
});
3720+
});
37203721

37213722
DE.diagnose(getLoc(commonAnchor),
37223723
diag::no_overloads_match_exactly_in_call, isApplication,
@@ -3757,7 +3758,7 @@ static bool diagnoseAmbiguity(
37573758

37583759
if (fn->getNumParams() == 1) {
37593760
auto *argList =
3760-
cs.getArgumentList(solution.Fixes.front()->getLocator());
3761+
solution.getArgumentList(solution.Fixes.front()->getLocator());
37613762
assert(argList);
37623763

37633764
const auto &param = fn->getParams()[0];
@@ -4671,6 +4672,18 @@ void ConstraintSystem::associateArgumentList(ConstraintLocator *locator,
46714672
(void)inserted;
46724673
}
46734674

4675+
ArgumentList *Solution::getArgumentList(ConstraintLocator *locator) const {
4676+
if (!locator)
4677+
return nullptr;
4678+
4679+
if (auto *infoLocator = constraintSystem->getArgumentInfoLocator(locator)) {
4680+
auto known = argumentLists.find(infoLocator);
4681+
if (known != argumentLists.end())
4682+
return known->second;
4683+
}
4684+
return nullptr;
4685+
}
4686+
46744687
/// Given an apply expr, returns true if it is expected to have a direct callee
46754688
/// overload, resolvable using `getChoiceFor`. Otherwise, returns false.
46764689
static bool shouldHaveDirectCalleeOverload(const CallExpr *callExpr) {
@@ -4728,12 +4741,10 @@ Type Solution::resolveInterfaceType(Type type) const {
47284741

47294742
Optional<FunctionArgApplyInfo>
47304743
Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
4731-
auto &cs = getConstraintSystem();
4732-
47334744
// It's only valid to use `&` in argument positions, but we need
47344745
// to figure out exactly where it was used.
47354746
if (auto *argExpr = getAsExpr<InOutExpr>(locator->getAnchor())) {
4736-
auto *argLoc = cs.getArgumentLocator(argExpr);
4747+
auto *argLoc = getConstraintSystem().getArgumentLocator(argExpr);
47374748
assert(argLoc && "Incorrect use of `inout` expression");
47384749
locator = argLoc;
47394750
}
@@ -4766,7 +4777,7 @@ Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
47664777
if (!argExpr)
47674778
return None;
47684779

4769-
auto *argList = cs.getArgumentList(argLocator);
4780+
auto *argList = getArgumentList(argLocator);
47704781
if (!argList)
47714782
return None;
47724783

0 commit comments

Comments
 (0)