Skip to content

Commit b433724

Browse files
committed
Remove default argument from getContextualType
1 parent ee50783 commit b433724

9 files changed

+32
-20
lines changed

include/swift/Sema/ConstraintSystem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3201,7 +3201,7 @@ class ConstraintSystem {
32013201
/// for use in constraint solving, \c forConstraint should be set to \c true,
32023202
/// which will ensure that unbound generics have been opened and placeholder
32033203
/// types have been converted to type variables, etc.
3204-
Type getContextualType(ASTNode node, bool forConstraint = false) {
3204+
Type getContextualType(ASTNode node, bool forConstraint) {
32053205
if (forConstraint) {
32063206
auto known = contextualTypes.find(node);
32073207
if (known == contextualTypes.end())

lib/Sema/CSApply.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6454,7 +6454,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
64546454
// Diagnose conversions to invalid function types that couldn't be performed
64556455
// beforehand because of placeholders.
64566456
if (auto *fnTy = toType->getAs<FunctionType>()) {
6457-
auto contextTy = cs.getContextualType(expr);
6457+
auto contextTy = cs.getContextualType(expr, /*forConstraint=*/false);
64586458
if (cs.getConstraintLocator(locator)->isForContextualType() && contextTy &&
64596459
contextTy->hasPlaceholder()) {
64606460
bool hadError = TypeChecker::diagnoseInvalidFunctionType(

lib/Sema/CSDiagnostics.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class FailureDiagnostic {
132132

133133
Type getContextualType(ASTNode anchor) const {
134134
auto &cs = getConstraintSystem();
135-
return cs.getContextualType(anchor);
135+
return cs.getContextualType(anchor, /*forConstraint=*/false);
136136
}
137137

138138
TypeLoc getContextualTypeLoc(ASTNode anchor) const {

lib/Sema/CSFix.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ getStructuralTypeContext(const Solution &solution, ConstraintLocator *locator) {
398398

399399
auto &cs = solution.getConstraintSystem();
400400
auto anchor = locator->getAnchor();
401-
auto contextualType = cs.getContextualType(anchor);
401+
auto contextualType = cs.getContextualType(anchor, /*forConstraint=*/false);
402402
auto exprType = cs.getType(anchor);
403403
return std::make_tuple(contextualTypeElt->getPurpose(), exprType,
404404
contextualType);

lib/Sema/CSGen.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,9 @@ namespace {
564564
return false;
565565

566566
return isFavoredParamAndArg(CS, paramTy, argTy) &&
567-
hasContextuallyFavorableResultType(fnTy,
568-
CS.getContextualType(expr));
567+
hasContextuallyFavorableResultType(
568+
fnTy,
569+
CS.getContextualType(expr, /*forConstraint=*/false));
569570
};
570571

571572
favorCallOverloads(expr, CS, isFavoredDecl);
@@ -714,7 +715,7 @@ namespace {
714715
auto firstParamTy = params[0].getOldType();
715716
auto secondParamTy = params[1].getOldType();
716717

717-
auto contextualTy = CS.getContextualType(expr);
718+
auto contextualTy = CS.getContextualType(expr, /*forConstraint=*/false);
718719

719720
// Avoid favoring overloads that would require narrowing conversion
720721
// to match the arguments.
@@ -1709,7 +1710,7 @@ namespace {
17091710
return Type();
17101711

17111712
auto locator = CS.getConstraintLocator(expr);
1712-
auto contextualType = CS.getContextualType(expr);
1713+
auto contextualType = CS.getContextualType(expr, /*forConstraint=*/false);
17131714
auto contextualPurpose = CS.getContextualTypePurpose(expr);
17141715

17151716
auto joinElementTypes = [&](Optional<Type> elementType) {
@@ -1837,7 +1838,7 @@ namespace {
18371838
auto valueAssocTy = dictionaryProto->getAssociatedType(C.Id_Value);
18381839

18391840
auto locator = CS.getConstraintLocator(expr);
1840-
auto contextualType = CS.getContextualType(expr);
1841+
auto contextualType = CS.getContextualType(expr, /*forConstraint=*/false);
18411842
auto contextualPurpose = CS.getContextualTypePurpose(expr);
18421843

18431844
// If a contextual type exists for this expression and is a dictionary
@@ -1904,7 +1905,7 @@ namespace {
19041905

19051906
// If no contextual type is present, Merge equivalence classes of key
19061907
// and value types as necessary.
1907-
if (!CS.getContextualType(expr)) {
1908+
if (!CS.getContextualType(expr, /*forConstraint=*/false)) {
19081909
for (auto element1 : expr->getElements()) {
19091910
for (auto element2 : expr->getElements()) {
19101911
if (element1 == element2)
@@ -2068,7 +2069,13 @@ namespace {
20682069
return resolvedTy;
20692070
}
20702071

2071-
if (auto contextualType = CS.getContextualType(closure)) {
2072+
// Because we are only pulling out the result type from the contextual
2073+
// type, we avoid prematurely converting any inferrable types by setting
2074+
// forConstraint=false. Later on in inferClosureType we call
2075+
// replaceInferableTypesWithTypeVars before returning to ensure we don't
2076+
// introduce any placeholders into the constraint system.
2077+
if (auto contextualType =
2078+
CS.getContextualType(closure, /*forConstraint=*/false)) {
20722079
if (auto fnType = contextualType->getAs<FunctionType>())
20732080
return fnType->getResult();
20742081
}

lib/Sema/CSSimplify.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -4949,7 +4949,8 @@ bool ConstraintSystem::repairFailures(
49494949
// If `if` expression has a contextual type, let's consider it a source of
49504950
// truth and produce a contextual mismatch instead of per-branch failure,
49514951
// because it's a better pointer than potential then-to-else type mismatch.
4952-
if (auto contextualType = getContextualType(anchor)) {
4952+
if (auto contextualType =
4953+
getContextualType(anchor, /*forConstraint=*/false)) {
49534954
auto purpose = getContextualTypePurpose(anchor);
49544955
if (contextualType->isEqual(rhs)) {
49554956
auto *loc = getConstraintLocator(
@@ -6336,7 +6337,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
63366337
// If this is a `nil` literal, it would be a contextual failure.
63376338
if (auto *Nil = getAsExpr<NilLiteralExpr>(anchor)) {
63386339
auto *fixLocator = getConstraintLocator(
6339-
getContextualType(Nil)
6340+
getContextualType(Nil, /*forConstraint=*/false)
63406341
? locator.withPathElement(LocatorPathElt::ContextualType(
63416342
getContextualTypePurpose(Nil)))
63426343
: locator);
@@ -9633,7 +9634,7 @@ ConstraintSystem::simplifyKeyPathConstraint(
96339634
return SolutionKind::Error;
96349635

96359636
// If the expression has contextual type information, try using that too.
9636-
if (auto contextualTy = getContextualType(keyPath)) {
9637+
if (auto contextualTy = getContextualType(keyPath, /*forConstraint=*/false)) {
96379638
if (!tryMatchRootAndValueFromType(contextualTy))
96389639
return SolutionKind::Error;
96399640
}

lib/Sema/CSSolver.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ void ConstraintSystem::shrink(Expr *expr) {
877877
// A dictionary expression is just a set of tuples; try to solve ones
878878
// that have overload sets.
879879
if (auto collectionExpr = dyn_cast<CollectionExpr>(expr)) {
880-
visitCollectionExpr(collectionExpr, CS.getContextualType(expr),
880+
visitCollectionExpr(collectionExpr,
881+
CS.getContextualType(expr, /*forConstraint=*/false),
881882
CS.getContextualTypePurpose(expr));
882883
// Don't try to walk into the dictionary.
883884
return {false, expr};
@@ -950,7 +951,8 @@ void ConstraintSystem::shrink(Expr *expr) {
950951
if (Candidates.empty())
951952
return expr;
952953

953-
auto contextualType = CS.getContextualType(expr);
954+
auto contextualType = CS.getContextualType(expr,
955+
/*forConstraint=*/false);
954956
// If there is a contextual type set for this expression.
955957
if (!contextualType.isNull()) {
956958
Candidates.push_back(Candidate(CS, PrimaryExpr, contextualType,

lib/Sema/ConstraintSystem.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -3670,7 +3670,7 @@ static bool diagnoseAmbiguityWithContextualType(
36703670
auto name = result->choices.front().getName();
36713671
DE.diagnose(getLoc(anchor), diag::no_candidates_match_result_type,
36723672
name.getBaseName().userFacingName(),
3673-
cs.getContextualType(anchor));
3673+
cs.getContextualType(anchor, /*forConstraint=*/false));
36743674

36753675
for (const auto &solution : solutions) {
36763676
auto overload = solution.getOverloadChoice(calleeLocator);
@@ -3685,7 +3685,8 @@ static bool diagnoseAmbiguityWithContextualType(
36853685
auto fnType = type->castTo<FunctionType>();
36863686
DE.diagnose(
36873687
loc, diag::cannot_convert_candidate_result_to_contextual_type,
3688-
decl->getName(), fnType->getResult(), cs.getContextualType(anchor));
3688+
decl->getName(), fnType->getResult(),
3689+
cs.getContextualType(anchor, /*forConstraint=*/false));
36893690
} else {
36903691
DE.diagnose(loc, diag::found_candidate_type, type);
36913692
}
@@ -3759,7 +3760,8 @@ static bool diagnoseAmbiguity(
37593760
if (locator->isForContextualType()) {
37603761
auto baseName = name.getBaseName();
37613762
DE.diagnose(getLoc(commonAnchor), diag::no_candidates_match_result_type,
3762-
baseName.userFacingName(), cs.getContextualType(anchor));
3763+
baseName.userFacingName(),
3764+
cs.getContextualType(anchor, /*forConstraint=*/false));
37633765
} else if (name.isOperator()) {
37643766
auto *anchor = castToExpr(commonCalleeLocator->getAnchor());
37653767

lib/Sema/TypeCheckCodeCompletion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ sawSolution(const constraints::Solution &S) {
11531153
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
11541154
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
11551155
if (!ParentExpr)
1156-
ExpectedTy = CS.getContextualType(CompletionExpr);
1156+
ExpectedTy = CS.getContextualType(CompletionExpr, /*forConstraint=*/false);
11571157

11581158
auto *CalleeLocator = S.getCalleeLocator(Locator);
11591159
ValueDecl *ReferencedDecl = nullptr;

0 commit comments

Comments
 (0)