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