Skip to content

Commit 05b223a

Browse files
committed
[Sema] Switch typeCheckExpression to use ContextualTypeInfo
1 parent 588d42c commit 05b223a

11 files changed

+49
-41
lines changed

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,7 +4296,7 @@ namespace {
42964296
{ Identifier() });
42974297

42984298
auto resultTy = TypeChecker::typeCheckExpression(
4299-
callExpr, cs.DC, valueType, CTP_CannotFail);
4299+
callExpr, cs.DC, /*contextualInfo=*/{valueType, CTP_CannotFail});
43004300
assert(resultTy && "Conversion cannot fail!");
43014301
(void)resultTy;
43024302

@@ -8026,8 +8026,8 @@ static Optional<SolutionApplicationTarget> applySolutionToForEachStmt(
80268026
Expr *convertElementExpr = elementExpr;
80278027
if (TypeChecker::typeCheckExpression(
80288028
convertElementExpr, dc,
8029-
optPatternType,
8030-
CTP_CoerceOperand).isNull()) {
8029+
/*contextualInfo=*/{optPatternType, CTP_CoerceOperand})
8030+
.isNull()) {
80318031
return None;
80328032
}
80338033
elementExpr->setIsPlaceholder(false);

lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,14 @@ struct ContextualTypeInfo {
908908
TypeLoc typeLoc;
909909
ContextualTypePurpose purpose;
910910

911+
ContextualTypeInfo() : typeLoc(TypeLoc()), purpose(CTP_Unused) {}
912+
913+
ContextualTypeInfo(Type contextualTy, ContextualTypePurpose purpose)
914+
: typeLoc(TypeLoc::withoutLoc(contextualTy)), purpose(purpose) {}
915+
916+
ContextualTypeInfo(TypeLoc typeLoc, ContextualTypePurpose purpose)
917+
: typeLoc(typeLoc), purpose(purpose) {}
918+
911919
Type getType() const { return typeLoc.getType(); }
912920
};
913921

lib/Sema/DebuggerTestingTransform.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/Subsystems.h"
3030

3131
#include "TypeChecker.h"
32+
#include "ConstraintSystem.h"
3233

3334
using namespace swift;
3435

