@@ -22,7 +22,7 @@ using namespace swift;
22
22
using namespace constraints ;
23
23
24
24
void ConstraintSystem::PotentialBindings::inferTransitiveBindings (
25
- const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
25
+ ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
26
26
const llvm::SmallDenseMap<TypeVariableType *,
27
27
ConstraintSystem::PotentialBindings>
28
28
&inferredBindings) {
@@ -144,7 +144,7 @@ isUnviableDefaultType(Type defaultType,
144
144
}
145
145
146
146
void ConstraintSystem::PotentialBindings::inferDefaultTypes (
147
- const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
147
+ ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
148
148
auto isDirectRequirement = [&](Constraint *constraint) -> bool {
149
149
if (auto *typeVar = constraint->getFirstType ()->getAs <TypeVariableType>()) {
150
150
auto *repr = cs.getRepresentative (typeVar);
@@ -300,7 +300,7 @@ void ConstraintSystem::PotentialBindings::inferDefaultTypes(
300
300
}
301
301
302
302
void ConstraintSystem::PotentialBindings::finalize (
303
- const ConstraintSystem &cs,
303
+ ConstraintSystem &cs,
304
304
const llvm::SmallDenseMap<TypeVariableType *,
305
305
ConstraintSystem::PotentialBindings>
306
306
&inferredBindings) {
@@ -377,6 +377,15 @@ void ConstraintSystem::PotentialBindings::finalize(
377
377
PotentiallyIncomplete = true ;
378
378
}
379
379
380
+ // If this type variable is associated with a code completion token
381
+ // and it failed to infer any bindings let's adjust hole's locator
382
+ // to point to a code completion token to avoid attempting to "fix"
383
+ // this problem since its rooted in the fact that constraint system
384
+ // is under-constrained.
385
+ if (AssociatedCodeCompletionToken) {
386
+ locator = cs.getConstraintLocator (AssociatedCodeCompletionToken);
387
+ }
388
+
380
389
addPotentialBinding (PotentialBinding::forHole (TypeVar, locator));
381
390
}
382
391
@@ -620,8 +629,7 @@ bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
620
629
}
621
630
622
631
ConstraintSystem::PotentialBindings
623
- ConstraintSystem::inferBindingsFor (TypeVariableType *typeVar,
624
- bool finalize) const {
632
+ ConstraintSystem::inferBindingsFor (TypeVariableType *typeVar, bool finalize) {
625
633
assert (typeVar->getImpl ().getRepresentative (nullptr ) == typeVar &&
626
634
" not a representative" );
627
635
assert (!typeVar->getImpl ().getFixedType (nullptr ) && " has a fixed type" );
@@ -769,6 +777,18 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
769
777
770
778
result.InvolvesTypeVariables = true ;
771
779
780
+ // If current type variable is associated with a code completion token
781
+ // it's possible that it doesn't have enough contextual information
782
+ // to be resolved to anything, so let's note that fact in the potential
783
+ // bindings and use it when forming a hole if there are no other bindings
784
+ // available.
785
+ if (auto *locator = bindingTypeVar->getImpl ().getLocator ()) {
786
+ if (locator->directlyAt <CodeCompletionExpr>()) {
787
+ result.AssociatedCodeCompletionToken = locator->getAnchor ();
788
+ result.PotentiallyIncomplete = true ;
789
+ }
790
+ }
791
+
772
792
if (constraint->getKind () == ConstraintKind::Subtype &&
773
793
kind == AllowedBindingKind::Subtypes) {
774
794
result.SubtypeOf .insert (bindingTypeVar);
@@ -829,7 +849,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
829
849
// / representative type variable, along with flags indicating whether
830
850
// / those types should be opened.
831
851
bool ConstraintSystem::PotentialBindings::infer (
832
- const ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &exactTypes,
852
+ ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &exactTypes,
833
853
Constraint *constraint) {
834
854
switch (constraint->getKind ()) {
835
855
case ConstraintKind::Bind:
@@ -1222,6 +1242,21 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
1222
1242
auto *dstLocator = TypeVar->getImpl ().getLocator ();
1223
1243
auto *srcLocator = Binding.getLocator ();
1224
1244
1245
+ // FIXME: This check could be turned into an assert once
1246
+ // all code completion kinds are ported to use
1247
+ // `TypeChecker::typeCheckForCodeCompletion` API.
1248
+ if (cs.isForCodeCompletion ()) {
1249
+ // If the hole is originated from code completion expression
1250
+ // let's not try to fix this, anything connected to a
1251
+ // code completion is allowed to be a hole because presence
1252
+ // of a code completion token makes constraint system
1253
+ // under-constrained due to e.g. lack of expressions on the
1254
+ // right-hand side of the token, which are required for a
1255
+ // regular type-check.
1256
+ if (dstLocator->directlyAt <CodeCompletionExpr>())
1257
+ return None;
1258
+ }
1259
+
1225
1260
unsigned defaultImpact = 1 ;
1226
1261
1227
1262
if (auto *GP = TypeVar->getImpl ().getGenericParameter ()) {
0 commit comments