[Completion] Don't suggest nested type in where clause

We allow the unqualifed use of the enclosing nominal type in a where
clause, but only when it isn't a nested type, e.g:

```
struct S<T> {
  typealias T = T
  struct R<U> {
    typealias U = U
  }
}
extension S where S.T == Int {}   // allowed
extension S.R where R.U == Int {} // not allowed
```

Tweak the completion logic such that we don't suggest the type for
the nested case, instead it must be qualified.
This commit is contained in:
Hamish Knight
2025-02-25 14:49:12 +00:00
parent 0fc825b6fb
commit f73054ad10
2 changed files with 9 additions and 10 deletions

View File

@@ -3006,11 +3006,14 @@ void CompletionLookup::getGenericRequirementCompletions(
/*includeDerivedRequirements*/ false,
/*includeProtocolExtensionMembers*/ true);
// We not only allow referencing nested types/typealiases directly, but also
// qualified by the current type. Thus also suggest current self type so the
// qualified by the current type, as long as it's a top-level type (nested
// types need to be qualified). Thus also suggest current self type so the
// user can do a memberwise lookup on it.
if (auto SelfType = typeContext->getSelfNominalTypeDecl()) {
addNominalTypeRef(SelfType, DeclVisibilityKind::LocalDecl,
DynamicLookupInfo());
if (auto *NTD = typeContext->getSelfNominalTypeDecl()) {
if (!NTD->getDeclContext()->isTypeContext()) {
addNominalTypeRef(NTD, DeclVisibilityKind::LocalDecl,
DynamicLookupInfo());
}
}
// Self is also valid in all cases in which it can be used in function