@@ -1667,9 +1667,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1667
1667
auto desugar1 = type1->getDesugaredType ();
1668
1668
auto desugar2 = type2->getDesugaredType ();
1669
1669
1670
- auto *typeVar1 = desugar1->getAs <TypeVariableType>();
1671
- auto *typeVar2 = desugar2->getAs <TypeVariableType>();
1672
-
1673
1670
// If the types are obviously equivalent, we're done.
1674
1671
if (desugar1->isEqual (desugar2))
1675
1672
return getTypeMatchSuccess ();
@@ -1699,6 +1696,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1699
1696
return getTypeMatchAmbiguous ();
1700
1697
};
1701
1698
1699
+ auto *typeVar1 = dyn_cast<TypeVariableType>(desugar1);
1700
+ auto *typeVar2 = dyn_cast<TypeVariableType>(desugar2);
1701
+
1702
1702
// If either (or both) types are type variables, unify the type variables.
1703
1703
if (typeVar1 || typeVar2) {
1704
1704
// Handle the easy case of both being type variables, and being
@@ -1800,11 +1800,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1800
1800
case ConstraintKind::Conversion:
1801
1801
case ConstraintKind::ArgumentConversion:
1802
1802
case ConstraintKind::OperatorArgumentConversion:
1803
- // We couldn't solve this constraint. If only one of the types is a type
1804
- // variable, perhaps we can do something with it below.
1805
- if (typeVar1 && typeVar2)
1806
- return formUnsolvedResult ();
1807
- break ;
1803
+ return formUnsolvedResult ();
1808
1804
1809
1805
case ConstraintKind::ApplicableFunction:
1810
1806
case ConstraintKind::DynamicCallableApplicableFunction:
@@ -1830,11 +1826,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1830
1826
}
1831
1827
}
1832
1828
1833
- bool isTypeVarOrMember1 = desugar1->isTypeVariableOrMember ();
1834
- bool isTypeVarOrMember2 = desugar2->isTypeVariableOrMember ();
1829
+ // If one of the types is a member type of a type variable type,
1830
+ // there's nothing we can do.
1831
+ if (desugar1->isTypeVariableOrMember () ||
1832
+ desugar2->isTypeVariableOrMember ()) {
1833
+ return formUnsolvedResult ();
1834
+ }
1835
1835
1836
1836
llvm::SmallVector<RestrictionOrFix, 4 > conversionsOrFixes;
1837
- bool concrete = !isTypeVarOrMember1 && !isTypeVarOrMember2;
1838
1837
1839
1838
// Decompose parallel structure.
1840
1839
TypeMatchOptions subflags =
@@ -1854,35 +1853,39 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1854
1853
#define BUILTIN_TYPE (id, parent ) case TypeKind::id:
1855
1854
#define TYPE (id, parent )
1856
1855
#include " swift/AST/TypeNodes.def"
1857
- case TypeKind::Module:
1858
- if (desugar1 == desugar2) {
1859
- return getTypeMatchSuccess ();
1860
- }
1861
- return getTypeMatchFailure (locator);
1862
1856
1863
1857
case TypeKind::Error:
1864
1858
case TypeKind::Unresolved:
1865
- return getTypeMatchFailure (locator);
1859
+ return getTypeMatchFailure (locator);
1866
1860
1867
1861
case TypeKind::GenericTypeParam:
1868
1862
llvm_unreachable (" unmapped dependent type in type checker" );
1869
1863
1864
+ case TypeKind::TypeVariable:
1865
+ llvm_unreachable (" type variables should have already been handled by now" );
1866
+
1870
1867
case TypeKind::DependentMember:
1871
1868
// Nothing we can solve.
1872
1869
return formUnsolvedResult ();
1873
1870
1874
- case TypeKind::TypeVariable :
1871
+ case TypeKind::Module :
1875
1872
case TypeKind::PrimaryArchetype:
1876
1873
case TypeKind::OpenedArchetype:
1877
1874
case TypeKind::NestedArchetype:
1878
- // Nothing to do here; handle type variables and archetypes below.
1879
- break ;
1875
+ // If two module types or archetypes were not already equal, there's
1876
+ // nothing more we can do.
1877
+ return getTypeMatchFailure (locator);
1880
1878
1881
1879
case TypeKind::Tuple: {
1882
- assert (!type2->is <LValueType>() && " Unexpected lvalue type!" );
1883
- // Try the tuple-to-tuple conversion.
1884
- if (!type1->is <LValueType>())
1885
- conversionsOrFixes.push_back (ConversionRestrictionKind::TupleToTuple);
1880
+ auto result = matchTupleTypes (cast<TupleType>(desugar1),
1881
+ cast<TupleType>(desugar2),
1882
+ kind, subflags, locator);
1883
+ if (result != SolutionKind::Error)
1884
+ return result;
1885
+
1886
+ // FIXME: All cases in this switch should go down to the fix logic
1887
+ // to give repairFailures() a chance to run, but this breaks stuff
1888
+ // right now.
1886
1889
break ;
1887
1890
}
1888
1891
@@ -1891,11 +1894,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1891
1894
case TypeKind::Class: {
1892
1895
auto nominal1 = cast<NominalType>(desugar1);
1893
1896
auto nominal2 = cast<NominalType>(desugar2);
1894
- assert (!type2->is <LValueType>() && " Unexpected lvalue type!" );
1895
- if (!type1->is <LValueType>() &&
1896
- nominal1->getDecl () == nominal2->getDecl ()) {
1897
+ if (nominal1->getDecl () == nominal2->getDecl ())
1897
1898
conversionsOrFixes.push_back (ConversionRestrictionKind::DeepEquality);
1898
- }
1899
1899
1900
1900
// Check for CF <-> ObjectiveC bridging.
1901
1901
if (isa<ClassType>(desugar1) &&
@@ -1904,19 +1904,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
1904
1904
auto class2 = cast<ClassDecl>(nominal2->getDecl ());
1905
1905
1906
1906
// CF -> Objective-C via toll-free bridging.
1907
- assert (!type2->is <LValueType>() && " Unexpected lvalue type!" );
1908
- if (!type1->is <LValueType>() &&
1909
- class1->getForeignClassKind () == ClassDecl::ForeignKind::CFType &&
1907
+ if (class1->getForeignClassKind () == ClassDecl::ForeignKind::CFType &&
1910
1908
class2->getForeignClassKind () != ClassDecl::ForeignKind::CFType &&
1911
1909
class1->getAttrs ().hasAttribute <ObjCBridgedAttr>()) {
1912
1910
conversionsOrFixes.push_back (
1913
1911
ConversionRestrictionKind::CFTollFreeBridgeToObjC);
1914
1912
}
1915
1913
1916
1914
// Objective-C -> CF via toll-free bridging.
1917
- assert (!type2->is <LValueType>() && " Unexpected lvalue type!" );
1918
- if (!type1->is <LValueType>() &&
1919
- class2->getForeignClassKind () == ClassDecl::ForeignKind::CFType &&
1915
+ if (class2->getForeignClassKind () == ClassDecl::ForeignKind::CFType &&
1920
1916
class1->getForeignClassKind () != ClassDecl::ForeignKind::CFType &&
1921
1917
class2->getAttrs ().hasAttribute <ObjCBridgedAttr>()) {
1922
1918
conversionsOrFixes.push_back (
@@ -2001,16 +1997,24 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2001
1997
auto bound1 = cast<BoundGenericType>(desugar1);
2002
1998
auto bound2 = cast<BoundGenericType>(desugar2);
2003
1999
2004
- assert (!type2->is <LValueType>() && " Unexpected lvalue type!" );
2005
- if (!type1->is <LValueType>() && bound1->getDecl () == bound2->getDecl ()) {
2000
+ if (bound1->getDecl () == bound2->getDecl ())
2006
2001
conversionsOrFixes.push_back (ConversionRestrictionKind::DeepEquality);
2007
- }
2008
2002
break ;
2009
2003
}
2010
2004
}
2011
2005
}
2012
2006
2013
- if (concrete && kind >= ConstraintKind::Subtype) {
2007
+ if (kind >= ConstraintKind::Conversion) {
2008
+ // An lvalue of type T1 can be converted to a value of type T2 so long as
2009
+ // T1 is convertible to T2 (by loading the value). Note that we cannot get
2010
+ // a value of inout type as an lvalue though.
2011
+ if (type1->is <LValueType>() && !type2->is <InOutType>()) {
2012
+ return matchTypes (type1->getRValueType (), type2,
2013
+ kind, subflags, locator);
2014
+ }
2015
+ }
2016
+
2017
+ if (kind >= ConstraintKind::Subtype) {
2014
2018
// Subclass-to-superclass conversion.
2015
2019
if (type1->mayHaveSuperclass () &&
2016
2020
type2->getClassOrBoundGenericClass () &&
@@ -2166,14 +2170,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2166
2170
return getTypeMatchSuccess ();
2167
2171
}
2168
2172
2169
- if (concrete && kind >= ConstraintKind::Conversion) {
2170
- // An lvalue of type T1 can be converted to a value of type T2 so long as
2171
- // T1 is convertible to T2 (by loading the value). Note that we cannot get
2172
- // a value of inout type as an lvalue though.
2173
- if (type1->is <LValueType>() && !type2->is <InOutType>())
2174
- conversionsOrFixes.push_back (
2175
- ConversionRestrictionKind::LValueToRValue);
2176
-
2173
+ if (kind >= ConstraintKind::Conversion) {
2177
2174
// It is never legal to form an autoclosure that results in these
2178
2175
// implicit conversions to pointer types.
2179
2176
bool isAutoClosureArgument = false ;
@@ -2301,7 +2298,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2301
2298
}
2302
2299
}
2303
2300
2304
- if (concrete && kind >= ConstraintKind::OperatorArgumentConversion) {
2301
+ if (kind >= ConstraintKind::OperatorArgumentConversion) {
2305
2302
// If the RHS is an inout type, the LHS must be an @lvalue type.
2306
2303
if (auto *lvt = type1->getAs <LValueType>()) {
2307
2304
if (auto *iot = type2->getAs <InOutType>()) {
@@ -2317,7 +2314,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2317
2314
// to U by force-unwrapping the source value.
2318
2315
// A value of type T, T?, or T! can be converted to type U? or U! if
2319
2316
// T is convertible to U.
2320
- if (concrete && !type1->is <LValueType>() && kind >= ConstraintKind::Subtype) {
2317
+ if (!type1->is <LValueType>() && kind >= ConstraintKind::Subtype) {
2321
2318
enumerateOptionalConversionRestrictions (
2322
2319
type1, type2, kind, locator,
2323
2320
[&](ConversionRestrictionKind restriction) {
@@ -2329,15 +2326,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2329
2326
// literals.
2330
2327
if (auto elt = locator.last ()) {
2331
2328
if (elt->getKind () == ConstraintLocator::ClosureResult) {
2332
- if (concrete && kind >= ConstraintKind::Subtype &&
2329
+ if (kind >= ConstraintKind::Subtype &&
2333
2330
(type1->isUninhabited () || type2->isVoid ())) {
2334
2331
increaseScore (SK_FunctionConversion);
2335
2332
return getTypeMatchSuccess ();
2336
2333
}
2337
2334
}
2338
2335
}
2339
2336
2340
- if (concrete && kind == ConstraintKind::BindParam) {
2337
+ if (kind == ConstraintKind::BindParam) {
2341
2338
if (auto *iot = dyn_cast<InOutType>(desugar1)) {
2342
2339
if (auto *lvt = dyn_cast<LValueType>(desugar2)) {
2343
2340
return matchTypes (iot->getObjectType (), lvt->getObjectType (),
@@ -2351,7 +2348,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2351
2348
// Attempt fixes iff it's allowed, both types are concrete and
2352
2349
// we are not in the middle of attempting one already.
2353
2350
bool attemptFixes =
2354
- shouldAttemptFixes () && concrete && !flags.contains (TMF_ApplyingFix);
2351
+ shouldAttemptFixes () && !flags.contains (TMF_ApplyingFix);
2355
2352
2356
2353
// When we hit this point, we're committed to the set of potential
2357
2354
// conversions recorded thus far.
@@ -2421,7 +2418,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2421
2418
ForceDowncast::create (*this , type2, getConstraintLocator (locator)));
2422
2419
}
2423
2420
2424
- if (type2->getRValueType ()-> is <InOutType>()) {
2421
+ if (type2->is <InOutType>()) {
2425
2422
if (type1->is <LValueType>()) {
2426
2423
// If we're converting an lvalue to an inout type, add the missing '&'.
2427
2424
conversionsOrFixes.push_back (
@@ -2446,14 +2443,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
2446
2443
if (attemptFixes)
2447
2444
repairFailures (*this , type1, type2, conversionsOrFixes, locator);
2448
2445
2449
- if (conversionsOrFixes.empty ()) {
2450
- // If one of the types is a type variable or member thereof, we leave this
2451
- // unsolved.
2452
- if (isTypeVarOrMember1 || isTypeVarOrMember2)
2453
- return formUnsolvedResult ();
2454
-
2446
+ if (conversionsOrFixes.empty ())
2455
2447
return getTypeMatchFailure (locator);
2456
- }
2457
2448
2458
2449
// Where there is more than one potential conversion, create a disjunction
2459
2450
// so that we'll explore all of the options.
@@ -4930,6 +4921,8 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
4930
4921
ConstraintKind matchKind,
4931
4922
TypeMatchOptions flags,
4932
4923
ConstraintLocatorBuilder locator) {
4924
+ assert (!type1->isTypeVariableOrMember () && !type2->isTypeVariableOrMember ());
4925
+
4933
4926
// Add to the score based on context.
4934
4927
auto addContextualScore = [&] {
4935
4928
// Okay, we need to perform one or more conversions. If this
@@ -4941,41 +4934,18 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
4941
4934
}
4942
4935
};
4943
4936
4944
- // Local function to form an unsolved result.
4945
- auto formUnsolved = [&] {
4946
- if (flags.contains (TMF_GenerateConstraints)) {
4947
- addUnsolvedConstraint (
4948
- Constraint::createRestricted (
4949
- *this , matchKind, restriction, type1, type2,
4950
- getConstraintLocator (locator)));
4951
-
4952
- return SolutionKind::Solved;
4953
- }
4954
-
4955
- return SolutionKind::Unsolved;
4956
- };
4957
-
4958
4937
TypeMatchOptions subflags = getDefaultDecompositionOptions (flags);
4959
4938
4960
4939
switch (restriction) {
4961
4940
// for $< in { <, <c, <oc }:
4962
4941
// T_i $< U_i ===> (T_i...) $< (U_i...)
4963
- case ConversionRestrictionKind::TupleToTuple:
4964
- return matchTupleTypes (type1->castTo <TupleType>(),
4965
- type2->castTo <TupleType>(),
4966
- matchKind, subflags, locator);
4967
-
4968
4942
case ConversionRestrictionKind::DeepEquality:
4969
4943
return matchDeepEqualityTypes (type1, type2, locator);
4970
4944
4971
4945
case ConversionRestrictionKind::Superclass:
4972
4946
addContextualScore ();
4973
4947
return matchSuperclassTypes (type1, type2, subflags, locator);
4974
4948
4975
- case ConversionRestrictionKind::LValueToRValue:
4976
- return matchTypes (type1->getRValueType (), type2,
4977
- matchKind, subflags, locator);
4978
-
4979
4949
// for $< in { <, <c, <oc }:
4980
4950
// T $< U, U : P_i ===> T $< protocol<P_i...>
4981
4951
case ConversionRestrictionKind::Existential:
@@ -5027,9 +4997,6 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
5027
4997
increaseScore (SK_ValueToOptional);
5028
4998
5029
4999
assert (matchKind >= ConstraintKind::Subtype);
5030
- if (type2->isTypeVariableOrMember ())
5031
- return formUnsolved ();
5032
-
5033
5000
if (auto generic2 = type2->getAs <BoundGenericType>()) {
5034
5001
if (generic2->getDecl ()->isOptionalDecl ()) {
5035
5002
return matchTypes (type1, generic2->getGenericArgs ()[0 ],
@@ -5051,9 +5018,6 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
5051
5018
case ConversionRestrictionKind::OptionalToOptional: {
5052
5019
addContextualScore ();
5053
5020
5054
- if (type1->isTypeVariableOrMember () || type2->isTypeVariableOrMember ())
5055
- return formUnsolved ();
5056
-
5057
5021
assert (matchKind >= ConstraintKind::Subtype);
5058
5022
if (auto generic1 = type1->getAs <BoundGenericType>()) {
5059
5023
if (auto generic2 = type2->getAs <BoundGenericType>()) {
@@ -5296,18 +5260,6 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
5296
5260
llvm_unreachable (" bad conversion restriction" );
5297
5261
}
5298
5262
5299
- // Restrictions where CSApply can figure out the correct action from the shape of
5300
- // the types, rather than needing a record of the choice made.
5301
- static bool recordRestriction (ConversionRestrictionKind restriction) {
5302
- switch (restriction) {
5303
- case ConversionRestrictionKind::TupleToTuple:
5304
- case ConversionRestrictionKind::LValueToRValue:
5305
- return false ;
5306
- default :
5307
- return true ;
5308
- }
5309
- }
5310
-
5311
5263
ConstraintSystem::SolutionKind
5312
5264
ConstraintSystem::simplifyRestrictedConstraint (
5313
5265
ConversionRestrictionKind restriction,
@@ -5318,8 +5270,7 @@ ConstraintSystem::simplifyRestrictedConstraint(
5318
5270
switch (simplifyRestrictedConstraintImpl (restriction, type1, type2,
5319
5271
matchKind, flags, locator)) {
5320
5272
case SolutionKind::Solved:
5321
- if (recordRestriction (restriction))
5322
- ConstraintRestrictions.push_back (std::make_tuple (type1, type2, restriction));
5273
+ ConstraintRestrictions.push_back (std::make_tuple (type1, type2, restriction));
5323
5274
return SolutionKind::Solved;
5324
5275
5325
5276
case SolutionKind::Unsolved:
0 commit comments