Skip to content

Commit 17e5ec4

Browse files
authored
Merge pull request #33406 from xedin/adjuments-for-code-completion
[ConstraintSystem] A couple of adjustments to make interaction between solution and code completion simpler
2 parents 2a5af15 + 73262ae commit 17e5ec4

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,46 @@ bool ConstraintSystem::isTypeReference(Expr *E) {
176176
});
177177
}
178178

179+
bool Solution::isTypeReference(Expr *E) const {
180+
return E->isTypeReference(
181+
[&](Expr *expr) -> Type { return simplifyType(getType(expr)); },
182+
[&](Expr *expr) -> Decl * {
183+
ConstraintLocator *locator = nullptr;
184+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(E)) {
185+
locator = getConstraintLocator(UDE, {ConstraintLocator::Member});
186+
}
187+
188+
if (auto *UME = dyn_cast<UnresolvedMemberExpr>(E)) {
189+
locator =
190+
getConstraintLocator(UME, {ConstraintLocator::UnresolvedMember});
191+
}
192+
193+
if (isa<OverloadSetRefExpr>(E))
194+
locator = getConstraintLocator(const_cast<Expr *>(E));
195+
196+
if (locator) {
197+
if (auto selectedOverload = getOverloadChoiceIfAvailable(locator)) {
198+
const auto &choice = selectedOverload->choice;
199+
return choice.getDeclOrNull();
200+
}
201+
}
202+
203+
return nullptr;
204+
});
205+
}
206+
179207
bool ConstraintSystem::isStaticallyDerivedMetatype(Expr *E) {
180208
return E->isStaticallyDerivedMetatype(
181209
[&](Expr *E) -> Type { return simplifyType(getType(E)); },
182210
[&](Expr *E) -> bool { return isTypeReference(E); });
183211
}
184212

213+
bool Solution::isStaticallyDerivedMetatype(Expr *E) const {
214+
return E->isStaticallyDerivedMetatype(
215+
[&](Expr *E) -> Type { return simplifyType(getType(E)); },
216+
[&](Expr *E) -> bool { return isTypeReference(E); });
217+
}
218+
185219
Type ConstraintSystem::getInstanceType(TypeExpr *E) {
186220
if (!hasType(E))
187221
return Type();
@@ -8381,6 +8415,10 @@ Type Solution::getType(ASTNode node) const {
83818415
return cs.getType(node);
83828416
}
83838417

8418+
Type Solution::getResolvedType(ASTNode node) const {
8419+
return simplifyType(getType(node));
8420+
}
8421+
83848422
void Solution::setExprTypes(Expr *expr) const {
83858423
if (!expr)
83868424
return;

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ void ConstraintSystem::solveForCodeCompletion(
14281428

14291429
cs.shrink(expr);
14301430

1431-
if (cs.generateConstraints(expr, DC))
1431+
if (!cs.generateConstraints(expr, DC))
14321432
return;
14331433

14341434
llvm::SmallVector<Solution, 4> solutions;

lib/Sema/ConstraintSystem.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,11 @@ class Solution {
11901190
/// Retrieve the type of the given node, as recorded in this solution.
11911191
Type getType(ASTNode node) const;
11921192

1193+
/// Retrieve the type of the given node as recorded in this solution
1194+
/// and resolve all of the type variables in contains to form a fully
1195+
/// "resolved" concrete type.
1196+
Type getResolvedType(ASTNode node) const;
1197+
11931198
/// Resolve type variables present in the raw type, using generic parameter
11941199
/// types where possible.
11951200
Type resolveInterfaceType(Type type) const;
@@ -1210,6 +1215,16 @@ class Solution {
12101215
: nullptr;
12111216
}
12121217

1218+
/// This method implements functionality of `Expr::isTypeReference`
1219+
/// with data provided by a given solution.
1220+
bool isTypeReference(Expr *E) const;
1221+
1222+
/// Call Expr::isIsStaticallyDerivedMetatype on the given
1223+
/// expression, using a custom accessor for the type on the
1224+
/// expression that reads the type from the Solution
1225+
/// expression type map.
1226+
bool isStaticallyDerivedMetatype(Expr *E) const;
1227+
12131228
SWIFT_DEBUG_DUMP;
12141229

12151230
/// Dump this solution.

0 commit comments

Comments
 (0)