Skip to content

Commit 0a262fc

Browse files
authored
Merge pull request #28181 from CodaFi/exprt-systems
[NFC] Remove Diagnostic State From TypeChecker
2 parents 5a1cae1 + efb6117 commit 0a262fc

File tree

6 files changed

+49
-32
lines changed

6 files changed

+49
-32
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,8 @@ Expr *FailureDiagnosis::typeCheckChildIndependently(
10171017
// expression (which may lead to infinite recursion). If the client is
10181018
// telling us that it knows what it is doing, then believe it.
10191019
if (!options.contains(TCC_ForceRecheck)) {
1020-
if (CS.getTypeChecker().isExprBeingDiagnosed(subExpr)) {
1021-
auto *savedExpr = CS.getTypeChecker().getExprBeingDiagnosed(subExpr);
1020+
if (CS.isExprBeingDiagnosed(subExpr)) {
1021+
auto *savedExpr = CS.getExprBeingDiagnosed(subExpr);
10221022
if (subExpr == savedExpr)
10231023
return subExpr;
10241024

@@ -1028,7 +1028,7 @@ Expr *FailureDiagnosis::typeCheckChildIndependently(
10281028
}
10291029

10301030
// Mark current expression as about to be diagnosed.
1031-
CS.getTypeChecker().addExprForDiagnosis(subExpr, subExpr);
1031+
CS.addExprForDiagnosis(subExpr, subExpr);
10321032

10331033
// Validate contextual type before trying to use it.
10341034
std::tie(convertType, convertTypePurpose) =
@@ -1118,7 +1118,7 @@ Expr *FailureDiagnosis::typeCheckChildIndependently(
11181118
}
11191119

11201120
if (preCheckedExpr != subExpr)
1121-
CS.getTypeChecker().addExprForDiagnosis(preCheckedExpr, subExpr);
1121+
CS.addExprForDiagnosis(preCheckedExpr, subExpr);
11221122

11231123
return subExpr;
11241124
}
@@ -4068,7 +4068,7 @@ diagnoseAmbiguousMultiStatementClosure(ClosureExpr *closure) {
40684068
if (hasUnresolvedParams)
40694069
continue;
40704070

4071-
ConstraintSystem::preCheckExpression(resultExpr, CS.DC);
4071+
ConstraintSystem::preCheckExpression(resultExpr, CS.DC, &CS);
40724072

40734073
// Obtain type of the result expression without applying solutions,
40744074
// because otherwise this might result in leaking of type variables,

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ namespace {
11471147
// If this is a standalone `nil` literal expression e.g.
11481148
// `_ = nil`, let's diagnose it here because solver can't
11491149
// attempt any types for it.
1150-
if (!CS.getTypeChecker().isExprBeingDiagnosed(expr)) {
1150+
if (!CS.isExprBeingDiagnosed(expr)) {
11511151
auto *parentExpr = CS.getParentExpr(expr);
11521152

11531153
// `_ = nil`

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ static bool shouldCheckForPartialApplication(ConstraintSystem &cs,
17081708

17091709
// FIXME(diagnostics): This check should be removed together with
17101710
// expression based diagnostics.
1711-
if (cs.getTypeChecker().isExprBeingDiagnosed(anchor))
1711+
if (cs.isExprBeingDiagnosed(anchor))
17121712
return false;
17131713

17141714
// If this is a reference to instance method marked as 'mutating'

lib/Sema/ConstraintSystem.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3590,7 +3590,8 @@ class ConstraintSystem {
35903590
public:
35913591
/// Pre-check the expression, validating any types that occur in the
35923592
/// expression and folding sequence expressions.
3593-
static bool preCheckExpression(Expr *&expr, DeclContext *dc);
3593+
static bool preCheckExpression(Expr *&expr, DeclContext *dc,
3594+
ConstraintSystem *baseCS = nullptr);
35943595

35953596
/// Solve the system of constraints generated from provided expression.
35963597
///
@@ -3822,6 +3823,36 @@ class ConstraintSystem {
38223823
SmallVectorImpl<unsigned> &Ordering,
38233824
SmallVectorImpl<unsigned> &PartitionBeginning);
38243825

3826+
private:
3827+
/// The set of expressions currently being analyzed for failures.
3828+
llvm::DenseMap<Expr*, Expr*> DiagnosedExprs;
3829+
3830+
public:
3831+
void addExprForDiagnosis(Expr *E1, Expr *Result) {
3832+
DiagnosedExprs[E1] = Result;
3833+
}
3834+
bool isExprBeingDiagnosed(Expr *E) {
3835+
if (DiagnosedExprs.count(E)) {
3836+
return true;
3837+
}
3838+
3839+
if (baseCS && baseCS != this) {
3840+
return baseCS->isExprBeingDiagnosed(E);
3841+
}
3842+
return false;
3843+
}
3844+
Expr *getExprBeingDiagnosed(Expr *E) {
3845+
if (auto *expr = DiagnosedExprs[E]) {
3846+
return expr;
3847+
}
3848+
3849+
if (baseCS && baseCS != this) {
3850+
return baseCS->getExprBeingDiagnosed(E);
3851+
}
3852+
return nullptr;
3853+
}
3854+
3855+
public:
38253856
SWIFT_DEBUG_DUMP;
38263857
SWIFT_DEBUG_DUMPER(dump(Expr *));
38273858

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ namespace {
911911
class PreCheckExpression : public ASTWalker {
912912
ASTContext &Ctx;
913913
DeclContext *DC;
914-
914+
ConstraintSystem *BaseCS;
915+
915916
Expr *ParentExpr;
916917

917918
/// A stack of expressions being walked, used to determine where to
@@ -1053,17 +1054,14 @@ namespace {
10531054
}
10541055

10551056
public:
1056-
PreCheckExpression(DeclContext *dc, Expr *parent)
1057-
: Ctx(dc->getASTContext()), DC(dc), ParentExpr(parent) {}
1057+
PreCheckExpression(DeclContext *dc, Expr *parent, ConstraintSystem *base)
1058+
: Ctx(dc->getASTContext()), DC(dc), BaseCS(base), ParentExpr(parent) {}
10581059

10591060
ASTContext &getASTContext() const { return Ctx; }
10601061

10611062
bool walkToClosureExprPre(ClosureExpr *expr);
10621063

10631064
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
1064-
// FIXME: Remove TypeChecker dependencies below.
1065-
auto &TC = *Ctx.getLegacyGlobalTypeChecker();
1066-
10671065
// If this is a call, record the argument expression.
10681066
if (auto call = dyn_cast<ApplyExpr>(expr)) {
10691067
if (!isa<SelfApplyExpr>(expr)) {
@@ -1160,8 +1158,8 @@ namespace {
11601158
if (expr->isImplicit())
11611159
return finish(true, expr);
11621160

1163-
if (TC.isExprBeingDiagnosed(ParentExpr) ||
1164-
TC.isExprBeingDiagnosed(expr))
1161+
if (BaseCS && (BaseCS->isExprBeingDiagnosed(ParentExpr) ||
1162+
BaseCS->isExprBeingDiagnosed(expr)))
11651163
return finish(true, expr);
11661164

11671165
auto parents = ParentExpr->getParentMap();
@@ -2013,8 +2011,9 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
20132011

20142012
/// Pre-check the expression, validating any types that occur in the
20152013
/// expression and folding sequence expressions.
2016-
bool ConstraintSystem::preCheckExpression(Expr *&expr, DeclContext *dc) {
2017-
PreCheckExpression preCheck(dc, expr);
2014+
bool ConstraintSystem::preCheckExpression(Expr *&expr, DeclContext *dc,
2015+
ConstraintSystem *baseCS) {
2016+
PreCheckExpression preCheck(dc, expr, baseCS);
20182017
// Perform the pre-check.
20192018
if (auto result = expr->walk(preCheck)) {
20202019
expr = result;
@@ -2177,7 +2176,7 @@ Type TypeChecker::typeCheckExpressionImpl(Expr *&expr, DeclContext *dc,
21772176

21782177
// First, pre-check the expression, validating any types that occur in the
21792178
// expression and folding sequence expressions.
2180-
if (ConstraintSystem::preCheckExpression(expr, dc)) {
2179+
if (ConstraintSystem::preCheckExpression(expr, dc, baseCS)) {
21812180
listener.preCheckFailed(expr);
21822181
return Type();
21832182
}

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,6 @@ class TypeChecker final {
544544
private:
545545
ASTContext &Context;
546546

547-
/// The set of expressions currently being analyzed for failures.
548-
llvm::DenseMap<Expr*, Expr*> DiagnosedExprs;
549-
550547
/// If non-zero, warn when a function body takes longer than this many
551548
/// milliseconds to type-check.
552549
///
@@ -1778,16 +1775,6 @@ class TypeChecker final {
17781775
static void checkPropertyWrapperErrorHandling(PatternBindingDecl *binding,
17791776
Expr *expr);
17801777

1781-
void addExprForDiagnosis(Expr *E1, Expr *Result) {
1782-
DiagnosedExprs[E1] = Result;
1783-
}
1784-
bool isExprBeingDiagnosed(Expr *E) {
1785-
return DiagnosedExprs.count(E);
1786-
}
1787-
Expr *getExprBeingDiagnosed(Expr *E) {
1788-
return DiagnosedExprs[E];
1789-
}
1790-
17911778
/// If an expression references 'self.init' or 'super.init' in an
17921779
/// initializer context, returns the implicit 'self' decl of the constructor.
17931780
/// Otherwise, return nil.

0 commit comments

Comments
 (0)