Skip to content

Commit d039107

Browse files
committed
[ConstraintSystem] Replace use of UnresolvedType with specialized HoleType
1 parent 48c9767 commit d039107

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void ConstraintSystem::PotentialBindings::finalize(
335335
if (locator->isLastElement<LocatorPathElt::MemberRefBase>())
336336
PotentiallyIncomplete = true;
337337

338-
addPotentialBinding(PotentialBinding::forHole(cs.getASTContext(), locator));
338+
addPotentialBinding(PotentialBinding::forHole(TypeVar, locator));
339339
}
340340

341341
// Let's always consider `Any` to be a last resort binding because

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4434,6 +4434,12 @@ bool ConstraintSystem::repairFailures(
44344434
}
44354435

44364436
case ConstraintLocator::FunctionBuilderBodyResult: {
4437+
// If result type of the body couldn't be determined
4438+
// there is going to be other fix available to diagnose
4439+
// the underlying issue.
4440+
if (lhs->isHole())
4441+
return true;
4442+
44374443
conversionsOrFixes.push_back(ContextualMismatch::create(
44384444
*this, lhs, rhs, getConstraintLocator(locator)));
44394445
break;
@@ -4685,9 +4691,19 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
46854691

46864692
case TypeKind::Error:
46874693
case TypeKind::Unresolved:
4688-
case TypeKind::Hole:
46894694
return getTypeMatchFailure(locator);
46904695

4696+
case TypeKind::Hole: {
4697+
// If it's allowed to attempt fixes, let's delegate
4698+
// decision to `repairFailures`, since depending on
4699+
// locator we might either ignore such a mismatch,
4700+
// or record a specialized fix.
4701+
if (shouldAttemptFixes())
4702+
break;
4703+
4704+
return getTypeMatchFailure(locator);
4705+
}
4706+
46914707
case TypeKind::GenericTypeParam:
46924708
llvm_unreachable("unmapped dependent type in type checker");
46934709

lib/Sema/ConstraintSystem.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,11 +2617,13 @@ Type ConstraintSystem::simplifyTypeImpl(Type type,
26172617
// there will be a missing conformance fix applied in diagnostic mode,
26182618
// so the concrete dependent member type is considered a "hole" in
26192619
// order to continue solving.
2620+
auto memberTy = DependentMemberType::get(lookupBaseType, assocType);
26202621
if (shouldAttemptFixes() &&
2621-
getPhase() == ConstraintSystemPhase::Solving)
2622-
return getASTContext().TheUnresolvedType;
2622+
getPhase() == ConstraintSystemPhase::Solving) {
2623+
return HoleType::get(getASTContext(), memberTy);
2624+
}
26232625

2624-
return DependentMemberType::get(lookupBaseType, assocType);
2626+
return memberTy;
26252627
}
26262628

26272629
auto subs = SubstitutionMap::getProtocolSubstitutions(
@@ -2653,16 +2655,23 @@ Type ConstraintSystem::simplifyType(Type type) const {
26532655
}
26542656

26552657
Type Solution::simplifyType(Type type) const {
2656-
if (!type->hasTypeVariable())
2658+
if (!(type->hasTypeVariable() || type->hasHole()))
26572659
return type;
26582660

26592661
// Map type variables to fixed types from bindings.
2660-
return getConstraintSystem().simplifyTypeImpl(type,
2661-
[&](TypeVariableType *tvt) -> Type {
2662-
auto known = typeBindings.find(tvt);
2663-
assert(known != typeBindings.end());
2664-
return known->second;
2665-
});
2662+
auto &cs = getConstraintSystem();
2663+
auto resolvedType = cs.simplifyTypeImpl(
2664+
type, [&](TypeVariableType *tvt) -> Type { return getFixedType(tvt); });
2665+
2666+
// Holes shouldn't be reachable through a solution, they are only
2667+
// useful to determine what went wrong exactly.
2668+
if (resolvedType->hasHole()) {
2669+
return resolvedType.transform([&](Type type) {
2670+
return type->isHole() ? Type(cs.getASTContext().TheUnresolvedType) : type;
2671+
});
2672+
}
2673+
2674+
return resolvedType;
26662675
}
26672676

26682677
size_t Solution::getTotalMemory() const {
@@ -4646,7 +4655,7 @@ SolutionApplicationTarget SolutionApplicationTarget::forInitialization(
46464655
// Determine the contextual type for the initialization.
46474656
TypeLoc contextualType;
46484657
if (!(isa<OptionalSomePattern>(pattern) && !pattern->isImplicit()) &&
4649-
patternType && !patternType->isHole()) {
4658+
patternType && !patternType->is<UnresolvedType>()) {
46504659
contextualType = TypeLoc::withoutLoc(patternType);
46514660

46524661
// Only provide a TypeLoc if it makes sense to allow diagnostics.

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,9 +4564,10 @@ class ConstraintSystem {
45644564
return {type, kind, BindingSource};
45654565
}
45664566

4567-
static PotentialBinding forHole(ASTContext &ctx,
4567+
static PotentialBinding forHole(TypeVariableType *typeVar,
45684568
ConstraintLocator *locator) {
4569-
return {ctx.TheUnresolvedType, AllowedBindingKind::Exact,
4569+
return {HoleType::get(typeVar->getASTContext(), typeVar),
4570+
AllowedBindingKind::Exact,
45704571
/*source=*/locator};
45714572
}
45724573
};

0 commit comments

Comments
 (0)