@@ -365,6 +365,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
365
365
auto &tc = getTypeChecker ();
366
366
bool hasNonDependentMemberRelationalConstraints = false ;
367
367
bool hasDependentMemberRelationalConstraints = false ;
368
+ bool sawNilLiteral = false ;
368
369
for (auto constraint : constraints) {
369
370
switch (constraint->getKind ()) {
370
371
case ConstraintKind::Bind:
@@ -481,8 +482,10 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
481
482
// If there is a 'nil' literal constraint, we might need optional
482
483
// supertype bindings.
483
484
if (constraint->getProtocol ()->isSpecificProtocol (
484
- KnownProtocolKind::ExpressibleByNilLiteral))
485
+ KnownProtocolKind::ExpressibleByNilLiteral)) {
486
+ sawNilLiteral = true ;
485
487
addOptionalSupertypeBindings = true ;
488
+ }
486
489
487
490
// If there is a default literal type for this protocol, it's a
488
491
// potential binding.
@@ -721,6 +724,32 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
721
724
result.Bindings .clear ();
722
725
}
723
726
727
+ // Revise any optional-of-function-types we may try to nil literals
728
+ // to be non-throwing so they don't inadvertantly result in rethrows
729
+ // diagnostics.
730
+ if (sawNilLiteral) {
731
+ for (auto &binding : result.Bindings ) {
732
+ auto nested = binding.BindingType ->lookThroughAllOptionalTypes ();
733
+ if (!nested)
734
+ continue ;
735
+
736
+ if (!nested->is <FunctionType>())
737
+ continue ;
738
+
739
+ // Remove throws from the nested function type.
740
+ binding.BindingType =
741
+ binding.BindingType .transform ([&](Type inner) -> Type {
742
+ auto *fnTy = dyn_cast<FunctionType>(inner.getPointer ());
743
+ if (!fnTy)
744
+ return inner;
745
+
746
+ auto extInfo = fnTy->getExtInfo ().withThrows (false );
747
+ return FunctionType::get (fnTy->getParams (), fnTy->getResult (),
748
+ extInfo);
749
+ });
750
+ }
751
+ }
752
+
724
753
return result;
725
754
}
726
755
0 commit comments