Skip to content

Commit 8d582aa

Browse files
authored
Merge pull request #15475 from slavapestov/small-sema-nit
Fix a couple of small nits
2 parents cf9b29d + e0def7b commit 8d582aa

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

lib/AST/Type.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,26 +1466,14 @@ bool TypeBase::isBindableTo(Type b) {
14661466
if (bound != Bindings.end()) {
14671467
return bound->second->isEqual(subst);
14681468
}
1469-
1470-
auto canBindClassConstrainedArchetype = [](CanType t) -> bool {
1471-
// Classes and class-constrained archetypes.
1472-
if (t->mayHaveSuperclass())
1473-
return true;
1474-
1475-
// Pure @objc existentials.
1476-
if (t->isObjCExistentialType())
1477-
return true;
1478-
1479-
return false;
1480-
};
1481-
1469+
14821470
// Check that the archetype isn't constrained in a way that makes the
14831471
// binding impossible.
14841472
// For instance, if the archetype is class-constrained, and the binding
14851473
// is not a class, it can never be bound.
1486-
if (orig->requiresClass() && !canBindClassConstrainedArchetype(subst))
1474+
if (orig->requiresClass() && !subst->satisfiesClassConstraint())
14871475
return false;
1488-
1476+
14891477
// TODO: If the archetype has a superclass constraint, check that the
14901478
// substitution is a subclass.
14911479

lib/Sema/CalleeCandidateInfo.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,36 @@
2525
using namespace swift;
2626
using namespace constraints;
2727

28+
/// \brief Determine whether one type would be a valid substitution for an
29+
/// archetype.
30+
///
31+
/// \param type The potential type.
32+
///
33+
/// \param archetype The archetype for which type may (or may not) be
34+
/// substituted.
35+
///
36+
/// \param dc The context of the check.
37+
///
38+
/// \returns true if \c t1 is a valid substitution for \c t2.
39+
static bool isSubstitutableFor(Type type, ArchetypeType *archetype,
40+
DeclContext *dc) {
41+
if (archetype->requiresClass() && !type->satisfiesClassConstraint())
42+
return false;
43+
44+
if (auto superclass = archetype->getSuperclass()) {
45+
if (!superclass->isExactSuperclassOf(type))
46+
return false;
47+
}
48+
49+
for (auto proto : archetype->getConformsTo()) {
50+
if (!dc->getParentModule()->lookupConformance(
51+
type, proto))
52+
return false;
53+
}
54+
55+
return true;
56+
}
57+
2858
UncurriedCandidate::UncurriedCandidate(ValueDecl *decl, unsigned level)
2959
: declOrExpr(decl), level(level), substituted(false) {
3060

@@ -426,14 +456,14 @@ CalleeCandidateInfo::evaluateCloseness(UncurriedCandidate candidate,
426456
if (!archetype->isPrimary())
427457
return { CC_ArgumentMismatch, {}};
428458

429-
if (!CS.TC.isSubstitutableFor(substitution, archetype, CS.DC)) {
459+
if (!isSubstitutableFor(substitution, archetype, CS.DC)) {
430460
// If we have multiple non-substitutable types, this is just a mismatched mess.
431461
if (!nonSubstitutableArchetype.isNull())
432462
return { CC_ArgumentMismatch, {}};
433463

434464
if (auto argOptType = argType->getOptionalObjectType())
435465
mismatchesAreNearMisses &=
436-
CS.TC.isSubstitutableFor(argOptType, archetype, CS.DC);
466+
isSubstitutableFor(argOptType, archetype, CS.DC);
437467
else
438468
mismatchesAreNearMisses = false;
439469

@@ -455,7 +485,7 @@ CalleeCandidateInfo::evaluateCloseness(UncurriedCandidate candidate,
455485
// type might be the one that we should be substituting for instead.
456486
// Note that failureInfo is already set correctly for that case.
457487
if (isNonSubstitutableArchetype && nonSubstitutableArgs == 1 &&
458-
CS.TC.isSubstitutableFor(substitution, archetype, CS.DC)) {
488+
isSubstitutableFor(substitution, archetype, CS.DC)) {
459489
mismatchesAreNearMisses = argumentMismatchIsNearMiss(existingSubstitution, substitution);
460490
allGenericSubstitutions[archetype] = substitution;
461491
} else {
@@ -959,7 +989,7 @@ bool CalleeCandidateInfo::diagnoseGenericParameterErrors(Expr *badArgExpr) {
959989

960990
// Check for optional near miss.
961991
if (auto argOptType = substitution->getOptionalObjectType()) {
962-
if (CS.TC.isSubstitutableFor(argOptType, paramArchetype, CS.DC)) {
992+
if (isSubstitutableFor(argOptType, paramArchetype, CS.DC)) {
963993
CS.TC.diagnose(badArgExpr->getLoc(), diag::missing_unwrap_optional,
964994
argType);
965995
foundFailure = true;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,25 +2816,6 @@ bool TypeChecker::checkedCastMaySucceed(Type t1, Type t2, DeclContext *dc) {
28162816
return (kind != CheckedCastKind::Unresolved);
28172817
}
28182818

2819-
bool TypeChecker::isSubstitutableFor(Type type, ArchetypeType *archetype,
2820-
DeclContext *dc) {
2821-
if (archetype->requiresClass() && !type->satisfiesClassConstraint())
2822-
return false;
2823-
2824-
if (auto superclass = archetype->getSuperclass()) {
2825-
if (!superclass->isExactSuperclassOf(type))
2826-
return false;
2827-
}
2828-
2829-
for (auto proto : archetype->getConformsTo()) {
2830-
if (!dc->getParentModule()->lookupConformance(
2831-
type, proto))
2832-
return false;
2833-
}
2834-
2835-
return true;
2836-
}
2837-
28382819
Expr *TypeChecker::coerceToRValue(Expr *expr,
28392820
llvm::function_ref<Type(Expr *)> getType,
28402821
llvm::function_ref<void(Expr *, Type)> setType) {

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,19 +1277,6 @@ class TypeChecker final : public LazyResolver {
12771277
DeclContext *dc,
12781278
bool *unwrappedIUO = nullptr);
12791279

1280-
/// \brief Determine whether one type would be a valid substitution for an
1281-
/// archetype.
1282-
///
1283-
/// \param type The potential type.
1284-
///
1285-
/// \param archetype The archetype for which type may (or may not) be
1286-
/// substituted.
1287-
///
1288-
/// \param dc The context of the check.
1289-
///
1290-
/// \returns true if \c t1 is a valid substitution for \c t2.
1291-
bool isSubstitutableFor(Type type, ArchetypeType *archetype, DeclContext *dc);
1292-
12931280
/// If the inputs to an apply expression use a consistent "sugar" type
12941281
/// (that is, a typealias or shorthand syntax) equivalent to the result type
12951282
/// of the function, set the result type of the expression to that sugar type.

0 commit comments

Comments
 (0)