Skip to content

Commit 99555dc

Browse files
committed
[Constraint system] Add ConstraintSystem::getExprDepth() and use it for cleanup.
Rather than passing around or create depth maps at a few places in the constraint solver, introduce getExprDepth() and use it consistently.
1 parent a30f804 commit 99555dc

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,7 @@ static Type getUnlabeledType(Type type, ASTContext &ctx) {
710710

711711
SolutionCompareResult ConstraintSystem::compareSolutions(
712712
ConstraintSystem &cs, ArrayRef<Solution> solutions,
713-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
714-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights) {
713+
const SolutionDiff &diff, unsigned idx1, unsigned idx2) {
715714
if (cs.getASTContext().LangOpts.DebugConstraintSolver) {
716715
auto &log = cs.getASTContext().TypeCheckerDebug->getStream();
717716
log.indent(cs.solverState->depth * 2)
@@ -743,9 +742,9 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
743742

744743
auto getWeight = [&](ConstraintLocator *locator) -> unsigned {
745744
if (auto *anchor = locator->getAnchor()) {
746-
auto weight = weights.find(anchor);
747-
if (weight != weights.end())
748-
return weight->getSecond().first + 1;
745+
auto weight = cs.getExprDepth(anchor);
746+
if (weight)
747+
return *weight + 1;
749748
}
750749

751750
return 1;
@@ -1197,7 +1196,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
11971196
SmallVector<bool, 16> losers(viable.size(), false);
11981197
unsigned bestIdx = 0;
11991198
for (unsigned i = 1, n = viable.size(); i != n; ++i) {
1200-
switch (compareSolutions(*this, viable, diff, i, bestIdx, ExprWeights)) {
1199+
switch (compareSolutions(*this, viable, diff, i, bestIdx)) {
12011200
case SolutionCompareResult::Identical:
12021201
// FIXME: Might want to warn about this in debug builds, so we can
12031202
// find a way to eliminate the redundancy in the search space.
@@ -1221,7 +1220,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
12211220
if (i == bestIdx)
12221221
continue;
12231222

1224-
switch (compareSolutions(*this, viable, diff, bestIdx, i, ExprWeights)) {
1223+
switch (compareSolutions(*this, viable, diff, bestIdx, i)) {
12251224
case SolutionCompareResult::Identical:
12261225
// FIXME: Might want to warn about this in debug builds, so we can
12271226
// find a way to eliminate the redundancy in the search space.
@@ -1273,7 +1272,7 @@ ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
12731272
if (losers[j])
12741273
continue;
12751274

1276-
switch (compareSolutions(*this, viable, diff, i, j, ExprWeights)) {
1275+
switch (compareSolutions(*this, viable, diff, i, j)) {
12771276
case SolutionCompareResult::Identical:
12781277
// FIXME: Dub one of these the loser arbitrarily?
12791278
break;

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,7 +2695,6 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
26952695
// Heuristically, all other things being equal, we should complain about the
26962696
// ambiguous expression that (1) has the most overloads, (2) is deepest, or
26972697
// (3) comes earliest in the expression.
2698-
auto depthMap = expr->getDepthMap();
26992698
auto indexMap = expr->getPreorderIndexMap();
27002699

27012700
for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
@@ -2711,10 +2710,10 @@ bool ConstraintSystem::diagnoseAmbiguity(Expr *expr,
27112710
continue;
27122711
unsigned index = it->second;
27132712

2714-
auto e = depthMap.find(anchor);
2715-
if (e == depthMap.end())
2713+
auto optDepth = getExprDepth(anchor);
2714+
if (!optDepth)
27162715
continue;
2717-
unsigned depth = e->second.first;
2716+
unsigned depth = *optDepth;
27182717

27192718
// If we don't have a name to hang on to, it'll be hard to diagnose this
27202719
// overload.

lib/Sema/ConstraintSystem.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,18 @@ class ConstraintSystem {
19911991
return nullptr;
19921992
}
19931993

1994+
/// Retrieve the depth of the given expression.
1995+
Optional<unsigned> getExprDepth(Expr *expr) const {
1996+
auto e = ExprWeights.find(expr);
1997+
if (e != ExprWeights.end())
1998+
return e->second.first;
1999+
2000+
if (baseCS && baseCS != this)
2001+
return baseCS->getExprDepth(expr);
2002+
2003+
return None;
2004+
}
2005+
19942006
/// Returns a locator describing the callee for the anchor of a given locator.
19952007
///
19962008
/// - For an unresolved dot/member anchor, this will be a locator describing
@@ -3659,8 +3671,7 @@ class ConstraintSystem {
36593671
/// \param idx2 The index of the second solution.
36603672
static SolutionCompareResult
36613673
compareSolutions(ConstraintSystem &cs, ArrayRef<Solution> solutions,
3662-
const SolutionDiff &diff, unsigned idx1, unsigned idx2,
3663-
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &weights);
3674+
const SolutionDiff &diff, unsigned idx1, unsigned idx2);
36643675

36653676
public:
36663677
/// Increase the score of the given kind for the current (partial) solution

0 commit comments

Comments
 (0)