Skip to content

Commit bf58b0c

Browse files
committed
[CSGen] Use special accessor to get a type of VarDecl for solver use
Solver needs to handle invalid declarations but only in "code completion" mode since declaration in question might not be associated with code completion, otherwise (if constraint generation fails) there is going to be no completion results.
1 parent 17cb2d2 commit bf58b0c

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,19 +1241,20 @@ namespace {
12411241
if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
12421242
knownType = CS.getTypeIfAvailable(VD);
12431243
if (!knownType)
1244-
knownType = VD->getType();
1244+
knownType = CS.getVarType(VD);
12451245

12461246
if (knownType) {
1247-
assert(!knownType->isHole());
12481247
// If the known type has an error, bail out.
12491248
if (knownType->hasError()) {
12501249
if (!CS.hasType(E))
12511250
CS.setType(E, knownType);
12521251
return nullptr;
12531252
}
12541253

1255-
// Set the favored type for this expression to the known type.
1256-
CS.setFavoredType(E, knownType.getPointer());
1254+
if (!knownType->hasHole()) {
1255+
// Set the favored type for this expression to the known type.
1256+
CS.setFavoredType(E, knownType.getPointer());
1257+
}
12571258
}
12581259

12591260
// This can only happen when failure diagnostics is trying
@@ -2016,7 +2017,7 @@ namespace {
20162017

20172018
Type externalType;
20182019
if (param->getTypeRepr()) {
2019-
auto declaredTy = param->getType();
2020+
auto declaredTy = CS.getVarType(param);
20202021
externalType = CS.openUnboundGenericTypes(declaredTy, paramLoc);
20212022
} else {
20222023
// Let's allow parameters which haven't been explicitly typed
@@ -3884,7 +3885,7 @@ bool ConstraintSystem::generateConstraints(
38843885
getConstraintLocator(typeRepr));
38853886
setType(typeRepr, backingType);
38863887

3887-
auto propertyType = wrappedVar->getType();
3888+
auto propertyType = getVarType(wrappedVar);
38883889
if (propertyType->hasError())
38893890
return true;
38903891

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,3 +5213,21 @@ void ConstraintSystem::recordFixedRequirement(ConstraintLocator *reqLocator,
52135213
std::make_tuple(GP, reqKind, requirementTy.getPointer()));
52145214
}
52155215
}
5216+
5217+
// Replace any error types encountered with holes.
5218+
Type ConstraintSystem::getVarType(const VarDecl *var) {
5219+
auto type = var->getType();
5220+
5221+
// If this declaration is used as part of a code completion
5222+
// expression, solver needs to glance over the fact that
5223+
// it might be invalid to avoid failing constraint generation
5224+
// and produce completion results.
5225+
if (!isForCodeCompletion())
5226+
return type;
5227+
5228+
return type.transform([&](Type type) {
5229+
if (!type->is<ErrorType>())
5230+
return type;
5231+
return HoleType::get(Context, const_cast<VarDecl *>(var));
5232+
});
5233+
}

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,11 @@ class ConstraintSystem {
28392839
return known->second;
28402840
}
28412841

2842+
/// Retrieve type type of the given declaration to be used in
2843+
/// constraint system, this is better than calling `getType()`
2844+
/// directly because it accounts of constraint system flags.
2845+
Type getVarType(const VarDecl *var);
2846+
28422847
/// Cache the type of the expression argument and return that same
28432848
/// argument.
28442849
template <typename T>

0 commit comments

Comments
 (0)