Skip to content

Commit 1123b1f

Browse files
committed
Move constraint satisfiability utilities
1 parent 63896c1 commit 1123b1f

File tree

9 files changed

+45
-65
lines changed

9 files changed

+45
-65
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ static bool diagnoseClosureExplicitParameterMismatch(
22342234
isUnresolvedOrTypeVarType(argType))
22352235
continue;
22362236

2237-
if (!CS.TC.isConvertibleTo(argType, paramType, CS.DC)) {
2237+
if (!TypeChecker::isConvertibleTo(argType, paramType, CS.DC)) {
22382238
CS.getASTContext().Diags.diagnose(loc, diag::types_not_convertible,
22392239
false, paramType, argType);
22402240
return true;

lib/Sema/CSDiagnostics.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,11 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor, Type lhs,
521521

522522
if (req.getKind() != RequirementKind::SameType) {
523523
if (auto wrappedType = lhs->getOptionalObjectType()) {
524-
auto &tc = getTypeChecker();
525524
auto kind = (req.getKind() == RequirementKind::Superclass ?
526525
ConstraintKind::Subtype : ConstraintKind::ConformsTo);
527-
if (tc.typesSatisfyConstraint(wrappedType, rhs, /*openArchetypes=*/false,
528-
kind, getDC()))
526+
if (TypeChecker::typesSatisfyConstraint(wrappedType, rhs,
527+
/*openArchetypes=*/false,
528+
kind, getDC()))
529529
emitDiagnostic(getAnchor()->getLoc(),
530530
diag::wrapped_type_satisfies_requirement, wrappedType);
531531
}
@@ -1029,8 +1029,6 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
10291029
return false;
10301030

10311031
auto *DC = getDC();
1032-
auto &TC = getTypeChecker();
1033-
10341032
auto *anchor = getAnchor();
10351033
if (auto *assign = dyn_cast<AssignExpr>(anchor))
10361034
anchor = assign->getSrc();
@@ -1043,9 +1041,8 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
10431041
if (!toType->hasTypeRepr())
10441042
return false;
10451043

1046-
bool useAs = TC.isExplicitlyConvertibleTo(fromType, toType, DC);
1047-
bool useAsBang = !useAs && TC.checkedCastMaySucceed(fromType, toType, DC);
1048-
if (!useAs && !useAsBang)
1044+
bool useAs = TypeChecker::isExplicitlyConvertibleTo(fromType, toType, DC);
1045+
if (!useAs && TypeChecker::checkedCastMaySucceed(fromType, toType, DC))
10491046
return false;
10501047

10511048
auto *expr = findParentExpr(getAnchor());
@@ -2287,8 +2284,6 @@ void ContextualFailure::tryFixIts(InFlightDiagnostic &diagnostic) const {
22872284
}
22882285

22892286
bool ContextualFailure::diagnoseMissingFunctionCall() const {
2290-
auto &TC = getTypeChecker();
2291-
22922287
if (getLocator()->isLastElement<LocatorPathElt::RValueAdjustment>())
22932288
return false;
22942289

@@ -2297,7 +2292,7 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const {
22972292
return false;
22982293

22992294
if (ToType->is<AnyFunctionType>() ||
2300-
!TC.isConvertibleTo(srcFT->getResult(), ToType, getDC()))
2295+
!TypeChecker::isConvertibleTo(srcFT->getResult(), ToType, getDC()))
23012296
return false;
23022297

23032298
auto *anchor = getAnchor();
@@ -2479,8 +2474,6 @@ bool ContextualFailure::tryRawRepresentableFixIts(
24792474
InFlightDiagnostic &diagnostic,
24802475
KnownProtocolKind rawRepresentableProtocol) const {
24812476
auto &CS = getConstraintSystem();
2482-
auto &TC = getTypeChecker();
2483-
24842477
auto *expr = getAnchor();
24852478
auto fromType = getFromType();
24862479
auto toType = getToType();
@@ -2549,7 +2542,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25492542
convWrapBefore += "(rawValue: ";
25502543
std::string convWrapAfter = ")";
25512544
if (!isa<LiteralExpr>(expr) &&
2552-
!TC.isConvertibleTo(fromType, rawTy, getDC())) {
2545+
!TypeChecker::isConvertibleTo(fromType, rawTy, getDC())) {
25532546
// Only try to insert a converting construction if the protocol is a
25542547
// literal protocol and not some other known protocol.
25552548
switch (rawRepresentableProtocol) {
@@ -2574,7 +2567,7 @@ bool ContextualFailure::tryRawRepresentableFixIts(
25742567
if (conformsToKnownProtocol(CS, toType, rawRepresentableProtocol)) {
25752568
std::string convWrapBefore;
25762569
std::string convWrapAfter = ".rawValue";
2577-
if (!TC.isConvertibleTo(rawTy, toType, getDC())) {
2570+
if (!TypeChecker::isConvertibleTo(rawTy, toType, getDC())) {
25782571
// Only try to insert a converting construction if the protocol is a
25792572
// literal protocol and not some other known protocol.
25802573
switch (rawRepresentableProtocol) {
@@ -2621,8 +2614,7 @@ bool ContextualFailure::tryIntegerCastFixIts(
26212614
auto *anchor = getAnchor();
26222615
if (Expr *innerE = getInnerCastedExpr(anchor)) {
26232616
Type innerTy = getType(innerE);
2624-
auto &TC = getTypeChecker();
2625-
if (TC.isConvertibleTo(innerTy, ToType, getDC())) {
2617+
if (TypeChecker::isConvertibleTo(innerTy, ToType, getDC())) {
26262618
// Remove the unnecessary cast.
26272619
diagnostic.fixItRemoveChars(anchor->getLoc(), innerE->getStartLoc())
26282620
.fixItRemove(anchor->getEndLoc());
@@ -4140,8 +4132,7 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument(
41404132
auto argType = cs.simplifyType(cs.getType(argument));
41414133
auto paramType = fnType->getParams()[1].getPlainType();
41424134

4143-
auto &TC = cs.getTypeChecker();
4144-
return TC.isConvertibleTo(argType, paramType, cs.DC);
4135+
return TypeChecker::isConvertibleTo(argType, paramType, cs.DC);
41454136
}
41464137

41474138
std::tuple<Expr *, Expr *, unsigned, bool>

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,14 +3821,6 @@ bool swift::areGenericRequirementsSatisfied(
38213821
return CS.solveSingle().hasValue();
38223822
}
38233823

3824-
bool swift::canSatisfy(Type type1, Type type2, bool openArchetypes,
3825-
ConstraintKind kind, DeclContext *dc) {
3826-
auto *TC = dc->getASTContext().getLegacyGlobalTypeChecker();
3827-
assert(TC && "Must have type checker to make semantic query!");
3828-
return TC->typesSatisfyConstraint(type1, type2, openArchetypes, kind, dc,
3829-
/*unwrappedIUO=*/nullptr);
3830-
}
3831-
38323824
void swift::eraseOpenedExistentials(ConstraintSystem &CS, Expr *&expr) {
38333825
expr = expr->walk(SanitizeExpr(CS, /*eraseOEsOnly=*/true));
38343826
}

lib/Sema/CSRanking.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,6 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
846846
}
847847

848848
// The kinds of overload choice match, but the contents don't.
849-
auto &tc = cs.getTypeChecker();
850849
switch (choice1.getKind()) {
851850
case OverloadChoiceKind::TupleIndex:
852851
continue;
@@ -905,9 +904,9 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
905904
ctor2->getResultInterfaceType());
906905

907906
if (!resType1->isEqual(resType2)) {
908-
if (tc.isSubtypeOf(resType1, resType2, cs.DC)) {
907+
if (TypeChecker::isSubtypeOf(resType1, resType2, cs.DC)) {
909908
score1 += weight;
910-
} else if (tc.isSubtypeOf(resType2, resType1, cs.DC)) {
909+
} else if (TypeChecker::isSubtypeOf(resType2, resType1, cs.DC)) {
911910
score2 += weight;
912911
}
913912
}
@@ -1046,7 +1045,6 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
10461045
}
10471046

10481047
// Compare the type variable bindings.
1049-
auto &tc = cs.getTypeChecker();
10501048
for (auto &binding : diff.typeBindings) {
10511049
// If the type variable isn't one for which we should be looking at the
10521050
// bindings, don't.
@@ -1072,8 +1070,8 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
10721070
// If one type is a subtype of the other, but not vice-versa,
10731071
// we prefer the system with the more-constrained type.
10741072
// FIXME: Collapse this check into the second check.
1075-
auto type1Better = tc.isSubtypeOf(type1, type2, cs.DC);
1076-
auto type2Better = tc.isSubtypeOf(type2, type1, cs.DC);
1073+
auto type1Better = TypeChecker::isSubtypeOf(type1, type2, cs.DC);
1074+
auto type2Better = TypeChecker::isSubtypeOf(type2, type1, cs.DC);
10771075
if (type1Better || type2Better) {
10781076
if (type1Better)
10791077
++score1;

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,8 +2511,8 @@ static bool canBridgeThroughCast(ConstraintSystem &cs, Type fromType,
25112511
if (fromType->isAnyObject() && toType->getClassOrBoundGenericClass())
25122512
return true;
25132513

2514-
auto &TC = cs.getTypeChecker();
2515-
auto bridged = TC.getDynamicBridgedThroughObjCClass(cs.DC, fromType, toType);
2514+
auto bridged = TypeChecker::getDynamicBridgedThroughObjCClass(cs.DC,
2515+
fromType, toType);
25162516
if (!bridged)
25172517
return false;
25182518

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ CalleeCandidateInfo::ClosenessResultTy CalleeCandidateInfo::evaluateCloseness(
387387
// type is identical to the argument type, or substitutable via handling
388388
// of functions with primary archetypes in one or more parameters.
389389
// We can still do something more sophisticated with this.
390-
// FIXME: Use TC.isConvertibleTo?
390+
// FIXME: Use TypeChecker::isConvertibleTo?
391391

392392
TypeSubstitutionMap archetypesMap;
393393
bool matched;

lib/Sema/IDETypeCheckingRequests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ TypeRelationCheckRequest::evaluate(Evaluator &evaluator,
107107
break;
108108
}
109109
assert(CKind.hasValue());
110-
return canSatisfy(Owner.Pair.FirstTy, Owner.Pair.SecondTy, Owner.OpenArchetypes,
111-
*CKind, Owner.DC);
110+
return TypeChecker::typesSatisfyConstraint(Owner.Pair.FirstTy,
111+
Owner.Pair.SecondTy,
112+
Owner.OpenArchetypes,
113+
*CKind, Owner.DC);
112114
}
113115

114116
llvm::Expected<TypePair>

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ TypeChecker::getDynamicBridgedThroughObjCClass(DeclContext *dc,
422422
if (!valueType->isPotentiallyBridgedValueType())
423423
return Type();
424424

425-
return Context.getBridgedToObjC(dc, valueType);
425+
return dc->getASTContext().getBridgedToObjC(dc, valueType);
426426
}
427427

428428
Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,

lib/Sema/TypeChecker.h

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ class TypeChecker final {
870870
/// \param dc The context of the check.
871871
///
872872
/// \returns true if \c t1 is a subtype of \c t2.
873-
bool isSubtypeOf(Type t1, Type t2, DeclContext *dc);
873+
static bool isSubtypeOf(Type t1, Type t2, DeclContext *dc);
874874

875875
/// Determine whether one type is implicitly convertible to another.
876876
///
@@ -884,8 +884,8 @@ class TypeChecker final {
884884
/// conversion force-unwrapped an implicitly-unwrapped optional.
885885
///
886886
/// \returns true if \c t1 can be implicitly converted to \c t2.
887-
bool isConvertibleTo(Type t1, Type t2, DeclContext *dc,
888-
bool *unwrappedIUO = nullptr);
887+
static bool isConvertibleTo(Type t1, Type t2, DeclContext *dc,
888+
bool *unwrappedIUO = nullptr);
889889

890890
/// Determine whether one type is explicitly convertible to another,
891891
/// i.e. using an 'as' expression.
@@ -897,7 +897,7 @@ class TypeChecker final {
897897
/// \param dc The context of the conversion.
898898
///
899899
/// \returns true if \c t1 can be explicitly converted to \c t2.
900-
bool isExplicitlyConvertibleTo(Type t1, Type t2, DeclContext *dc);
900+
static bool isExplicitlyConvertibleTo(Type t1, Type t2, DeclContext *dc);
901901

902902
/// Determine whether one type is bridged to another type.
903903
///
@@ -911,8 +911,8 @@ class TypeChecker final {
911911
/// conversion force-unwrapped an implicitly-unwrapped optional.
912912
///
913913
/// \returns true if \c t1 can be explicitly converted to \c t2.
914-
bool isObjCBridgedTo(Type t1, Type t2, DeclContext *dc,
915-
bool *unwrappedIUO = nullptr);
914+
static bool isObjCBridgedTo(Type t1, Type t2, DeclContext *dc,
915+
bool *unwrappedIUO = nullptr);
916916

917917
/// Return true if performing a checked cast from one type to another
918918
/// with the "as!" operator could possibly succeed.
@@ -925,7 +925,7 @@ class TypeChecker final {
925925
///
926926
/// \returns true if a checked cast from \c t1 to \c t2 may succeed, and
927927
/// false if it will certainly fail, e.g. because the types are unrelated.
928-
bool checkedCastMaySucceed(Type t1, Type t2, DeclContext *dc);
928+
static bool checkedCastMaySucceed(Type t1, Type t2, DeclContext *dc);
929929

930930
/// Determine whether a constraint of the given kind can be satisfied
931931
/// by the two types.
@@ -944,11 +944,11 @@ class TypeChecker final {
944944
/// or bridge operation force-unwraps an implicitly-unwrapped optional.
945945
///
946946
/// \returns true if \c t1 and \c t2 satisfy the constraint.
947-
bool typesSatisfyConstraint(Type t1, Type t2,
948-
bool openArchetypes,
949-
constraints::ConstraintKind kind,
950-
DeclContext *dc,
951-
bool *unwrappedIUO = nullptr);
947+
static bool typesSatisfyConstraint(Type t1, Type t2,
948+
bool openArchetypes,
949+
constraints::ConstraintKind kind,
950+
DeclContext *dc,
951+
bool *unwrappedIUO = nullptr);
952952

953953
/// If the inputs to an apply expression use a consistent "sugar" type
954954
/// (that is, a typealias or shorthand syntax) equivalent to the result type
@@ -1251,13 +1251,13 @@ class TypeChecker final {
12511251
/// \returns a CheckedCastKind indicating the semantics of the cast. If the
12521252
/// cast is invalid, Unresolved is returned. If the cast represents an implicit
12531253
/// conversion, Coercion is returned.
1254-
CheckedCastKind typeCheckCheckedCast(Type fromType,
1255-
Type toType,
1256-
CheckedCastContextKind contextKind,
1257-
DeclContext *dc,
1258-
SourceLoc diagLoc,
1259-
Expr *fromExpr,
1260-
SourceRange diagToRange);
1254+
static CheckedCastKind typeCheckCheckedCast(Type fromType,
1255+
Type toType,
1256+
CheckedCastContextKind ctxKind,
1257+
DeclContext *dc,
1258+
SourceLoc diagLoc,
1259+
Expr *fromExpr,
1260+
SourceRange diagToRange);
12611261

12621262
/// Find the Objective-C class that bridges between a value of the given
12631263
/// dynamic type and the given value type.
@@ -1274,9 +1274,9 @@ class TypeChecker final {
12741274
/// type as an Objective-C class, e.g., \c NSString represents \c
12751275
/// String, or a null type if there is no such type or if the
12761276
/// dynamic type isn't something we can start from.
1277-
Type getDynamicBridgedThroughObjCClass(DeclContext *dc,
1278-
Type dynamicType,
1279-
Type valueType);
1277+
static Type getDynamicBridgedThroughObjCClass(DeclContext *dc,
1278+
Type dynamicType,
1279+
Type valueType);
12801280

12811281
/// Resolve ambiguous pattern/expr productions inside a pattern using
12821282
/// name lookup information. Must be done before type-checking the pattern.
@@ -2013,9 +2013,6 @@ bool areGenericRequirementsSatisfied(const DeclContext *DC,
20132013
SubstitutionMap Substitutions,
20142014
bool isExtension);
20152015

2016-
bool canSatisfy(Type type1, Type type2, bool openArchetypes,
2017-
constraints::ConstraintKind kind, DeclContext *dc);
2018-
20192016
bool hasDynamicMemberLookupAttribute(Type type,
20202017
llvm::DenseMap<CanType, bool> &DynamicMemberLookupCache);
20212018
} // end namespace swift

0 commit comments

Comments
 (0)