Commit Graph

117 Commits

Author SHA1 Message Date
Hamish Knight
e631a37d0b [Completion] Avoid using unbound contextual type in argument completion
Match the logic in `getTypeForCompletion` and avoid using an unbound
generic type as the expected type for a completion.

rdar://155420395
2025-07-09 12:57:36 +01:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Slava Pestov
b0303bb5a1 Sema: Restore old behavior in ConstraintSystem::openType()
My change to preserve type sugar in type transforms introduced a
performance regression here, because openType() is called for
every disjunction choice.

My hope is to optimize the TypeAliasType representation and remove
this at some point, but for now, let's just restore the old
desugaring behavior in this case.
2024-10-08 11:53:45 -04:00
Slava Pestov
074684fe06 AST: Use TypeTransform::transformSubMap() to transform TypeAliasType 2024-08-21 13:21:47 -04:00
Alexander Cyon
a80b8ae06f [test/IDE] Fix typos 2024-08-08 19:35:00 +02: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
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
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
a5a17aa955 [tests] Add a %batch-code-completion lit substitution
I could never remember the command to run batch code completion tests. Add a lit substitution for it.
2023-09-18 13:57:49 -07:00
Alex Hoppen
527a79e9f8 [IDE] Set constraint system options from solveForCodeCompletion in applyResultBuilderBodyTransform when solving for code completion 2023-07-07 19:51:01 +02:00
Alex Hoppen
c7e0bfae02 [IDE] Adjust test cases for migrating all completion kinds to solver-based 2023-07-07 19:51:01 +02:00
Alex Hoppen
c385fe5e87 [CodeCompletion] Migrate PostfixExprParen to solver-based 2023-07-07 19:50:46 +02:00
Alex Hoppen
6cec68e302 [IDE] Ignore score kinds that represent implicit conversions when solving for code completion
Ignore conversion score increases during code completion to make sure we don't filter solutions that might start receiving the best score based on a choice of the code completion token.
2023-07-07 19:50:46 +02:00
Anthony Latsis
3f629315a9 Merge pull request #65731 from AnthonyLatsis/always-print-any
ASTPrinter: Turn on explicit `any` printing for everything and remove the option to disable it
2023-05-16 03:27:36 +03:00
Anthony Latsis
7f6d3bcd41 ASTPrinter: Turn on explicit any printing for everything and remove the option to disable it 2023-05-13 02:55:49 +03: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
edfd3fde38 Merge pull request #64506 from ahoppen/ahoppen/no-begin-end-completions
[IDE] Remove "Begin completions" and "End completions" from test cases
2023-03-22 17:21:37 -07:00
Alex Hoppen
32eff21977 [IDE] Remove "Begin completions" and "End completions" from test cases
These test lines weren't actually providing any value and were annoying to write. Let's jut remove them.
2023-03-22 09:07:17 -07:00
Alex Hoppen
35871c2af7 [CodeCompletion] Fix assertion failure if completing in a macro call argument
rdar://106966610
2023-03-21 13:41:45 -07:00
Alex Hoppen
f129c55adc [IDE] Remove special handling for multi-statement closures 2023-02-28 16:25:12 +01:00
Alex Hoppen
390d7a6882 [CodeCompletion] Compute type relations for the function calls selected by ArgumentCompletion 2023-02-22 22:33:40 +01:00
Anthony Latsis
8c1f391878 Gardening: Migrate test suite to GH issues: IDE 2022-08-31 03:00:06 +03: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
Rintaro Ishizaki
4e621408aa [CodeCompletion] Ensure synthesized members are available before lookup
In ExprContextAnalyzer, when looking up members, some implicit
members weren't populated. Ensure all implicit members available by
force synthesizing them.

rdar://89773376
2022-04-15 11:47:36 -07:00
Alex Hoppen
5d01a097e1 [CodeCompletion] Don't distinguish convertible and idenical type relation
I think that preferring identical over convertible makes sense in e.g. C++ where we have implicit user-defined type conversions but since we don’t have them in Swift, I think the distinction doesn’t make too much sense, because if we have a `func foo(x: Int?)`, want don’t really want to  prioritize variables of type `Int?` over `Int` Similarly if we have `func foo(x: View)`, we don’t want to prioritize a variable of type `View` over e.g. `Text`.

