Skip to content

Commit bbe0c09

Browse files
committed
[ConstraintSystem] Avoid member lookup if base type is a "hole"
Member lookup on a "hole" is not going to produce any results and it makes sense to detect early and short-circuit the constraint.
1 parent 805a5e9 commit bbe0c09

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6060,20 +6060,33 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
60606060

60616061
auto locator = getConstraintLocator(locatorB);
60626062

6063-
// If this is an unresolved member ref e.g. `.foo` and its contextual base
6064-
// type has been determined to be a "hole", let's mark the resulting member
6065-
// type as a potential hole and continue solving.
6066-
if (shouldAttemptFixes() && kind == ConstraintKind::UnresolvedValueMember &&
6067-
baseObjTy->getMetatypeInstanceType()->isHole()) {
6068-
auto *fix =
6069-
SpecifyBaseTypeForContextualMember::create(*this, member, locator);
6070-
if (recordFix(fix))
6071-
return SolutionKind::Error;
6063+
// If the base type of this member lookup is a "hole" there is no
6064+
// reason to perform a lookup because it wouldn't return any results.
6065+
if (shouldAttemptFixes()) {
6066+
auto markMemberTypeAsPotentialHole = [&](Type memberTy) {
6067+
if (auto *typeVar = memberTy->getAs<TypeVariableType>())
6068+
recordPotentialHole(typeVar);
6069+
};
60726070

6073-
if (auto *typeVar = memberTy->getAs<TypeVariableType>())
6074-
recordPotentialHole(typeVar);
6071+
// If this is an unresolved member ref e.g. `.foo` and its contextual base
6072+
// type has been determined to be a "hole", let's mark the resulting member
6073+
// type as a potential hole and continue solving.
6074+
if (kind == ConstraintKind::UnresolvedValueMember &&
6075+
baseObjTy->getMetatypeInstanceType()->isHole()) {
6076+
auto *fix =
6077+
SpecifyBaseTypeForContextualMember::create(*this, member, locator);
6078+
if (recordFix(fix))
6079+
return SolutionKind::Error;
60756080

6076-
return SolutionKind::Solved;
6081+
markMemberTypeAsPotentialHole(memberTy);
6082+
return SolutionKind::Solved;
6083+
} else if (kind == ConstraintKind::ValueMember && baseObjTy->isHole()) {
6084+
// If base type is a "hole" there is no reason to record any
6085+
// more "member not found" fixes for chained member references.
6086+
increaseScore(SK_Fix);
6087+
markMemberTypeAsPotentialHole(memberTy);
6088+
return SolutionKind::Solved;
6089+
}
60776090
}
60786091

60796092
MemberLookupResult result =
@@ -6345,15 +6358,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyMemberConstraint(
63456358
// fake its presence based on use, that makes it possible to diagnose
63466359
// problems related to member lookup more precisely.
63476360

6348-
// If base type is a "hole" there is no reason to record any
6349-
// more "member not found" fixes for chained member references.
6350-
if (baseTy->isHole()) {
6351-
increaseScore(SK_Fix);
6352-
if (auto *memberTypeVar = memberTy->getAs<TypeVariableType>())
6353-
recordPotentialHole(memberTypeVar);
6354-
return SolutionKind::Solved;
6355-
}
6356-
63576361
return fixMissingMember(origBaseTy, memberTy, locator);
63586362
}
63596363
return SolutionKind::Error;

0 commit comments

Comments
 (0)