[5.5][CodeComplete] Show completions from constrained protocol extension #37080
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cherry-picks #37065
Consider the following example.
With SE-0299 accepted, we should be suggesting
.one
.For that, we need to consider the extension applied when performing the unresolved dot code completion with a primary archetype that conforms to
FontStyle
.However, in the following case, which performs an unresolved dot completion on the same base type, we don't want to suggest
.one
because that would requireT == FontStyleOne
, which we can’t assume.Since the constraint system cannot tell us how it came up with the archetype, we need to apply a heuristic to differentiate between the two cases.
What seems to work fine in most cases, is to determine if
T
referes to a generic parameter that is visible from the decl context we are completing in (i.e. the decl context we are completing in is a child context of the context thatT
is declared in). If it is not, thenT
cannot be the type of a variable we are completing on. Thus, we are in the first case and we should consider all extensions ofFontStyle
because we can further specializeT
by picking a more concrete type.Otherwise
T
may be the type of a variable we are completing on and we should be conservative and only suggest those extensions whose requirements are fulfilled byT
.Since this is just a heuristic, there are some corner cases, where we aren’t suggesting constrainted extensions although we should. For example, in the following example the call to
testRecursive
doesn’t useT
and we should thus suggestone
. But by the rules described above we detect thatT
is accessible at the call and thus don’t apply extension whose requirements aren’t satisfied.Similar completion issues also occurred without SE-0299 in more complicated, generic scenarios.