rdar://91349364
2022-04-13 08:28:17 +02:00
Alex Hoppen
ed58b689a1 [CodeCompletion] Allow references to top-level functions with error parameters
During normal type-checking we ignore functions that contain an error. During code completion, we want to consider them and replace all error types by placeholders so we can match up the known types.

We already do this for member types (see `getTypeOfMemberReference`). We should also do it for top-level functions.

Fixes rdar://81425383 [SR-14992]
2022-04-06 10:10:09 +02:00
Alex Hoppen
cbbca3801f [CodeCompletion] Teach TypeCheckASTNodeAtLocRequest to check initializers
This simplifies the logic in `ide::typeCheckContextAt` and fixes an issue that prevent code completion from working inside variables that are initialized by calling a closure.

Fixes rdar://90455154 [SR-16012]
Fixes rdar://85600167 [SR-15495]
2022-04-04 14:41:59 +02: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
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
Rintaro Ishizaki
0580357a05 Merge pull request #38044 from rintaro/ide-completion-rdar79092374
[CodeCompletion] Allow unresolved types in Callee analysis
2021-08-02 09:40:31 -07:00
Rintaro Ishizaki
18dc9c1c27 [CodeCompletion] Remove CodeComletionString::getName()
`CodeCompletioString::getName()` was used only as the sorting keys in
`CodeCompletionContext::sortCompletionResults()` which is effectively
deprecated. There's no reason to check them in `swift-ide-test`. Instead,
check `printCodeCompletionResultFilterName()` that is actually used for
filtering.
2021-07-16 13:24:19 -07:00
Rintaro Ishizaki
013333b0d7 [CodeCompletion] Allow unresolved types in Callee analysis
struct Generic<T> {
    init(destination: T, isActive: Bool) {  }
  }

  invalid {
    Generic(destination: true, #^COMPLETE^#)
  }

In this case, since `invalid { ... }` cannot be type checked.
`ExprContextAnalysis` tries to type check `Generic` alone which resolves
to bound `Generic` type with an unresolved type. Previously, we used to
give up the analysis when seeing unresolved type. That caused mis callee
analyis.

rdar://79092374
2021-06-22 17:31:44 -07:00
Rintaro Ishizaki
154cd88c86 [CodeCompletion] Use 'Flair' to describe "is argument labels" 2021-06-07 17:25:01 -07:00
Rintaro Ishizaki
6dd5d9482f [CodeCompletion] Introduce "Flair" in code completion
To describe fine grained priorities.

Introduce 'CodeCompletionFlair' that is a set of more descriptive flags for
prioritizing completion items. This aims to replace '
SemanticContextKind::ExpressionSpecific' which was a "catch all"
prioritization flag.
2021-06-07 17:25:01 -07:00
Hamish Knight
a8fa55118d Merge pull request #37477 from hamishknight/an-effectful-argument
[CodeComplete] Include expr keywords in argument completion
2021-05-18 21:00:01 +01:00
Hamish Knight
010bcc1feb [CodeComplete] Include expr keywords in argument completion
When completing for an argument expression, make
sure to include keywords that can be used in an
expression position, such as `try`, `await` and
`super`.

rdar://77869845
2021-05-18 17:24:51 +01:00
Rintaro Ishizaki
27639c20dd [CodeCompletion] Lenient argument label checking if matching overload not found
If label-matching overload is not found, try to find a position with
lenient label matching.
2021-05-18 09:17:32 -07:00
Rintaro Ishizaki
8d63f298c1 [CodeCompletion] Don't suggest argument labels from unapplicable overload
When matching an argument to a parameter list, if there is no matching
parameter, the argument list is not applicable to the parameters. In
such case, code completion should not suggest argument labels of the
function.

rdar://77867723
2021-05-17 13:39:43 -07:00
Alex Hoppen
74ff6923a1 [Sema] Don’t allow unresolved type variables if LeaveBraceStmtBodyUnchecked is true
According to Pavel, we want to eliminate allowing unresolved variables as much as possible. Removing this flag doesn’t break any test cases (at least not in a meaningful way) and fixes a crasher, so it seems reasonable to remove it.

Fixes rdar://76686564
2021-05-06 10:19:05 +02:00
Alex Hoppen
2392e79ec9 [CodeCompletion] Fix code suggestions for arguments in vararg followed by normal arg
For a function and call like
```swift
func test(_: Foo..., yArg: Baz) {}
test(.bar, #^COMPLETE^#)
```
the parser matches the code completion token to the `yArg` with a missing label, because this way all parameters are provided. However, because of this we don’t suggest any variables that could belong the the previous vararg list.

To fix this, if we encounter such a situation (argument without label after vararg), manually adjust the code completion token’s position in params to belong to the vararg list.

Fixes rdar://76977325 [SR-14515]
2021-04-23 17:23:57 +02:00
Alex Hoppen
f96548820d [CodeComplete] Complete argument labels in generic initializer
The actual fix is to perform qualified lookup on a TypeExpr in `collectPossibleCalleesForApply`.

This changes the behaviour of some test cases in `complete_multiple_trailingclosure.swift`, which now provide argument labels. To make the choices suggested less verbose, I refined the parameter matching to only match trailing closures to parameters of function types.

In `INIT_FALLBACK_1` we technically shouldn't be suggesting `arg3` based on the matched argument types (the closure returns `Int` and the constructor with `arg3` requires the closure to return `String`), but AFAICT we aren't doing type-checking at this stage, so there's no way to rule it out.
2021-04-12 10:10:01 +02:00
Alex Hoppen
7e536ff365 [CodeComplete] Complete argument labels after vararg
Currently, if the code completion token is after a label that refers to a vararg, it is part of that VarargExpansionExpr, so we don’t suggest the subsequent parameter’s label.

Add special handling to detect this situation and suggest the parameter label.

Fixes rdar://76355192
2021-04-08 19:33:30 +02:00
Alex Hoppen
fc408cb87e [CodeCompletion] Remove test lines that are already run using batch completion
The test has been migrated to batch completion, there is no need to have individual `RUN:` lines for new code completion tokens.
2021-04-07 10:49:02 +02:00
Alex Hoppen
fd8e34913a Merge pull request #36551 from ahoppen/pr/internal-labels-in-closures
[CodeComplete] Default parameter names of completed closure to internal names
2021-04-06 15:30:26 +02:00
Rintaro Ishizaki
e2039406f4 [CodeCompletion] Migrate some tests to batch completion test
Reapply b810bb7bbd but without
'test/IDE/complete_where_clause.swift' which caused non-deterministic
failures.
2021-04-05 08:57:04 -07:00
Ted Kremenek
00534aa02a Revert "[CodeCompletion] Migrate some tests to batch completion test #4" 2021-04-03 09:20:45 -07:00
Rintaro Ishizaki
b810bb7bbd [CodeCompletion] Migrate some tests to batch completion test 2021-04-02 11:50:13 -07:00
Alex Hoppen
865e80f9c4 [CodeComplete] Default parameter names of completed closure to internal names
If have a function that takes a trailing closure as follows
```
func sort(callback: (_ left: Int, _ right: Int) -> Bool) {}
```
completing a call to `sort` and expanding the trailing closure results in
```
sort { <#Int#>, <#Int#> in
  <#code#>
}
```

We should be doing a better job here and defaulting the trailing closure's to the internal names specified in the function signature. I.e. the final result should be
```
sort { left, right in
  <#code#>
}
```

This commit does exactly that.

Firstly, it keeps track of the closure's internal names (as specified in the declaration of `sort`) in the closure's type through a new `InternalLabel` property in `AnyFunctionType::Param`. Once the type containing the parameter gets canonicalized, the internal label is dropped.

Secondly, it adds a new option to `ASTPrinter` to always try and print parameter labels. With this option set to true, it will always print external paramter labels and, if they are present, print the internal parameter label as `_ <internalLabel>`.

Finally, we can use this new printing mode to print the trailing closure’s type as
```
<#T##callback: (Int, Int) -> Bool##(_ left: Int, _ right: Int) -> Bool#>
```

This is already correctly expanded by code-expand to the desired result. I also added a test case for that behaviour.
2021-04-01 19:14:19 +02:00
Alex Hoppen
433d7564a4 [CodeComplete] Match argument labels when completing function arguments
Previously, we always assumed that the position in the arguments would match the position in the parameter, which isn’t true in case of defaulted arguments. Try matching parameters based on argument labels to give better completion results.

Fixes rdar://60346573
2021-03-25 20:54:07 +01:00
Alex Hoppen
11df457563 [CodeComplete] Complete parameter labels in self.init call
The `init` of `self.init` needs to be looked up on the Metatype of `self`, not on the type of `self` itself. We were already doing a similar special-casing for `super.init`, which I extened to also cover `self.init`.

Resolves rdar://36521732
2021-03-23 09:27:58 +01:00