Skip to content

Commit e3a4b4f

Browse files
committed
[ConstraintSystem] Fix ConstraintGenerator to change phase to "constraint generation" and back
1 parent addc2b0 commit e3a4b4f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ namespace {
917917
class ConstraintGenerator : public ExprVisitor<ConstraintGenerator, Type> {
918918
ConstraintSystem &CS;
919919
DeclContext *CurDC;
920+
ConstraintSystemPhase CurrPhase;
920921
SmallVector<DeclContext*, 4> DCStack;
921922

922923
static const unsigned numEditorPlaceholderVariables = 2;
@@ -1108,9 +1109,16 @@ namespace {
11081109

11091110
public:
11101111
ConstraintGenerator(ConstraintSystem &CS, DeclContext *DC)
1111-
: CS(CS), CurDC(DC ? DC : CS.DC) { }
1112+
: CS(CS), CurDC(DC ? DC : CS.DC), CurrPhase(CS.getPhase()) {
1113+
// Although constraint system is initialized in `constraint
1114+
// generation` phase, we have to set it here manually because e.g.
1115+
// function builders could generate constraints for its body
1116+
// in the middle of the solving.
1117+
CS.setPhase(ConstraintSystemPhase::ConstraintGeneration);
1118+
}
11121119

11131120
virtual ~ConstraintGenerator() {
1121+
CS.setPhase(CurrPhase);
11141122
// We really ought to have this assertion:
11151123
// assert(DCStack.empty() && CurDC == CS.DC);
11161124
// Unfortunately, ASTWalker is really bad at letting us establish

lib/Sema/ConstraintSystem.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,16 +1536,24 @@ class ConstraintSystem {
15361536
};
15371537

15381538
public:
1539+
ConstraintSystemPhase getPhase() const { return Phase; }
1540+
15391541
/// Move constraint system to a new phase of its lifetime.
15401542
void setPhase(ConstraintSystemPhase newPhase) {
1543+
if (Phase == newPhase)
1544+
return;
1545+
15411546
#ifndef NDEBUG
15421547
switch (Phase) {
15431548
case ConstraintSystemPhase::ConstraintGeneration:
15441549
assert(newPhase == ConstraintSystemPhase::Solving);
15451550
break;
15461551

15471552
case ConstraintSystemPhase::Solving:
1548-
assert(newPhase == ConstraintSystemPhase::Diagnostics ||
1553+
// We can come back to constraint generation phase while
1554+
// processing function builder body.
1555+
assert(newPhase == ConstraintSystemPhase::ConstraintGeneration ||
1556+
newPhase == ConstraintSystemPhase::Diagnostics ||
15491557
newPhase == ConstraintSystemPhase::Finalization);
15501558
break;
15511559

0 commit comments

Comments
 (0)