@@ -244,7 +245,8 @@ class DebuggerTestingTransform : public ASTWalker {
244245
// TODO: typeCheckExpression() seems to assign types to everything here,
245246
// but may not be sufficient in some cases.
246247
Expr *FinalExpr = ClosureCall;
247-
if (!TypeChecker::typeCheckExpression(FinalExpr, getCurrentDeclContext()))
248+
if (!TypeChecker::typeCheckExpression(FinalExpr, getCurrentDeclContext(),
249+
/*contextualInfo=*/{}))
248250
llvm::report_fatal_error("Could not type-check instrumentation");
249251

250252
// Captures have to be computed after the closure is type-checked. This

lib/Sema/InstrumenterSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool InstrumenterBase::doTypeCheckImpl(ASTContext &Ctx, DeclContext *DC,
119119
DiagnosticSuppression suppression(Ctx.Diags);
120120
ErrorGatherer errorGatherer(Ctx.Diags);
121121

122-
TypeChecker::typeCheckExpression(parsedExpr, DC);
122+
TypeChecker::typeCheckExpression(parsedExpr, DC, /*contextualInfo=*/{});
123123

124124
if (parsedExpr) {
125125
ErrorFinder errorFinder;

lib/Sema/InstrumenterSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "TypeChecker.h"
19-
19+
#include "ConstraintSystem.h"
2020
#include "swift/AST/ASTWalker.h"
2121

2222
namespace swift {

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,8 @@ bool swift::typeCheckExpression(DeclContext *DC, Expr *&parsedExpr) {
962962
parsedExpr = parsedExpr->walk(SanitizeExpr(ctx, /*shouldReusePrecheckedType=*/false));
963963

964964
DiagnosticSuppression suppression(ctx.Diags);
965-
auto resultTy = TypeChecker::typeCheckExpression(parsedExpr, DC, Type(),
966-
CTP_Unused);
965+
auto resultTy =
966+
TypeChecker::typeCheckExpression(parsedExpr, DC, /*contextualInfo=*/{});
967967
return !resultTy;
968968
}
969969

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,10 @@ void constraints::performSyntacticDiagnosticsForTarget(
300300

301301
#pragma mark High-level entry points
302302
Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
303-
Type convertType,
304-
ContextualTypePurpose convertTypePurpose,
303+
ContextualTypeInfo contextualInfo,
305304
TypeCheckExprOptions options) {
306305
SolutionApplicationTarget target(
307-
expr, dc, convertTypePurpose, convertType,
306+
expr, dc, contextualInfo.purpose, contextualInfo.getType(),
308307
options.contains(TypeCheckExprFlags::IsDiscarded));
309308
auto resultTarget = typeCheckExpression(target, options);
310309
if (!resultTarget) {
@@ -422,9 +421,10 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
422421
DeclContext *DC, Type paramType,
423422
bool isAutoClosure) {
424423
assert(paramType && !paramType->hasError());
425-
return typeCheckExpression(
426-
defaultValue, DC, paramType,
427-
isAutoClosure ? CTP_AutoclosureDefaultParameter : CTP_DefaultParameter);
424+
return typeCheckExpression(defaultValue, DC, /*contextualInfo=*/
425+
{paramType, isAutoClosure
426+
? CTP_AutoclosureDefaultParameter
427+
: CTP_DefaultParameter});
428428
}
429429

430430
bool TypeChecker::typeCheckBinding(
@@ -593,7 +593,8 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
593593
// If this expression is already typechecked and has type Bool, then just
594594
// re-typecheck it.
595595
if (expr->getType() && expr->getType()->isBool()) {
596-
auto resultTy = TypeChecker::typeCheckExpression(expr, dc);
596+
auto resultTy =
597+
TypeChecker::typeCheckExpression(expr, dc, /*contextualInfo=*/{});
597598
return !resultTy;
598599
}
599600

@@ -602,8 +603,8 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
602603
return true;
603604

604605
auto resultTy = TypeChecker::typeCheckExpression(
605-
expr, dc, boolDecl->getDeclaredInterfaceType(),
606-
CTP_Condition);
606+
expr, dc,
607+
/*contextualInfo=*/{boolDecl->getDeclaredInterfaceType(), CTP_Condition});
607608
return !resultTy;
608609
}
609610

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,9 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
11991199

12001200
{
12011201
Expr *exprToCheck = prevValue;
1202-
if (TypeChecker::typeCheckExpression(exprToCheck, ED,
1203-
rawTy,
1204-
CTP_EnumCaseRawValue)) {
1202+
if (TypeChecker::typeCheckExpression(
1203+
exprToCheck, ED,
1204+
/*contextualInfo=*/{rawTy, CTP_EnumCaseRawValue})) {
12051205
TypeChecker::checkEnumElementEffects(elt, exprToCheck);
12061206
}
12071207
}

lib/Sema/TypeCheckStmt.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
781781
}
782782
}
783783

784-
auto exprTy = TypeChecker::typeCheckExpression(E, DC,
785-
ResultTy,
786-
ctp, options);
784+
auto exprTy =
785+
TypeChecker::typeCheckExpression(E, DC, {ResultTy, ctp}, options);
787786
RS->setResult(E);
788787

789788
if (!exprTy) {
@@ -844,8 +843,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
844843
}
845844

846845
TypeChecker::typeCheckExpression(exprToCheck, DC,
847-
contextType,
848-
contextTypePurpose);
846+
{contextType, contextTypePurpose});
849847

850848
// Propagate the change into the inout expression we stripped before.
851849
if (inout) {
@@ -867,10 +865,9 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
867865
Type exnType = getASTContext().getErrorDecl()->getDeclaredInterfaceType();
868866
if (!exnType) return TS;
869867

870-
TypeChecker::typeCheckExpression(E, DC, exnType,
871-
CTP_ThrowStmt);
868+
TypeChecker::typeCheckExpression(E, DC, {exnType, CTP_ThrowStmt});
872869
TS->setSubExpr(E);
873-
870+
874871
return TS;
875872
}
876873

@@ -885,7 +882,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
885882
TypeChecker::typeCheckDecl(DS->getTempDecl());
886883

887884
Expr *theCall = DS->getCallExpr();
888-
TypeChecker::typeCheckExpression(theCall, DC);
885+
TypeChecker::typeCheckExpression(theCall, DC, /*contextualInfo=*/{});
889886
DS->setCallExpr(theCall);
890887

891888
return DS;
@@ -1159,7 +1156,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
11591156
Stmt *visitSwitchStmt(SwitchStmt *switchStmt) {
11601157
// Type-check the subject expression.
11611158
Expr *subjectExpr = switchStmt->getSubjectExpr();
1162-
auto resultTy = TypeChecker::typeCheckExpression(subjectExpr, DC);
1159+
auto resultTy = TypeChecker::typeCheckExpression(subjectExpr, DC,
1160+
/*contextualInfo=*/{});
11631161
auto limitExhaustivityChecks = !resultTy;
11641162
if (Expr *newSubjectExpr =
11651163
TypeChecker::coerceToRValue(getASTContext(), subjectExpr))
@@ -1519,7 +1517,7 @@ void StmtChecker::typeCheckASTNode(ASTNode &node) {
15191517
}
15201518

15211519
auto resultTy =
1522-
TypeChecker::typeCheckExpression(E, DC, Type(), CTP_Unused, options);
1520+
TypeChecker::typeCheckExpression(E, DC, /*contextualInfo=*/{}, options);
15231521

15241522
// If a closure expression is unused, the user might have intended to write
15251523
// "do { ... }".
@@ -1619,9 +1617,8 @@ static Expr* constructCallToSuperInit(ConstructorDecl *ctor,
16191617
r = new (Context) TryExpr(SourceLoc(), r, Type(), /*implicit=*/true);
16201618

16211619
DiagnosticSuppression suppression(ctor->getASTContext().Diags);
1622-
auto resultTy =
1623-
TypeChecker::typeCheckExpression(r, ctor, Type(), CTP_Unused,
1624-
TypeCheckExprFlags::IsDiscarded);
1620+
auto resultTy = TypeChecker::typeCheckExpression(
1621+
r, ctor, /*contextualInfo=*/{}, TypeCheckExprFlags::IsDiscarded);
16251622
if (!resultTy)
16261623
return nullptr;
16271624

lib/Sema/TypeCheckStorage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "CodeSynthesis.h"
1919
#include "TypeChecker.h"
20+
#include "ConstraintSystem.h"
2021
#include "TypeCheckAvailability.h"
2122
#include "TypeCheckDecl.h"
2223
#include "TypeCheckType.h"
@@ -916,7 +917,8 @@ static Expr *buildStorageReference(AccessorDecl *accessor,
916917
// FIXME: Since we're not resolving overloads or anything, we should be
917918
// building fully type-checked AST above; we already have all the
918919
// information that we need.
919-
if (!TypeChecker::typeCheckExpression(lookupExpr, accessor))
920+
if (!TypeChecker::typeCheckExpression(lookupExpr, accessor,
921+
/*contextualInfo=*/{}))
920922
return nullptr;
921923

922924
// Make sure we produce an lvalue only when desired.

lib/Sema/TypeChecker.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace constraints {
5151
class Solution;
5252
class SolutionApplicationTarget;
5353
class SolutionResult;
54+
struct ContextualTypeInfo;
5455
}
5556

5657
/// Special-case type checking semantics for certain declarations.
@@ -549,21 +550,18 @@ Expr *findLHS(DeclContext *DC, Expr *E, Identifier name);
549550
/// \param expr The expression to type-check, which will be modified in
550551
/// place.
551552
///
552-
/// \param convertTypePurpose When convertType is specified, this indicates
553+
/// \param contextualInfo The type that the expression is being converted to,
554+
/// or null if the expression is standalone. When convertType is specified, this indicates
553555
/// what the conversion is doing. This allows diagnostics generation to
554556
/// produce more specific and helpful error messages when the conversion fails
555557
/// to be possible.
556558
///
557-
/// \param convertType The type that the expression is being converted to,
558-
/// or null if the expression is standalone.
559-
///
560559
/// \param options Options that control how type checking is performed.
561560
///
562561
/// \returns The type of the top-level expression, or Type() if an
563562
/// error occurred.
564563
Type typeCheckExpression(Expr *&expr, DeclContext *dc,
565-
Type convertType = Type(),
566-
ContextualTypePurpose convertTypePurpose = CTP_Unused,
564+
constraints::ContextualTypeInfo contextualInfo,
567565
TypeCheckExprOptions options = TypeCheckExprOptions());
568566

569567
Optional<constraints::SolutionApplicationTarget>

0 commit comments

Comments
 (0)