Skip to content

Commit b663c9b

Browse files
authored
Merge pull request swiftlang#32561 from slavapestov/code-completion-entry-points
Sema: Move code completion entry points to TypeCheckCodeCompletion.cpp
2 parents 7f8c679 + b7cea44 commit b663c9b

File tree

6 files changed

+383
-351
lines changed

6 files changed

+383
-351
lines changed

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_swift_host_library(swiftSema STATIC
4040
TypeCheckAvailability.cpp
4141
TypeCheckCaptures.cpp
4242
TypeCheckCircularity.cpp
43+
TypeCheckCodeCompletion.cpp
4344
TypeCheckConstraints.cpp
4445
TypeCheckDecl.cpp
4546
TypeCheckDeclObjC.cpp

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5777,16 +5777,9 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, AnyFunctionType *funcType,
57775777
return cs.cacheType(arg);
57785778
}
57795779

5780-
static ClosureExpr *getClosureLiteralExpr(Expr *expr) {
5780+
static bool isClosureLiteralExpr(Expr *expr) {
57815781
expr = expr->getSemanticsProvidingExpr();
5782-
5783-
if (auto *captureList = dyn_cast<CaptureListExpr>(expr))
5784-
return captureList->getClosureBody();
5785-
5786-
if (auto *closure = dyn_cast<ClosureExpr>(expr))
5787-
return closure;
5788-
5789-
return nullptr;
5782+
return (isa<CaptureListExpr>(expr) || isa<ClosureExpr>(expr));
57905783
}
57915784

57925785
/// If the expression is an explicit closure expression (potentially wrapped in
@@ -7329,7 +7322,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
73297322
}
73307323

73317324
// An immediate application of a closure literal is always noescape.
7332-
if (getClosureLiteralExpr(fn)) {
7325+
if (isClosureLiteralExpr(fn)) {
73337326
if (auto fnTy = cs.getType(fn)->getAs<FunctionType>()) {
73347327
fnTy = cast<FunctionType>(
73357328
fnTy->withExtInfo(fnTy->getExtInfo().withNoEscape()));

lib/Sema/CSGen.cpp

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,30 +3776,26 @@ namespace {
37763776
}
37773777
};
37783778

3779-
/// AST walker that "sanitizes" an expression for the
3780-
/// constraint-based type checker.
3779+
/// AST walker that "sanitizes" an expression for re-typechecking during
3780+
/// code completion.
37813781
///
3782-
/// This is necessary because Sema fills in too much type information before
3783-
/// the type-checker runs, causing redundant work, and for expression that
3784-
/// have already been typechecked and may contain unhandled AST nodes.
3785-
///
3786-
/// FIXME: Remove this one we no longer re-type check expressions during
3787-
/// diagnostics and code completion.
3782+
/// FIXME: Remove this.
37883783
class SanitizeExpr : public ASTWalker {
3789-
ConstraintSystem &CS;
3784+
ASTContext &C;
3785+
bool ShouldReusePrecheckedType;
37903786
llvm::SmallDenseMap<OpaqueValueExpr *, Expr *, 4> OpenExistentials;
37913787

37923788
public:
3793-
SanitizeExpr(ConstraintSystem &cs) : CS(cs){ }
3794-
3795-
ASTContext &getASTContext() const { return CS.getASTContext(); }
3789+
SanitizeExpr(ASTContext &C,
3790+
bool shouldReusePrecheckedType)
3791+
: C(C), ShouldReusePrecheckedType(shouldReusePrecheckedType) { }
37963792

37973793
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
37983794
while (true) {
37993795

38003796
// If we should reuse pre-checked types, don't sanitize the expression
38013797
// if it's already type-checked.
3802-
if (CS.shouldReusePrecheckedType() && expr->getType())
3798+
if (ShouldReusePrecheckedType && expr->getType())
38033799
return { false, expr };
38043800

38053801
// OpenExistentialExpr contains OpaqueValueExpr in its sub expression.
@@ -3892,7 +3888,7 @@ namespace {
38923888
};
38933889

38943890
if (TE->isImplicit() && TE->getNumElements() == 1 &&
3895-
TE->getElementName(0) == getASTContext().Id_dynamicMember &&
3891+
TE->getElementName(0) == C.Id_dynamicMember &&
38963892
isImplicitKeyPathExpr(TE->getElement(0))) {
38973893
auto *keyPathExpr = cast<KeyPathExpr>(TE->getElement(0));
38983894
auto *componentExpr = keyPathExpr->getParsedPath();
@@ -3957,15 +3953,15 @@ namespace {
39573953
argList.labels[0].empty() &&
39583954
!isa<VarargExpansionExpr>(argList.args[0])) {
39593955
auto *result =
3960-
new (getASTContext()) ParenExpr(argList.lParenLoc,
3961-
argList.args[0],
3962-
argList.rParenLoc,
3963-
argList.hasTrailingClosure);
3956+
new (C) ParenExpr(argList.lParenLoc,
3957+
argList.args[0],
3958+
argList.rParenLoc,
3959+
argList.hasTrailingClosure);
39643960
result->setImplicit();
39653961
return result;
39663962
}
39673963

3968-
return TupleExpr::create(getASTContext(),
3964+
return TupleExpr::create(C,
39693965
argList.lParenLoc,
39703966
argList.args,
39713967
argList.labels,
@@ -3976,32 +3972,18 @@ namespace {
39763972
}
39773973

39783974
Expr *walkToExprPost(Expr *expr) override {
3979-
if (CS.hasType(expr)) {
3980-
Type type = CS.getType(expr);
3981-
if (type->hasOpenedExistential()) {
3982-
type = type.transform([&](Type type) -> Type {
3983-
if (auto archetype = type->getAs<OpenedArchetypeType>())
3984-
return archetype->getOpenedExistentialType();
3985-
return type;
3986-
});
3987-
CS.setType(expr, type);
3988-
// Set new type to the expression directly.
3989-
expr->setType(type);
3990-
}
3991-
}
3992-
39933975
assert(!isa<ImplicitConversionExpr>(expr) &&
39943976
"ImplicitConversionExpr should be eliminated in walkToExprPre");
39953977

39963978
auto buildMemberRef = [&](Type memberType, Expr *base, SourceLoc dotLoc,
39973979
ConcreteDeclRef member, DeclNameLoc memberLoc,
39983980
bool implicit) -> Expr * {
3999-
auto *memberRef = new (getASTContext())
3981+
auto *memberRef = new (C)
40003982
MemberRefExpr(base, dotLoc, member, memberLoc, implicit);
40013983

40023984
if (memberType) {
40033985
memberRef->setType(memberType);
4004-
return CS.cacheType(memberRef);
3986+
return memberRef;
40053987
}
40063988

40073989
return memberRef;
@@ -4188,7 +4170,8 @@ namespace {
41884170
static Expr *generateConstraintsFor(ConstraintSystem &cs, Expr *expr,
41894171
DeclContext *DC) {
41904172
// Remove implicit conversions from the expression.
4191-
expr = expr->walk(SanitizeExpr(cs));
4173+
expr = expr->walk(SanitizeExpr(cs.getASTContext(),
4174+
cs.shouldReusePrecheckedType()));
41924175

41934176
// Walk the expression, generating constraints.
41944177
ConstraintGenerator cg(cs, DC);

0 commit comments

Comments
 (0)