@@ -523,6 +523,7 @@ namespace {
523
523
enum class LiteralBindingKind : unsigned char {
524
524
None,
525
525
Collection,
526
+ Float,
526
527
Atom,
527
528
};
528
529
@@ -537,7 +538,16 @@ namespace {
537
538
AllowedBindingKind Kind;
538
539
539
540
// / The defaulted protocol associated with this binding.
540
- Optional<ProtocolDecl *> DefaultedProtocol;
541
+ Optional<ProtocolDecl *> DefaultedProtocol;
542
+
543
+ // / Whether this is a binding that comes from a 'Defaultable' constraint.
544
+ bool IsDefaultableBinding = false ;
545
+
546
+ PotentialBinding (Type type, AllowedBindingKind kind,
547
+ Optional<ProtocolDecl *> defaultedProtocol = None,
548
+ bool isDefaultableBinding = false )
549
+ : BindingType(type), Kind(kind), DefaultedProtocol(defaultedProtocol),
550
+ IsDefaultableBinding (isDefaultableBinding) { }
541
551
};
542
552
543
553
struct PotentialBindings {
@@ -556,25 +566,35 @@ namespace {
556
566
// / Whether this type variable is only bound above by existential types.
557
567
bool SubtypeOfExistentialType = false ;
558
568
569
+ // / The number of defaultable bindings.
570
+ unsigned NumDefaultableBindings = 0 ;
571
+
559
572
// / Determine whether the set of bindings is non-empty.
560
573
explicit operator bool () const {
561
574
return !Bindings.empty ();
562
575
}
563
576
577
+ // / Whether there are any non-defaultable bindings.
578
+ bool hasNonDefaultableBindings () const {
579
+ return Bindings.size () > NumDefaultableBindings;
580
+ }
581
+
564
582
// / Compare two sets of bindings, where \c x < y indicates that
565
583
// / \c x is a better set of bindings that \c y.
566
584
friend bool operator <(const PotentialBindings &x,
567
585
const PotentialBindings &y) {
568
- return std::make_tuple (x.FullyBound ,
586
+ return std::make_tuple (!x.hasNonDefaultableBindings (),
587
+ x.FullyBound ,
569
588
x.SubtypeOfExistentialType ,
570
589
static_cast <unsigned char >(x.LiteralBinding ),
571
590
x.InvolvesTypeVariables ,
572
- -x.Bindings .size ())
573
- < std::make_tuple (y.FullyBound ,
591
+ -(x.Bindings .size () - x.NumDefaultableBindings ))
592
+ < std::make_tuple (!y.hasNonDefaultableBindings (),
593
+ y.FullyBound ,
574
594
y.SubtypeOfExistentialType ,
575
595
static_cast <unsigned char >(y.LiteralBinding ),
576
596
y.InvolvesTypeVariables ,
577
- -y.Bindings .size ());
597
+ -( y.Bindings .size () - y. NumDefaultableBindings ));
578
598
}
579
599
580
600
void foundLiteralBinding (ProtocolDecl *proto) {
@@ -585,6 +605,10 @@ namespace {
585
605
LiteralBinding = LiteralBindingKind::Collection;
586
606
break ;
587
607
608
+ case KnownProtocolKind::ExpressibleByFloatLiteral:
609
+ LiteralBinding = LiteralBindingKind::Float;
610
+ break ;
611
+
588
612
default :
589
613
if (LiteralBinding != LiteralBindingKind::Collection)
590
614
LiteralBinding = LiteralBindingKind::Atom;
@@ -672,7 +696,7 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
672
696
PotentialBindings result;
673
697
llvm::SmallPtrSet<CanType, 4 > exactTypes;
674
698
llvm::SmallPtrSet<ProtocolDecl *, 4 > literalProtocols;
675
- bool hasDefaultableConstraint = false ;
699
+ SmallVector<Constraint *, 2 > defaultableConstraints ;
676
700
auto &tc = cs.getTypeChecker ();
677
701
for (auto constraint : constraints) {
678
702
// Only visit each constraint once.
@@ -709,7 +733,7 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
709
733
710
734
case ConstraintKind::Defaultable:
711
735
// Do these in a separate pass.
712
- hasDefaultableConstraint = true ;
736
+ defaultableConstraints. push_back (constraint) ;
713
737
continue ;
714
738
715
739
case ConstraintKind::Disjunction:
@@ -985,17 +1009,14 @@ static PotentialBindings getPotentialBindings(ConstraintSystem &cs,
985
1009
}
986
1010
}
987
1011
988
- // If we haven't found any other bindings yet, go ahead and consider
989
- // the defaulting constraints.
990
- if (result.Bindings .empty () && hasDefaultableConstraint) {
991
- for (Constraint *constraint : constraints) {
992
- if (constraint->getKind () != ConstraintKind::Defaultable)
993
- continue ;
1012
+ // / Add defaultable constraints last.
1013
+ for (auto constraint : defaultableConstraints) {
1014
+ Type type = constraint->getSecondType ();
1015
+ if (!exactTypes.insert (type->getCanonicalType ()).second )
1016
+ continue ;
994
1017
995
- result.Bindings .push_back ({constraint->getSecondType (),
996
- AllowedBindingKind::Exact,
997
- None});
998
- }
1018
+ ++result.NumDefaultableBindings ;
1019
+ result.Bindings .push_back ({type, AllowedBindingKind::Exact, None, true });
999
1020
}
1000
1021
1001
1022
// Determine if the bindings only constrain the type variable from above with
@@ -1062,7 +1083,12 @@ static bool tryTypeVariableBindings(
1062
1083
log <<" \n " ;
1063
1084
}
1064
1085
1065
- for (auto binding : bindings) {
1086
+ for (const auto &binding : bindings) {
1087
+ // If this is a defaultable binding and we have found any solutions,
1088
+ // don't explore the default binding.
1089
+ if (binding.IsDefaultableBinding && anySolved)
1090
+ continue ;
1091
+
1066
1092
auto type = binding.BindingType ;
1067
1093
1068
1094
// If the type variable can't bind to an lvalue, make sure the
0 commit comments