@@ -1611,230 +1611,6 @@ static Constraint *selectBestBindingDisjunction(
1611
1611
return firstBindDisjunction;
1612
1612
}
1613
1613
1614
- // For a given type, collect any concrete types or literal
1615
- // conformances we can reach by walking the constraint graph starting
1616
- // from this point.
1617
- //
1618
- // For example, if the type is a type variable, we'll walk back
1619
- // through the constraints mentioning this type variable and find what
1620
- // types are converted to this type along with what literals are
1621
- // conformed-to by this type.
1622
- void ConstraintSystem::ArgumentInfoCollector::walk (Type argType) {
1623
- llvm::SmallSet<TypeVariableType *, 4 > visited;
1624
- llvm::SmallVector<Type, 4 > worklist;
1625
- worklist.push_back (argType);
1626
-
1627
- while (!worklist.empty ()) {
1628
- auto itemTy = worklist.pop_back_val ()->getRValueType ();
1629
-
1630
- if (!itemTy->is <TypeVariableType>()) {
1631
- addType (itemTy);
1632
- continue ;
1633
- }
1634
-
1635
- auto tyvar = itemTy->castTo <TypeVariableType>();
1636
- if (auto fixedTy = CS.getFixedType (tyvar)) {
1637
- addType (fixedTy);
1638
- continue ;
1639
- }
1640
-
1641
- auto *rep = CS.getRepresentative (tyvar);
1642
-
1643
- // FIXME: This can happen when we have two type variables that are
1644
- // subtypes of each other. We would ideally merge those type
1645
- // variables somewhere.
1646
- if (visited.count (rep))
1647
- continue ;
1648
-
1649
- visited.insert (rep);
1650
-
1651
- auto constraints = CS.getConstraintGraph ().gatherConstraints (
1652
- rep, ConstraintGraph::GatheringKind::EquivalenceClass);
1653
-
1654
- for (auto *constraint : constraints) {
1655
- switch (constraint->getKind ()) {
1656
- case ConstraintKind::LiteralConformsTo:
1657
- addLiteralProtocol (constraint->getProtocol ());
1658
- break ;
1659
-
1660
- case ConstraintKind::Bind:
1661
- case ConstraintKind::Equal: {
1662
- auto firstTy = constraint->getFirstType ();
1663
- auto secondTy = constraint->getSecondType ();
1664
- if (firstTy->is <TypeVariableType>()) {
1665
- auto otherRep =
1666
- CS.getRepresentative (firstTy->castTo <TypeVariableType>());
1667
- if (otherRep->isEqual (rep))
1668
- worklist.push_back (secondTy);
1669
- }
1670
- if (secondTy->is <TypeVariableType>()) {
1671
- auto otherRep =
1672
- CS.getRepresentative (secondTy->castTo <TypeVariableType>());
1673
- if (otherRep->isEqual (rep))
1674
- worklist.push_back (firstTy);
1675
- }
1676
- break ;
1677
- }
1678
-
1679
- case ConstraintKind::Subtype:
1680
- case ConstraintKind::OperatorArgumentConversion:
1681
- case ConstraintKind::ArgumentConversion:
1682
- case ConstraintKind::Conversion:
1683
- case ConstraintKind::BridgingConversion:
1684
- case ConstraintKind::BindParam:
1685
- case ConstraintKind::OpaqueUnderlyingType: {
1686
- auto secondTy = constraint->getSecondType ();
1687
- if (secondTy->is <TypeVariableType>()) {
1688
- auto otherRep =
1689
- CS.getRepresentative (secondTy->castTo <TypeVariableType>());
1690
- if (otherRep->isEqual (rep))
1691
- worklist.push_back (constraint->getFirstType ());
1692
- }
1693
- break ;
1694
- }
1695
-
1696
- case ConstraintKind::DynamicTypeOf:
1697
- case ConstraintKind::EscapableFunctionOf: {
1698
- auto firstTy = constraint->getFirstType ();
1699
- if (firstTy->is <TypeVariableType>()) {
1700
- auto otherRep =
1701
- CS.getRepresentative (firstTy->castTo <TypeVariableType>());
1702
- if (otherRep->isEqual (rep))
1703
- worklist.push_back (constraint->getSecondType ());
1704
- }
1705
- break ;
1706
- }
1707
-
1708
- case ConstraintKind::OptionalObject: {
1709
- // Get the underlying object type.
1710
- auto secondTy = constraint->getSecondType ();
1711
- if (secondTy->is <TypeVariableType>()) {
1712
- auto otherRep =
1713
- CS.getRepresentative (secondTy->castTo <TypeVariableType>());
1714
- if (otherRep->isEqual (rep)) {
1715
- // See if we can actually determine what the underlying
1716
- // type is.
1717
- Type fixedTy;
1718
- auto firstTy = constraint->getFirstType ();
1719
- if (!firstTy->is <TypeVariableType>()) {
1720
- fixedTy = firstTy;
1721
- } else {
1722
- fixedTy = CS.getFixedType (firstTy->castTo <TypeVariableType>());
1723
- }
1724
- if (fixedTy && fixedTy->getOptionalObjectType ())
1725
- worklist.push_back (fixedTy->getOptionalObjectType ());
1726
- }
1727
- }
1728
- break ;
1729
- }
1730
-
1731
- case ConstraintKind::KeyPathApplication:
1732
- case ConstraintKind::KeyPath: {
1733
- auto firstTy = constraint->getFirstType ();
1734
- if (firstTy->is <TypeVariableType>()) {
1735
- auto otherRep =
1736
- CS.getRepresentative (firstTy->castTo <TypeVariableType>());
1737
- if (otherRep->isEqual (rep))
1738
- worklist.push_back (constraint->getThirdType ());
1739
- }
1740
- break ;
1741
- }
1742
-
1743
- case ConstraintKind::BindToPointerType:
1744
- case ConstraintKind::ValueMember:
1745
- case ConstraintKind::ValueWitness:
1746
- case ConstraintKind::UnresolvedValueMember:
1747
- case ConstraintKind::Disjunction:
1748
- case ConstraintKind::CheckedCast:
1749
- case ConstraintKind::OpenedExistentialOf:
1750
- case ConstraintKind::ApplicableFunction:
1751
- case ConstraintKind::DynamicCallableApplicableFunction:
1752
- case ConstraintKind::BindOverload:
1753
- case ConstraintKind::FunctionInput:
1754
- case ConstraintKind::FunctionResult:
1755
- case ConstraintKind::SelfObjectOfProtocol:
1756
- case ConstraintKind::ConformsTo:
1757
- case ConstraintKind::Defaultable:
1758
- case ConstraintKind::OneWayEqual:
1759
- case ConstraintKind::OneWayBindParam:
1760
- case ConstraintKind::DefaultClosureType:
1761
- break ;
1762
- }
1763
- }
1764
- }
1765
- }
1766
-
1767
- void ConstraintSystem::ArgumentInfoCollector::minimizeLiteralProtocols () {
1768
- if (LiteralProtocols.size () <= 1 )
1769
- return ;
1770
-
1771
- llvm::SmallVector<std::pair<ProtocolDecl *, Type>, 2 > candidates;
1772
- llvm::SmallVector<ProtocolDecl *, 2 > skippedProtocols;
1773
-
1774
- for (auto *protocol : LiteralProtocols) {
1775
- if (auto defaultType = TypeChecker::getDefaultType (protocol, CS.DC )) {
1776
- candidates.push_back ({protocol, defaultType});
1777
- continue ;
1778
- }
1779
-
1780
- // Looks like argument expected to conform to something like
1781
- // `ExpressibleByNilLiteral` which doesn't have a default
1782
- // type and as a result can't participate in minimalization.
1783
- skippedProtocols.push_back (protocol);
1784
- }
1785
-
1786
- if (candidates.size () <= 1 )
1787
- return ;
1788
-
1789
- unsigned result = 0 ;
1790
- for (unsigned i = 1 , n = candidates.size (); i != n; ++i) {
1791
- const auto &candidate = candidates[i];
1792
-
1793
- auto first =
1794
- TypeChecker::conformsToProtocol (candidate.second , candidates[result].first ,
1795
- CS.DC );
1796
- auto second =
1797
- TypeChecker::conformsToProtocol (candidates[result].second , candidate.first ,
1798
- CS.DC );
1799
- if (first.isInvalid () == second.isInvalid ())
1800
- return ;
1801
-
1802
- if (!first.isInvalid ())
1803
- result = i;
1804
- }
1805
-
1806
- LiteralProtocols.clear ();
1807
- LiteralProtocols.insert (candidates[result].first );
1808
- LiteralProtocols.insert (skippedProtocols.begin (), skippedProtocols.end ());
1809
- }
1810
-
1811
- void ConstraintSystem::ArgumentInfoCollector::dump () const {
1812
- auto &log = llvm::errs ();
1813
- log << " types:\n " ;
1814
- for (auto type : Types)
1815
- type->print (log);
1816
- log << " \n " ;
1817
-
1818
- log << " literal protocols:\n " ;
1819
- for (auto *proto : LiteralProtocols)
1820
- proto->print (log);
1821
- log << " \n " ;
1822
- }
1823
-
1824
- // Check to see if we know something about the types of all arguments
1825
- // in the given function type.
1826
- bool ConstraintSystem::haveTypeInformationForAllArguments (
1827
- FunctionType *fnType) {
1828
- llvm::SetVector<Constraint *> literalConformsTo;
1829
- return llvm::all_of (fnType->getParams (),
1830
- [&](AnyFunctionType::Param param) -> bool {
1831
- ArgumentInfoCollector argInfo (*this , param);
1832
- auto countFacts = argInfo.getTypes ().size () +
1833
- argInfo.getLiteralProtocols ().size ();
1834
- return countFacts > 0 ;
1835
- });
1836
- }
1837
-
1838
1614
Optional<std::pair<Constraint *, unsigned >>
1839
1615
ConstraintSystem::findConstraintThroughOptionals (
1840
1616
TypeVariableType *typeVar, OptionalWrappingDirection optionalDirection,
0 commit comments