Skip to content

Commit 852169a

Browse files
committed
[ConstraintSystem] Split unviable lookup result storage into candidates & reasons
That makes it easy to process unviable choices the same way as viable ones and request rejection reasons only when necessary.
1 parent ce2fe02 commit 852169a

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -791,15 +791,19 @@ void FailureDiagnosis::diagnoseUnviableLookupResults(
791791
// Otherwise, we have at least one (and potentially many) viable candidates
792792
// sort them out. If all of the candidates have the same problem (commonly
793793
// because there is exactly one candidate!) diagnose this.
794-
bool sameProblem = true;
795-
auto firstProblem = result.UnviableCandidates[0].second;
794+
auto firstProblem = result.UnviableReasons[0];
795+
bool sameProblem = llvm::all_of(
796+
result.UnviableReasons,
797+
[&firstProblem](const MemberLookupResult::UnviableReason &problem) {
798+
return problem == firstProblem;
799+
});
800+
796801
ValueDecl *member = nullptr;
797802
for (auto cand : result.UnviableCandidates) {
798803
if (member == nullptr)
799-
member = cand.first.getDecl();
800-
sameProblem &= cand.second == firstProblem;
804+
member = cand.getDecl();
801805
}
802-
806+
803807
auto instanceTy = baseObjTy;
804808
if (auto *MTT = instanceTy->getAs<AnyMetatypeType>())
805809
instanceTy = MTT->getInstanceType();
@@ -842,7 +846,7 @@ void FailureDiagnosis::diagnoseUnviableLookupResults(
842846
}
843847

844848
case MemberLookupResult::UR_Inaccessible: {
845-
auto decl = result.UnviableCandidates[0].first.getDecl();
849+
auto decl = result.UnviableCandidates[0].getDecl();
846850
// FIXME: What if the unviable candidates have different levels of access?
847851
//
848852
// If we found an inaccessible member of a protocol extension, it might
@@ -853,8 +857,8 @@ void FailureDiagnosis::diagnoseUnviableLookupResults(
853857
diagnose(nameLoc, diag::candidate_inaccessible, decl->getBaseName(),
854858
decl->getFormalAccessScope().accessLevelForDiagnostics());
855859
for (auto cand : result.UnviableCandidates)
856-
diagnose(cand.first.getDecl(), diag::decl_declared_here, memberName);
857-
860+
diagnose(cand.getDecl(), diag::decl_declared_here, memberName);
861+
858862
return;
859863
}
860864
}
@@ -4206,7 +4210,7 @@ bool FailureDiagnosis::diagnoseMethodAttributeFailures(
42064210

42074211
SmallVector<OverloadChoice, 2> choices;
42084212
for (auto &unviable : results.UnviableCandidates)
4209-
choices.push_back(OverloadChoice(baseType, unviable.first.getDecl(),
4213+
choices.push_back(OverloadChoice(baseType, unviable.getDecl(),
42104214
UDE->getFunctionRefKind()));
42114215

42124216
CalleeCandidateInfo unviableCandidates(baseType, choices, hasTrailingClosure,

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3719,7 +3719,7 @@ swift::resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name) {
37193719

37203720
// Keep track of all the unviable members.
37213721
for (auto Can : LookupResult.UnviableCandidates)
3722-
Result.Impl.AllDecls.push_back(Can.first.getDecl());
3722+
Result.Impl.AllDecls.push_back(Can.getDecl());
37233723

37243724
// Keep track of the start of viable choices.
37253725
Result.Impl.ViableStartIdx = Result.Impl.AllDecls.size();

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,10 +3933,10 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
39333933
result.addViable(
39343934
OverloadChoice::getDynamicMemberLookup(baseTy, decl, name));
39353935
}
3936-
for (auto candidate : subscripts.UnviableCandidates) {
3937-
auto decl = candidate.first.getDecl();
3936+
for (auto index : indices(subscripts.UnviableCandidates)) {
3937+
auto decl = subscripts.UnviableCandidates[index].getDecl();
39383938
auto choice = OverloadChoice::getDynamicMemberLookup(baseTy, decl,name);
3939-
result.addUnviable(choice, candidate.second);
3939+
result.addUnviable(choice, subscripts.UnviableReasons[index]);
39403940
}
39413941
}
39423942
}
@@ -4232,21 +4232,15 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
42324232
}
42334233

42344234
if (!result.UnviableCandidates.empty()) {
4235-
SmallVector<OverloadChoice, 8> choices;
4236-
llvm::transform(
4237-
result.UnviableCandidates, std::back_inserter(choices),
4238-
[](const std::pair<OverloadChoice, MemberLookupResult::UnviableReason>
4239-
&candidate) { return candidate.first; });
4240-
42414235
// Generate constraints for unvailable choices if they have a fix,
42424236
// and disable them by default, they'd get picked up in the "salvage" mode.
4243-
generateConstraints(candidates, memberTy, choices, useDC, locator,
4244-
/*favoredChoice=*/None, /*requiresFix=*/true,
4245-
[&](unsigned idx, const OverloadChoice &choice) {
4246-
return fixMemberRef(
4247-
*this, baseTy, member, choice, locator,
4248-
result.UnviableCandidates[idx].second);
4249-
});
4237+
generateConstraints(
4238+
candidates, memberTy, result.UnviableCandidates, useDC, locator,
4239+
/*favoredChoice=*/None, /*requiresFix=*/true,
4240+
[&](unsigned idx, const OverloadChoice &choice) {
4241+
return fixMemberRef(*this, baseTy, member, choice, locator,
4242+
result.UnviableReasons[idx]);
4243+
});
42504244
}
42514245

42524246
if (!candidates.empty()) {

lib/Sema/ConstraintSystem.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,12 @@ struct MemberLookupResult {
873873
/// The member is inaccessible (e.g. a private member in another file).
874874
UR_Inaccessible,
875875
};
876-
877-
/// This is a list of considered (but rejected) candidates, along with a
878-
/// reason for their rejection.
879-
SmallVector<std::pair<OverloadChoice, UnviableReason>, 4> UnviableCandidates;
880876

877+
/// This is a list of considered (but rejected) candidates, along with a
878+
/// reason for their rejection. Split into separate collections to make
879+
/// it easier to use in conjunction with viable candidates.
880+
SmallVector<OverloadChoice, 4> UnviableCandidates;
881+
SmallVector<UnviableReason, 4> UnviableReasons;
881882

882883
/// Mark this as being an already-diagnosed error and return itself.
883884
MemberLookupResult &markErrorAlreadyDiagnosed() {
@@ -890,7 +891,8 @@ struct MemberLookupResult {
890891
}
891892

892893
void addUnviable(OverloadChoice candidate, UnviableReason reason) {
893-
UnviableCandidates.push_back({candidate, reason});
894+
UnviableCandidates.push_back(candidate);
895+
UnviableReasons.push_back(reason);
894896
}
895897

896898
Optional<unsigned> getFavoredIndex() const {

0 commit comments

Comments
 (0)