Commit Graph

43 Commits

Author SHA1 Message Date
Hamish Knight
f7e9222145 [Completion] Tweak assert for subscript call pattern
We may not have a semantic context kind if the
subscript decl is missing, for cases such as
the implicit `keyPath` subscript.

rdar://139333904
2024-11-15 17:44:27 +00:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Hamish Knight
1d20392b36 [Completion] Fix trailing closure index check
`Res.FirstTrailingClosureIndex` is an index into
the argument list, so comparing it against a
parameter index is wrong. Instead, compare it
against the argument index of the completion token,
which is what we want to be checking. This ensures
we don't try and offer argument completions for
non-function default arguments.

rdar://127760308
2024-06-06 22:05:40 +01:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Alex Hoppen
278ecef284 [CodeCompletion] Don’t report a call pattern as convertible if its result type doesn’t match
To compute the expected type of a call pattern (which is the return type of the function if that call pattern is being used), we called `getTypeForCompletion` for the entire call, in the same way that we do for the code completion token. However, this pattern does not generally work. For the code completion token it worked because the code completion expression doesn’t have an inherent type and it inherits the type solely from its context. Calls, however, have an inherent return type and that type gets assigned as the `typeForCompletion`.

Implement targeted checks for the two most common cases where an expected type exists: If the call that we suggest call patterns for is itself an argument to another function or if it is used in a place that has a contextual type in the constraint system (eg. a variable binding or a `return` statement). This means that we no longer return `Convertible` for call patterns in some more complex scenarios. But given that this information was computed based on incorrect results and that in those cases all call patterns had a `Convertible` type relation, I think that’s acceptable. Fixing this would require recording more information in the constraints system, which is out-of-scope for now.
2024-04-10 16:06:47 -07:00
Alex Hoppen
0b580f7155 Merge pull request #71587 from ahoppen/ahoppen/nested-call-completion
[CodeCompletion] Always suggest call pattern for nested function calls
2024-02-26 09:14:15 -08:00
Alex Hoppen
17520dcf6d [CodeCompletion] Always suggest call pattern for nested function calls
When completing in cases like `bar(arg: foo(|, option: 1)`, we don’t know if `option` belongs to the call to `foo` or `bar`. Be defensive and also suggest the signature.
2024-02-21 18:53:07 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Hamish Knight
d7fc22aaca [IDE] Simplify isImplicitSingleExpressionReturn
Use the generalized implied result logic, and
rename to `isImpliedResult` since that's really
what we're querying here, and it needs to handle
implicit-last-exprs if enabled.
2024-02-07 18:14:23 +00:00
Alex Hoppen
695e69e09e [CodeComplete] Suggest single argument labels if code completion is invoked at start of function call with exiting parameters
This removes the distinction between argument completions and postfix expr paren completions, which was meaningless since solver-based completion.

It then determines whether to suggest the entire function call pattern (with all argument labels) or only a single argument based on whether there are any existing arguments in the call.

For this to work properly, we need to improve parser recovery a little bit so that it parsers arguments after the code completion token properly.

This should make call pattern heuristics obsolete.

rdar://84809503
2024-01-22 12:21:04 -08:00
Alex Hoppen
a1bfb510e1 [CodeCompletion] Migrate labeled trailing closure completions to solver-based
rdar://113472967
2023-08-24 15:41:36 -07:00
Alex Hoppen
72cadecf21 [CodeCompletion] Split result delivery into a result colleciton and consumer phase
This will allow us to run two different completion kinds and deliver results from both of them.

Also: Compute a unified type context for global lookup. Previously, we always used the expected type context of the last lookup. But really, we should be considering all possible types from all constraint system solutions when computing code completion results from the cache.
2023-08-24 15:41:36 -07:00
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Alex Hoppen
597c0dc90a [SourceKit] Fix crash when completing a parameter of an unapplied instance member reference 2023-07-07 21:16:59 +02:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Alex Hoppen
9dd87e9c4e [CodeCompletion] Fix ASAN failure when completing in parameter packs
Whe completing in parameter packs, we were calling `getParameterAt` with `Res.FuncDeclRef`. But the substitution map in `Res.FuncDeclRef` contained type variables that were allocated in the constraint system’s arena. And that arena had been freed when we call this from `deliverResults`.

The fix is to compute the optional parameters in advance in `sawSolutionImpl`

rdar://109093909
2023-05-10 15:24:19 -07:00
Alex Hoppen
3ee4668532 [CodeComplete] Fix a nullptr crash
If `FuncDeclRef` is `null` we shouldn’t be trying to get any parameters from it.
2023-05-09 13:41:42 -07:00
Alex Hoppen
6d8b1f42c7 [CodeCompletion] Fix a crash when completing an argument to a function taking a parameter pack
We previously asserted that for a call the function type had the same number of parameters as the declaration. But that’s not true for parameter packs anymore because the parameter pack will be exploded in the function type to account for passing multiple arguments to the pack.

To fix this, use `ConcreteDeclRef` instead of a `ValueDecl`, which has a substitution map and is able to account for the exploded parameter packs when accessed using `getParameterAt`.

