Skip to content

Commit b0cdb72

Browse files
committed
IUO: Add a method to build a binding disjunction for T? and T.
Not yet used, so NFC.
1 parent 4dbf89b commit b0cdb72

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lib/Sema/ConstraintSystem.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,44 @@ class ConstraintSystem {
23312331
ConstraintLocatorBuilder locator);
23322332

23332333
public:
2334+
// Build a disjunction for the choices between an Optional type and
2335+
// it's underlying type. We'll make the choice of the Optional
2336+
// preferred, and select that if the expression type-checks
2337+
// appropriately with that type.
2338+
void buildDisjunctionForImplicitlyUnwrappedOptional(
2339+
TypeVariableType *tv, Type type, ConstraintLocator *locator) {
2340+
2341+
// Create the constraint to bind to the optional type and make it
2342+
// the favored choice.
2343+
auto *bindToOptional =
2344+
Constraint::create(*this, ConstraintKind::Bind, tv, type, locator);
2345+
bindToOptional->setFavored();
2346+
2347+
Type underlyingType;
2348+
2349+
// FIXME: Support unwrapping results of function calls.
2350+
assert(!type->is<AnyFunctionType>());
2351+
2352+
underlyingType = type->getWithoutSpecifierType()->getAnyOptionalObjectType();
2353+
assert(underlyingType);
2354+
2355+
if (type->is<LValueType>())
2356+
underlyingType = LValueType::get(underlyingType);
2357+
else if (type->is<InOutType>())
2358+
underlyingType = InOutType::get(underlyingType);
2359+
2360+
// Create the constraint to bind to the underlying type.
2361+
auto *bindToUnderlying = Constraint::create(*this, ConstraintKind::Bind, tv,
2362+
underlyingType, locator);
2363+
2364+
llvm::SmallVector<Constraint *, 2> choices = {bindToOptional,
2365+
bindToUnderlying};
2366+
2367+
// Create the disjunction
2368+
addDisjunctionConstraint(choices, locator, RememberChoice);
2369+
}
2370+
2371+
23342372
/// \brief Resolve the given overload set to the given choice.
23352373
void resolveOverload(ConstraintLocator *locator, Type boundType,
23362374
OverloadChoice choice, DeclContext *useDC);

0 commit comments

Comments
 (0)