@@ -5340,47 +5340,18 @@ TypeVarBindingProducer::TypeVarBindingProducer(
5340
5340
return protocol->isSpecificProtocol (
5341
5341
KnownProtocolKind::ExpressibleByNilLiteral);
5342
5342
})) {
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
-
5369
5343
// A binding to `Any` which should always be considered as a last resort.
5370
5344
Optional<Binding> Any;
5371
5345
5372
5346
for (const auto &binding : bindings.Bindings ) {
5373
5347
auto type = binding.BindingType ;
5374
5348
5375
- if (!CanBeNil && type->isAny ()) {
5376
- Any.emplace (binding);
5377
- continue ;
5378
- }
5379
-
5380
5349
// Adjust optionality of existing bindings based on presence of
5381
5350
// `ExpressibleByNilLiteral` requirement.
5382
- if (CanBeNil && requiresOptionalAdjustment (binding)) {
5351
+ if (requiresOptionalAdjustment (binding)) {
5383
5352
Bindings.push_back (binding.withType (OptionalType::get (type)));
5353
+ } else if (type->isAny ()) {
5354
+ Any.emplace (binding);
5384
5355
} else {
5385
5356
Bindings.push_back (binding);
5386
5357
}
@@ -5393,3 +5364,33 @@ TypeVarBindingProducer::TypeVarBindingProducer(
5393
5364
Bindings.push_back (*Any);
5394
5365
}
5395
5366
}
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