Skip to content

Commit 515ffe9

Browse files
committed
Sema: Don't try to solve conversions where one side is a type variable
1 parent e276b12 commit 515ffe9

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,9 +1667,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16671667
auto desugar1 = type1->getDesugaredType();
16681668
auto desugar2 = type2->getDesugaredType();
16691669

1670-
auto *typeVar1 = desugar1->getAs<TypeVariableType>();
1671-
auto *typeVar2 = desugar2->getAs<TypeVariableType>();
1672-
16731670
// If the types are obviously equivalent, we're done.
16741671
if (desugar1->isEqual(desugar2))
16751672
return getTypeMatchSuccess();
@@ -1699,6 +1696,9 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16991696
return getTypeMatchAmbiguous();
17001697
};
17011698

1699+
auto *typeVar1 = dyn_cast<TypeVariableType>(desugar1);
1700+
auto *typeVar2 = dyn_cast<TypeVariableType>(desugar2);
1701+
17021702
// If either (or both) types are type variables, unify the type variables.
17031703
if (typeVar1 || typeVar2) {
17041704
// Handle the easy case of both being type variables, and being
@@ -1800,11 +1800,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
18001800
case ConstraintKind::Conversion:
18011801
case ConstraintKind::ArgumentConversion:
18021802
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();
18081804

18091805
case ConstraintKind::ApplicableFunction:
18101806
case ConstraintKind::DynamicCallableApplicableFunction:
@@ -1830,11 +1826,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
18301826
}
18311827
}
18321828

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+
}
18351835

18361836
llvm::SmallVector<RestrictionOrFix, 4> conversionsOrFixes;
1837-
bool concrete = !isTypeVarOrMember1 && !isTypeVarOrMember2;
18381837

18391838
// Decompose parallel structure.
18401839
TypeMatchOptions subflags =
@@ -1854,29 +1853,28 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
18541853
#define BUILTIN_TYPE(id, parent) case TypeKind::id:
18551854
#define TYPE(id, parent)
18561855
#include "swift/AST/TypeNodes.def"
1857-
case TypeKind::Module:
1858-
if (desugar1 == desugar2) {
1859-
return getTypeMatchSuccess();
1860-
}
1861-
return getTypeMatchFailure(locator);
18621856

18631857
case TypeKind::Error:
18641858
case TypeKind::Unresolved:
1865-
return getTypeMatchFailure(locator);
1859+
return getTypeMatchFailure(locator);
18661860

18671861
case TypeKind::GenericTypeParam:
18681862
llvm_unreachable("unmapped dependent type in type checker");
18691863

1864+
case TypeKind::TypeVariable:
1865+
llvm_unreachable("type variables should have already been handled by now");
1866+
18701867
case TypeKind::DependentMember:
18711868
// Nothing we can solve.
18721869
return formUnsolvedResult();
18731870

1874-
case TypeKind::TypeVariable:
1871+
case TypeKind::Module:
18751872
case TypeKind::PrimaryArchetype:
18761873
case TypeKind::OpenedArchetype:
18771874
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);
18801878

18811879
case TypeKind::Tuple: {
18821880
assert(!type2->is<LValueType>() && "Unexpected lvalue type!");
@@ -2010,7 +2008,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
20102008
}
20112009
}
20122010

2013-
if (concrete && kind >= ConstraintKind::Subtype) {
2011+
if (kind >= ConstraintKind::Subtype) {
20142012
// Subclass-to-superclass conversion.
20152013
if (type1->mayHaveSuperclass() &&
20162014
type2->getClassOrBoundGenericClass() &&
@@ -2166,7 +2164,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
21662164
return getTypeMatchSuccess();
21672165
}
21682166

2169-
if (concrete && kind >= ConstraintKind::Conversion) {
2167+
if (kind >= ConstraintKind::Conversion) {
21702168
// An lvalue of type T1 can be converted to a value of type T2 so long as
21712169
// T1 is convertible to T2 (by loading the value). Note that we cannot get
21722170
// a value of inout type as an lvalue though.
@@ -2301,7 +2299,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23012299
}
23022300
}
23032301

2304-
if (concrete && kind >= ConstraintKind::OperatorArgumentConversion) {
2302+
if (kind >= ConstraintKind::OperatorArgumentConversion) {
23052303
// If the RHS is an inout type, the LHS must be an @lvalue type.
23062304
if (auto *lvt = type1->getAs<LValueType>()) {
23072305
if (auto *iot = type2->getAs<InOutType>()) {
@@ -2317,7 +2315,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23172315
// to U by force-unwrapping the source value.
23182316
// A value of type T, T?, or T! can be converted to type U? or U! if
23192317
// T is convertible to U.
2320-
if (concrete && !type1->is<LValueType>() && kind >= ConstraintKind::Subtype) {
2318+
if (!type1->is<LValueType>() && kind >= ConstraintKind::Subtype) {
23212319
enumerateOptionalConversionRestrictions(
23222320
type1, type2, kind, locator,
23232321
[&](ConversionRestrictionKind restriction) {
@@ -2329,15 +2327,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23292327
// literals.
23302328
if (auto elt = locator.last()) {
23312329
if (elt->getKind() == ConstraintLocator::ClosureResult) {
2332-
if (concrete && kind >= ConstraintKind::Subtype &&
2330+
if (kind >= ConstraintKind::Subtype &&
23332331
(type1->isUninhabited() || type2->isVoid())) {
23342332
increaseScore(SK_FunctionConversion);
23352333
return getTypeMatchSuccess();
23362334
}
23372335
}
23382336
}
23392337

2340-
if (concrete && kind == ConstraintKind::BindParam) {
2338+
if (kind == ConstraintKind::BindParam) {
23412339
if (auto *iot = dyn_cast<InOutType>(desugar1)) {
23422340
if (auto *lvt = dyn_cast<LValueType>(desugar2)) {
23432341
return matchTypes(iot->getObjectType(), lvt->getObjectType(),
@@ -2351,7 +2349,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23512349
// Attempt fixes iff it's allowed, both types are concrete and
23522350
// we are not in the middle of attempting one already.
23532351
bool attemptFixes =
2354-
shouldAttemptFixes() && concrete && !flags.contains(TMF_ApplyingFix);
2352+
shouldAttemptFixes() && !flags.contains(TMF_ApplyingFix);
23552353

23562354
// When we hit this point, we're committed to the set of potential
23572355
// conversions recorded thus far.
@@ -2446,14 +2444,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
24462444
if (attemptFixes)
24472445
repairFailures(*this, type1, type2, conversionsOrFixes, locator);
24482446

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-
2447+
if (conversionsOrFixes.empty())
24552448
return getTypeMatchFailure(locator);
2456-
}
24572449

24582450
// Where there is more than one potential conversion, create a disjunction
24592451
// so that we'll explore all of the options.

0 commit comments

Comments
 (0)