@@ -389,6 +389,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
389
389
auto &tc = getTypeChecker ();
390
390
bool hasNonDependentMemberRelationalConstraints = false ;
391
391
bool hasDependentMemberRelationalConstraints = false ;
392
+ bool sawNilLiteral = false ;
392
393
for (auto constraint : constraints) {
393
394
switch (constraint->getKind ()) {
394
395
case ConstraintKind::Bind:
@@ -505,8 +506,10 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
505
506
// If there is a 'nil' literal constraint, we might need optional
506
507
// supertype bindings.
507
508
if (constraint->getProtocol ()->isSpecificProtocol (
508
- KnownProtocolKind::ExpressibleByNilLiteral))
509
+ KnownProtocolKind::ExpressibleByNilLiteral)) {
510
+ sawNilLiteral = true ;
509
511
addOptionalSupertypeBindings = true ;
512
+ }
510
513
511
514
// If there is a default literal type for this protocol, it's a
512
515
// potential binding.
@@ -745,6 +748,32 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
745
748
result.Bindings .clear ();
746
749
}
747
750
751
+ // Revise any optional-of-function-types we may try to nil literals
752
+ // to be non-throwing so they don't inadvertantly result in rethrows
753
+ // diagnostics.
754
+ if (sawNilLiteral) {
755
+ for (auto &binding : result.Bindings ) {
756
+ auto nested = binding.BindingType ->lookThroughAllOptionalTypes ();
757
+ if (!nested)
758
+ continue ;
759
+
760
+ if (!nested->is <FunctionType>())
761
+ continue ;
762
+
763
+ // Remove throws from the nested function type.
764
+ binding.BindingType =
765
+ binding.BindingType .transform ([&](Type inner) -> Type {
766
+ auto *fnTy = dyn_cast<FunctionType>(inner.getPointer ());
767
+ if (!fnTy)
768
+ return inner;
769
+
770
+ auto extInfo = fnTy->getExtInfo ().withThrows (false );
771
+ return FunctionType::get (fnTy->getParams (), fnTy->getResult (),
772
+ extInfo);
773
+ });
774
+ }
775
+ }
776
+
748
777
return result;
749
778
}
750
779
0 commit comments