rdar://100066716
2023-05-05 15:18:57 -07:00
Alex Hoppen
788c9f6fbf Merge pull request #63857 from ahoppen/ahoppen/solution-specific-var-types
[IDE] Set solution-specific variable types as interface types
2023-03-08 08:30:55 -08:00
Alex Hoppen
614679f73d [IDE] Set solution-specific variable types as interface types
Setting the interface type of a variable, just to reset it to a null type is actually really gross. But quite a few methods further down in the generation of code completion results (such as USR generation) need to get a variable’s type and passing them along in a separate map would be really invasive. So this seems like the least bad solution to me.
2023-03-07 17:48:41 -08:00
Hamish Knight
54c2fbf6f3 [CS] NFC: Add Solution::getTargetFor 2023-03-07 15:16:34 +00:00
Hamish Knight
2976edbe20 [CS] Rename SolutionApplicationTarget -> SyntacticElementTarget 2023-03-06 20:54:06 +00:00
Alex Hoppen
470dcde915 [CodeCompletion] Don't show call pattern completions for overridden functions 2023-02-22 22:33:42 +01:00
Alex Hoppen
390d7a6882 [CodeCompletion] Compute type relations for the function calls selected by ArgumentCompletion 2023-02-22 22:33:40 +01:00
Alex Hoppen
f4986f9b3e [CodeCompletion] Don't consider calls to initializers on metatypes in argument completion 2023-02-22 22:31:08 +01:00
Alex Hoppen
062eccbbbb [CodeCompletion] Hide underscored code completion results in argument completion 2023-02-22 22:31:08 +01:00
Alex Hoppen
16af937919 Merge pull request #63515 from ahoppen/ahoppen/look-through-optional
[CodeCompletion] Look through optional when determining the function type of a called overload
2023-02-17 17:24:19 +01:00
Alex Hoppen
754e9d7d54 [CodeCompletion] Fix issue when completing inside a switch in a closure 2023-02-14 09:40:56 +01:00
Alex Hoppen
5d986ecdc6 [CodeCompletion] Look through optional when determining the function type of a called overload
rdar://97339983
2023-02-08 17:30:02 +01:00
Alex Hoppen
1fb439382e [IDE] Move getSelectedOverloadInfo to be a function on Solution
We will need this for solver-based cursor info.
2022-12-05 23:52:04 +01:00
Doug Gregor
248b72bbff Track the pre-adjusted "reference" type for declaration reference. 2022-06-30 17:00:32 -07:00
Alex Hoppen
11266ebb25 [CodeCompletion] Look through @dynamicMemberLookup key path functions
This enables us to provide proper code completion for the function that is being referenced by the dynamic key path.

rdar://79092371
2022-05-08 12:58:24 +02:00
Alex Hoppen
2d7dbd3f4e [CodeCompletion] Check if type is function before making call pattern suggestions 2022-03-25 10:42:08 +01:00
Alex Hoppen
080410521c [CodeCompletion] Don't fail in ArgumentCompltion if no overload was found 2022-03-25 10:42:08 +01:00
Alex Hoppen
2392117a2d [CodeCompletion] Record solution-specific variable types in argument completion
We are doing global completion for function arguments, so we should also record the variable types determined by the solution.
2022-03-25 10:42:08 +01:00
Alex Hoppen
f2230502e4 [CodeCompletion] Compute semantic context based on where the called fucntion is defined 2022-03-25 09:36:06 +01:00
Alex Hoppen
237376b980 [CodeCompletion] Call into simplifyTypeImpl when simplifying a type for code completion
This makes sure we e.g. resolve dependent member types after we perform type variable to generic parameter substitutions for code completion
2022-03-25 09:36:05 +01:00
Alex Hoppen
95d39842a9 [CodeCompletion] Don't record solution results twice in ExprCompletion
This fixes a performance problem where we were sometimes reporting >1000 solutions but all the relevant parameters of the result were equal.
2022-03-24 09:11:39 +01:00
Alex Hoppen
3597652960 [CodeCompletion] Make sawSolution non-final and override sawSolutionImpl from subclasses
This eliminates a source of bugs if subclasses of `TypeCheckCompletionCallback` forget to call the superclass’s implementation of `sawSolution` from their overridden method.
2022-03-21 23:41:26 +01:00
Alex Hoppen
03d819f442 [CodeCompletion] Check whether surrounding context supports async in all solver-based completion kinds 2022-03-21 20:04:32 +01:00
Alex Hoppen
17eb6ea1a2 [CodeCompletion] Unify logic to retrieve completion expr type for all completion callbacks 2022-03-21 20:04:32 +01:00
Alex Hoppen
e2a62f1a60 [CodeCompletion] Migrate expression completions to solver-based 2022-03-21 13:00:33 +01:00
Alex Hoppen
f538d33e5f [CodeCompletion][Sema] Migrate CallArgurment position completion to the solver-based implementation
This hooks up call argument position completion to the typeCheckForCodeCompletion API to generate completions from all the solutions the constraint solver produces (even those requiring fixes), rather than relying on a single solution being applied to the AST (if any).

Co-authored-by: Nathan Hawes <nathan.john.hawes@gmail.com>
2022-03-17 15:15:54 +01:00