Skip to content

Commit b4549d6

Browse files
committed
[Constraint System] Refactor generateConstraints
We don't need a separate generateConstraints function for closures.
1 parent d398166 commit b4549d6

File tree

5 files changed

+40
-51
lines changed

5 files changed

+40
-51
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5042,15 +5042,7 @@ class ConstraintSystem {
50425042
bool generateConstraints(SolutionApplicationTarget &target,
50435043
FreeTypeVariableBinding allowFreeTypeVariables);
50445044

5045-
/// Generate constraints for the body of the given closure.
5046-
///
5047-
/// \param closure the closure expression
5048-
///
5049-
/// \returns \c true if constraint generation failed, \c false otherwise
5050-
LLVM_NODISCARD
5051-
bool generateConstraints(ClosureExpr *closure);
5052-
5053-
/// Generate constraints for the body of the given function.
5045+
/// Generate constraints for the body of the given function or closure.
50545046
///
50555047
/// \param fn The function or closure expression
50565048
/// \param body The body of the given function that should be

lib/AST/Module.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,8 +3497,7 @@ ArrayRef<OpaqueTypeDecl *> SourceFile::getOpaqueReturnTypeDecls() {
34973497
for (auto *vd : UnvalidatedDeclsWithOpaqueReturnTypes.takeVector()) {
34983498
if (auto opaqueDecl = vd->getOpaqueResultTypeDecl()) {
34993499
auto inserted = ValidatedOpaqueReturnTypes.insert(
3500-
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(),
3501-
opaqueDecl});
3500+
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(), opaqueDecl});
35023501
if (inserted.second) {
35033502
OpaqueReturnTypes.push_back(opaqueDecl);
35043503
}

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,13 +7234,13 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
72347234

72357235
pattern = patternRes.get();
72367236
}
7237-
7237+
72387238
bool hasOpaqueReturnTy = false;
72397239
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
72407240
hasOpaqueReturnTy = typedPattern->getTypeRepr()->hasOpaque();
72417241
}
72427242
auto sf = CurDeclContext->getParentSourceFile();
7243-
7243+
72447244
// Configure all vars with attributes, 'static' and parent pattern.
72457245
pattern->forEachVariable([&](VarDecl *VD) {
72467246
VD->setStatic(StaticLoc.isValid());
@@ -7543,13 +7543,12 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
75437543
CurDeclContext);
75447544

75457545
// Let the source file track the opaque return type mapping, if any.
7546-
if (FuncRetTy && FuncRetTy->hasOpaque() &&
7547-
!InInactiveClauseEnvironment) {
7546+
if (FuncRetTy && FuncRetTy->hasOpaque() && !InInactiveClauseEnvironment) {
75487547
if (auto sf = CurDeclContext->getParentSourceFile()) {
75497548
sf->addUnvalidatedDeclWithOpaqueResultType(FD);
75507549
}
75517550
}
7552-
7551+
75537552
// Parse a 'where' clause if present.
75547553
if (Tok.is(tok::kw_where)) {
75557554
ContextChange CC(*this, FD);
@@ -8512,7 +8511,7 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
85128511
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
85138512
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
85148513
Subscript->getAttrs() = Attributes;
8515-
8514+
85168515
// Let the source file track the opaque return type mapping, if any.
85178516
if (ElementTy.get() && ElementTy.get()->hasOpaque() &&
85188517
!InInactiveClauseEnvironment) {

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10797,7 +10797,8 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1079710797
setSolutionApplicationTarget(closure, target);
1079810798

1079910799
// Generate constraints from the body of this closure.
10800-
return !generateConstraints(closure);
10800+
return !generateConstraints(
10801+
AnyFunctionRef(cast<AbstractClosureExpr>(closure)), closure->getBody());
1080110802
}
1080210803

1080310804
ConstraintSystem::SolutionKind
@@ -14543,7 +14544,7 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1454314544

1454414545
case ConstraintKind::Disjunction:
1454514546
case ConstraintKind::Conjunction:
14546-
// {Dis, Con}junction constraints are never solved here.
14547+
// See {Dis, Con}junctionStep class in CSStep.cpp for solving
1454714548
return SolutionKind::Unsolved;
1454814549

1454914550
case ConstraintKind::OneWayEqual:

lib/Sema/CSSyntacticElement.cpp

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,46 +1122,44 @@ class SyntacticElementConstraintGenerator
11221122
};
11231123
}
11241124

1125-
bool ConstraintSystem::generateConstraints(ClosureExpr *closure) {
1126-
auto &ctx = closure->getASTContext();
1125+
bool ConstraintSystem::generateConstraints(AnyFunctionRef fn, BraceStmt *body) {
1126+
NullablePtr<ConstraintLocator> locator;
11271127

1128-
if (participatesInInference(closure)) {
1129-
SyntacticElementConstraintGenerator generator(
1130-
*this, SyntacticElementContext::forClosure(closure),
1131-
getConstraintLocator(closure));
1128+
if (auto *func = fn.getAbstractFunctionDecl()) {
1129+
locator = getConstraintLocator(func);
1130+
} else {
1131+
auto closure = cast<ClosureExpr>(fn.getAbstractClosureExpr());
1132+
locator = getConstraintLocator(closure);
11321133

1133-
generator.visit(closure->getBody());
1134+
auto &ctx = closure->getASTContext();
11341135

1135-
if (closure->hasSingleExpressionBody())
1136-
return generator.hadError;
1137-
}
1136+
if (participatesInInference(closure)) {
1137+
SyntacticElementConstraintGenerator generator(
1138+
*this, closure, getConstraintLocator(closure));
11381139

1139-
// If this closure has an empty body and no explicit result type
1140-
// let's bind result type to `Void` since that's the only type empty body
1141-
// can produce. Otherwise, if (multi-statement) closure doesn't have
1142-
// an explicit result (no `return` statements) let's default it to `Void`.
1143-
if (!hasExplicitResult(closure)) {
1144-
auto constraintKind =
1145-
(closure->hasEmptyBody() && !closure->hasExplicitResultType())
1146-
? ConstraintKind::Bind
1147-
: ConstraintKind::Defaultable;
1140+
generator.visit(closure->getBody());
11481141

1149-
addConstraint(
1150-
constraintKind, getClosureType(closure)->getResult(),
1151-
ctx.TheEmptyTupleType,
1152-
getConstraintLocator(closure, ConstraintLocator::ClosureResult));
1153-
}
1142+
if (closure->hasSingleExpressionBody())
1143+
return generator.hadError;
1144+
}
11541145

1155-
return false;
1156-
}
1146+
// If this closure has an empty body and no explicit result type
1147+
// let's bind result type to `Void` since that's the only type empty body
1148+
// can produce. Otherwise, if (multi-statement) closure doesn't have
1149+
// an explicit result (no `return` statements) let's default it to `Void`.
1150+
if (!hasExplicitResult(closure)) {
1151+
auto constraintKind =
1152+
(closure->hasEmptyBody() && !closure->hasExplicitResultType())
1153+
? ConstraintKind::Bind
1154+
: ConstraintKind::Defaultable;
11571155

1158-
bool ConstraintSystem::generateConstraints(AnyFunctionRef fn, BraceStmt *body) {
1159-
NullablePtr<ConstraintLocator> locator;
1156+
addConstraint(
1157+
constraintKind, getClosureType(closure)->getResult(),
1158+
ctx.TheEmptyTupleType,
1159+
getConstraintLocator(closure, ConstraintLocator::ClosureResult));
1160+
}
11601161

1161-
if (auto *func = fn.getAbstractFunctionDecl()) {
1162-
locator = getConstraintLocator(func);
1163-
} else {
1164-
locator = getConstraintLocator(fn.getAbstractClosureExpr());
1162+
return false;
11651163
}
11661164

11671165
SyntacticElementConstraintGenerator generator(

0 commit comments

Comments
 (0)