@@ -1709,15 +1709,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1709
1709
1710
1710
ConstraintKind subKind = ConstraintKind::Equal;
1711
1711
// A.Type < B.Type if A < B and both A and B are classes.
1712
- if (isa<MetatypeType>(desugar1) &&
1713
- kind != ConstraintKind::Equal &&
1712
+ if (isa<MetatypeType>(meta1) &&
1714
1713
meta1->getInstanceType ()->mayHaveSuperclass () &&
1715
1714
meta2->getInstanceType ()->getClassOrBoundGenericClass ())
1716
1715
subKind = std::min (kind, ConstraintKind::Subtype);
1717
1716
// P.Type < Q.Type if P < Q, both P and Q are protocols, and P.Type
1718
1717
// and Q.Type are both existential metatypes.
1719
- else if (isa<ExistentialMetatypeType>(meta1)
1720
- && isa<ExistentialMetatypeType>(meta2))
1718
+ else if (isa<ExistentialMetatypeType>(meta1))
1721
1719
subKind = std::min (kind, ConstraintKind::Subtype);
1722
1720
1723
1721
return matchTypes (meta1->getInstanceType (), meta2->getInstanceType (),
@@ -1827,13 +1825,48 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1827
1825
1828
1826
// Subclass-to-superclass conversion.
1829
1827
if (type1->mayHaveSuperclass () &&
1830
- type2->mayHaveSuperclass () &&
1831
1828
type2->getClassOrBoundGenericClass () &&
1832
1829
type1->getClassOrBoundGenericClass ()
1833
1830
!= type2->getClassOrBoundGenericClass ()) {
1834
1831
conversionsOrFixes.push_back (ConversionRestrictionKind::Superclass);
1835
1832
}
1836
1833
1834
+ // Existential-to-superclass conversion.
1835
+ if (type1->isClassExistentialType () &&
1836
+ type2->getClassOrBoundGenericClass ()) {
1837
+ conversionsOrFixes.push_back (ConversionRestrictionKind::Superclass);
1838
+ }
1839
+
1840
+ // Metatype-to-existential-metatype conversion.
1841
+ //
1842
+ // Equivalent to a conformance relation on the instance types.
1843
+ if (type1->is <MetatypeType>() &&
1844
+ type2->is <ExistentialMetatypeType>()) {
1845
+ conversionsOrFixes.push_back (
1846
+ ConversionRestrictionKind::MetatypeToExistentialMetatype);
1847
+ }
1848
+
1849
+ // Existential-metatype-to-superclass-metatype conversion.
1850
+ if (type2->is <MetatypeType>()) {
1851
+ if (auto *meta1 = type1->getAs <ExistentialMetatypeType>()) {
1852
+ if (meta1->getInstanceType ()->isClassExistentialType ()) {
1853
+ conversionsOrFixes.push_back (
1854
+ ConversionRestrictionKind::ExistentialMetatypeToMetatype);
1855
+ }
1856
+ }
1857
+ }
1858
+
1859
+ // Concrete value to existential conversion.
1860
+ if (!type1->is <LValueType>() &&
1861
+ type2->isExistentialType ()) {
1862
+
1863
+ // Penalize conversions to Any.
1864
+ if (kind >= ConstraintKind::Conversion && type2->isAny ())
1865
+ increaseScore (ScoreKind::SK_EmptyExistentialConversion);
1866
+
1867
+ conversionsOrFixes.push_back (ConversionRestrictionKind::Existential);
1868
+ }
1869
+
1837
1870
// T -> AnyHashable.
1838
1871
if (isAnyHashableType (desugar2)) {
1839
1872
// Don't allow this in operator contexts or we'll end up allowing
@@ -2072,43 +2105,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2072
2105
}
2073
2106
}
2074
2107
2075
- // Conformance of a metatype to an existential metatype is actually
2076
- // equivalent to a conformance relationship on the instance types.
2077
- // This applies to nested metatype levels, so if A : P then
2078
- // A.Type : P.Type.
2079
- if (concrete &&
2080
- kind >= ConstraintKind::Subtype &&
2081
- type1->is <MetatypeType>() &&
2082
- type2->is <ExistentialMetatypeType>()) {
2083
- conversionsOrFixes.push_back (
2084
- ConversionRestrictionKind::MetatypeToExistentialMetatype);
2085
- }
2086
-
2087
- // An existential metatype can be upcast to a class metatype if the
2088
- // existential contains a superclass constraint.
2089
- if (concrete &&
2090
- kind >= ConstraintKind::Subtype &&
2091
- type1->is <ExistentialMetatypeType>() &&
2092
- type2->is <MetatypeType>()) {
2093
- conversionsOrFixes.push_back (
2094
- ConversionRestrictionKind::ExistentialMetatypeToMetatype);
2095
- }
2096
-
2097
- // Instance type check for the above. We are going to check conformance once
2098
- // we hit commit_to_conversions below, but we have to add a token restriction
2099
- // to ensure we wrap the metatype value in a metatype erasure.
2100
- if (concrete &&
2101
- kind >= ConstraintKind::Subtype &&
2102
- !type1->is <LValueType>() &&
2103
- type2->isExistentialType ()) {
2104
-
2105
- // Penalize conversions to Any.
2106
- if (kind >= ConstraintKind::Conversion && type2->isAny ())
2107
- increaseScore (ScoreKind::SK_EmptyExistentialConversion);
2108
-
2109
- conversionsOrFixes.push_back (ConversionRestrictionKind::Existential);
2110
- }
2111
-
2112
2108
// A value of type T! can be converted to type U if T is convertible
2113
2109
// to U by force-unwrapping the source value.
2114
2110
// A value of type T, T?, or T! can be converted to type U? or U! if
0 commit comments