Skip to content

Commit 50cc0f0

Browse files
committed
[ConstraintSystem] NFC: Extract requiresOptionalAdjustment so it could be used for default bindings
1 parent e678429 commit 50cc0f0

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5852,6 +5852,10 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
58525852
/// \returns true if some new bindings were sucessfully computed,
58535853
/// false otherwise.
58545854
bool computeNext();
5855+
5856+
/// Check whether binding type is required to either conform to
5857+
/// `ExpressibleByNilLiteral` protocol or be wrapped into an optional type.
5858+
bool requiresOptionalAdjustment(const Binding &binding) const;
58555859
};
58565860

58575861
/// Iterator over disjunction choices, makes it

lib/Sema/ConstraintSystem.cpp

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,47 +5340,18 @@ TypeVarBindingProducer::TypeVarBindingProducer(
53405340
return protocol->isSpecificProtocol(
53415341
KnownProtocolKind::ExpressibleByNilLiteral);
53425342
})) {
5343-
auto requiresOptionalAdjustment =
5344-
[&cs](const ConstraintSystem::PotentialBinding &binding) {
5345-
if (binding.Kind == BindingKind::Supertypes) {
5346-
auto type = binding.BindingType->getRValueType();
5347-
// If the type doesn't conform to ExpressibleByNilLiteral,
5348-
// produce an optional of that type as a potential binding. We
5349-
// overwrite the binding in place because the non-optional type
5350-
// will fail to type-check against the nil-literal conformance.
5351-
bool conformsToExprByNilLiteral = false;
5352-
if (auto *nominalBindingDecl = type->getAnyNominal()) {
5353-
SmallVector<ProtocolConformance *, 2> conformances;
5354-
conformsToExprByNilLiteral = nominalBindingDecl->lookupConformance(
5355-
cs.DC->getParentModule(),
5356-
cs.getASTContext().getProtocol(
5357-
KnownProtocolKind::ExpressibleByNilLiteral),
5358-
conformances);
5359-
}
5360-
return !conformsToExprByNilLiteral;
5361-
} else if (binding.isDefaultableBinding() &&
5362-
binding.BindingType->isAny()) {
5363-
return true;
5364-
}
5365-
5366-
return false;
5367-
};
5368-
53695343
// A binding to `Any` which should always be considered as a last resort.
53705344
Optional<Binding> Any;
53715345

53725346
for (const auto &binding : bindings.Bindings) {
53735347
auto type = binding.BindingType;
53745348

5375-
if (!CanBeNil && type->isAny()) {
5376-
Any.emplace(binding);
5377-
continue;
5378-
}
5379-
53805349
// Adjust optionality of existing bindings based on presence of
53815350
// `ExpressibleByNilLiteral` requirement.
5382-
if (CanBeNil && requiresOptionalAdjustment(binding)) {
5351+
if (requiresOptionalAdjustment(binding)) {
53835352
Bindings.push_back(binding.withType(OptionalType::get(type)));
5353+
} else if (type->isAny()) {
5354+
Any.emplace(binding);
53845355
} else {
53855356
Bindings.push_back(binding);
53865357
}
@@ -5393,3 +5364,33 @@ TypeVarBindingProducer::TypeVarBindingProducer(
53935364
Bindings.push_back(*Any);
53945365
}
53955366
}
5367+
5368+
bool TypeVarBindingProducer::requiresOptionalAdjustment(
5369+
const Binding &binding) const {
5370+
// If type variable can't be `nil` then adjustment is
5371+
// not required.
5372+
if (!CanBeNil)
5373+
return false;
5374+
5375+
if (binding.Kind == BindingKind::Supertypes) {
5376+
auto type = binding.BindingType->getRValueType();
5377+
// If the type doesn't conform to ExpressibleByNilLiteral,
5378+
// produce an optional of that type as a potential binding. We
5379+
// overwrite the binding in place because the non-optional type
5380+
// will fail to type-check against the nil-literal conformance.
5381+
bool conformsToExprByNilLiteral = false;
5382+
if (auto *nominalBindingDecl = type->getAnyNominal()) {
5383+
SmallVector<ProtocolConformance *, 2> conformances;
5384+
conformsToExprByNilLiteral = nominalBindingDecl->lookupConformance(
5385+
CS.DC->getParentModule(),
5386+
CS.getASTContext().getProtocol(
5387+
KnownProtocolKind::ExpressibleByNilLiteral),
5388+
conformances);
5389+
}
5390+
return !conformsToExprByNilLiteral;
5391+
} else if (binding.isDefaultableBinding() && binding.BindingType->isAny()) {
5392+
return true;
5393+
}
5394+
5395+
return false;
5396+
}

0 commit comments

Comments
 (0)