Skip to content

Commit 3ab8710

Browse files
committed
[Diagnostics] Cleanup use of constraint system by ConstraintFix/FailureDiagnostic
1 parent 52cf601 commit 3ab8710

File tree

6 files changed

+46
-34
lines changed

6 files changed

+46
-34
lines changed

lib/Sema/CSApply.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ ConstraintLocator *Solution::getCalleeLocator(ConstraintLocator *locator,
165165
});
166166
}
167167

168+
ConstraintLocator *
169+
Solution::getConstraintLocator(Expr *anchor,
170+
ArrayRef<LocatorPathElt> path) const {
171+
auto &cs = getConstraintSystem();
172+
return cs.getConstraintLocator(anchor, path);
173+
}
174+
168175
/// Return the implicit access kind for a MemberRefExpr with the
169176
/// specified base and member in the specified DeclContext.
170177
static AccessSemantics
@@ -7702,6 +7709,15 @@ ProtocolConformanceRef Solution::resolveConformance(
77027709
return ProtocolConformanceRef::forInvalid();
77037710
}
77047711

7712+
Type Solution::getType(TypedNode node) const {
7713+
auto result = nodeTypes.find(node);
7714+
if (result != nodeTypes.end())
7715+
return result->second;
7716+
7717+
auto &cs = getConstraintSystem();
7718+
return cs.getType(node);
7719+
}
7720+
77057721
void Solution::setExprTypes(Expr *expr) const {
77067722
if (!expr)
77077723
return;

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Type FailureDiagnostic::getType(Expr *expr, bool wantRValue) const {
8080
}
8181

8282
Type FailureDiagnostic::getType(const TypeLoc &loc, bool wantRValue) const {
83-
return resolveType(S.getType(loc), /*reconstituteSugar=*/false, wantRValue);
83+
return resolveType(S.getType(&loc), /*reconstituteSugar=*/false, wantRValue);
8484
}
8585

8686
template <typename... ArgTypes>
@@ -132,8 +132,7 @@ Expr *FailureDiagnostic::getBaseExprFor(Expr *anchor) const {
132132

133133
Optional<SelectedOverload>
134134
FailureDiagnostic::getChoiceFor(ConstraintLocator *locator) const {
135-
auto &cs = getConstraintSystem();
136-
return getOverloadChoiceIfAvailable(cs.getCalleeLocator(locator));
135+
return getOverloadChoiceIfAvailable(S.getCalleeLocator(locator));
137136
}
138137

139138
Type FailureDiagnostic::restoreGenericParameters(
@@ -1421,8 +1420,8 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() {
14211420

14221421
AssignmentFailure::AssignmentFailure(Expr *destExpr, const Solution &solution,
14231422
SourceLoc diagnosticLoc)
1424-
: FailureDiagnostic(solution, cs.getConstraintLocator(destExpr)),
1425-
DestExpr(destExpr), Loc(diagnosticLoc),
1423+
: FailureDiagnostic(solution, destExpr), DestExpr(destExpr),
1424+
Loc(diagnosticLoc),
14261425
DeclDiagnostic(findDeclDiagonstic(getASTContext(), destExpr)),
14271426
TypeDiagnostic(diag::assignment_lhs_not_lvalue) {}
14281427

@@ -3782,7 +3781,6 @@ bool ImplicitInitOnNonConstMetatypeFailure::diagnoseAsError() {
37823781
}
37833782

37843783
bool MissingArgumentsFailure::diagnoseAsError() {
3785-
auto &cs = getConstraintSystem();
37863784
auto *locator = getLocator();
37873785

37883786
if (!(locator->isLastElement<LocatorPathElt::ApplyArgToParam>() ||
@@ -6021,8 +6019,6 @@ bool AssignmentTypeMismatchFailure::diagnoseAsNote() {
60216019

60226020
bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() {
60236021
auto *anchor = getAnchor();
6024-
auto &cs = getConstraintSystem();
6025-
60266022
// Member reference could be wrapped into a number of parens
60276023
// e.g. `((.foo))`.
60286024
auto *parentExpr = findParentExpr(anchor);

lib/Sema/CSDiagnostics.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class FailureDiagnostic {
5555
std::tie(Anchor, HasComplexLocator) = computeAnchor();
5656
}
5757

58+
FailureDiagnostic(const Solution &solution, Expr *anchor)
59+
: FailureDiagnostic(solution, solution.getConstraintLocator(anchor)) {}
60+
5861
virtual ~FailureDiagnostic();
5962

6063
/// Try to diagnose a problem given affected expression,
@@ -155,8 +158,7 @@ class FailureDiagnostic {
155158
}
156159

157160
ValueDecl *getResolvedMemberRef(UnresolvedDotExpr *member) const {
158-
auto &cs = getConstraintSystem();
159-
auto locator = cs.getConstraintLocator(member, ConstraintLocator::Member);
161+
auto *locator = getConstraintLocator(member, ConstraintLocator::Member);
160162
if (auto overload = getOverloadChoiceIfAvailable(locator))
161163
return overload->choice.getDeclOrNull();
162164
return nullptr;
@@ -169,13 +171,17 @@ class FailureDiagnostic {
169171
return S.getOverloadChoiceIfAvailable(locator);
170172
}
171173

172-
/// Retrive the constraint locator for the given anchor and
173-
/// path, uniqued and automatically calculate the summary flags
174174
ConstraintLocator *
175175
getConstraintLocator(Expr *anchor,
176-
ArrayRef<ConstraintLocator::PathElement> path) {
177-
auto &cs = getConstraintSystem();
178-
return cs.getConstraintLocator(anchor, path);
176+
ConstraintLocator::PathElement element) const {
177+
return S.getConstraintLocator(anchor, {element});
178+
}
179+
180+
/// Retrive the constraint locator for the given anchor and
181+
/// path, uniqued and automatically calculate the summary flags
182+
ConstraintLocator *getConstraintLocator(
183+
Expr *anchor, ArrayRef<ConstraintLocator::PathElement> path = {}) const {
184+
return S.getConstraintLocator(anchor, path);
179185
}
180186

181187
Optional<FunctionArgApplyInfo>
@@ -503,10 +509,9 @@ class TrailingClosureAmbiguityFailure final : public FailureDiagnostic {
503509
ArrayRef<OverloadChoice> Choices;
504510

505511
public:
506-
TrailingClosureAmbiguityFailure(const Solution &solution, Expr *anchor,
512+
TrailingClosureAmbiguityFailure(ArrayRef<Solution> solutions, Expr *anchor,
507513
ArrayRef<OverloadChoice> choices)
508-
: FailureDiagnostic(solution, cs.getConstraintLocator(anchor)),
509-
Choices(choices) {}
514+
: FailureDiagnostic(solutions.front(), anchor), Choices(choices) {}
510515

511516
bool diagnoseAsError() override { return false; }
512517

@@ -529,9 +534,9 @@ class AssignmentFailure final : public FailureDiagnostic {
529534
AssignmentFailure(Expr *destExpr, const Solution &solution,
530535
SourceLoc diagnosticLoc, Diag<StringRef> declDiag,
531536
Diag<Type> typeDiag)
532-
: FailureDiagnostic(solution, cs.getConstraintLocator(destExpr)),
533-
DestExpr(destExpr), Loc(diagnosticLoc), DeclDiagnostic(declDiag),
534-
TypeDiagnostic(typeDiag) {}
537+
: FailureDiagnostic(solution, destExpr), DestExpr(destExpr),
538+
Loc(diagnosticLoc), DeclDiagnostic(declDiag), TypeDiagnostic(typeDiag) {
539+
}
535540

536541
bool diagnoseAsError() override;
537542

lib/Sema/CSFix.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ bool MissingConformance::diagnose(const Solution &solution, bool asNote) const {
188188
auto *locator = getLocator();
189189

190190
if (IsContextual) {
191+
auto &cs = solution.getConstraintSystem();
191192
auto context = cs.getContextualTypePurpose(locator->getAnchor());
192193
MissingContextualConformanceFailure failure(
193194
solution, context, NonConformingType, ProtocolType, locator);

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,8 +2982,6 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
29822982
// If we didn't find an ambiguous overload, diagnose the common fixes.
29832983
if (ambiguousOverload == solutionDiff.overloads.end()) {
29842984
bool diagnosed = false;
2985-
ConstraintSystem::SolverScope scope(*this);
2986-
applySolution(solutions.front());
29872985
for (auto fixes: aggregatedFixes) {
29882986
// A common fix must appear in all solutions
29892987
if (fixes.second.size() < solutions.size()) continue;
@@ -3042,11 +3040,8 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
30423040
continue;
30433041

30443042
if (solution.Fixes.size() == 1) {
3045-
// Create scope so each applied solution is rolled back.
3046-
ConstraintSystem::SolverScope scope(*this);
3047-
applySolution(solution);
3048-
// All of the solutions supposed to produce a "candidate" note.
3049-
diagnosed &= solution.Fixes.front()->diagnose(/*asNote*/ true);
3043+
diagnosed &=
3044+
solution.Fixes.front()->diagnose(solution, /*asNote*/ true);
30503045
} else if (llvm::all_of(solution.Fixes,
30513046
[&](ConstraintFix *fix) {
30523047
return fix->getLocator()
@@ -3217,7 +3212,7 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
32173212
: diag::ambiguous_decl_ref,
32183213
name);
32193214

3220-
TrailingClosureAmbiguityFailure failure(*this, anchor,
3215+
TrailingClosureAmbiguityFailure failure(solutions, anchor,
32213216
overload.choices);
32223217
if (failure.diagnoseAsNote())
32233218
return true;

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,13 @@ class Solution {
975975
ConstraintLocator *getCalleeLocator(ConstraintLocator *locator,
976976
bool lookThroughApply = true) const;
977977

978+
ConstraintLocator *
979+
getConstraintLocator(Expr *anchor, ArrayRef<LocatorPathElt> path = {}) const;
980+
978981
void setExprTypes(Expr *expr) const;
979982

980983
/// Retrieve the type of the given node, as recorded in this solution.
981-
Type getType(TypedNode node) const {
982-
auto known = nodeTypes.find(node);
983-
assert(known != nodeTypes.end());
984-
return known->second;
985-
}
984+
Type getType(TypedNode node) const;
986985

987986
/// Resolve type variables present in the raw type, using generic parameter
988987
/// types where possible.

0 commit comments

Comments
 (0)