Commit Graph

164 Commits

Author SHA1 Message Date
Hamish Knight
669a2ce9b0 [CS] Sink placeholder handling logic into Solution::simplifyType
Move the logic from `FailureDiagnostic::resolveType` into
`Solution::simplifyType` to allow completion to use it too. While
here, also handle cases where the placeholder is from a different
member of the equivalence class to the generic parameter.
2025-10-04 12:56:52 +01:00
Hamish Knight
3b056039de [test] Remove some unnecessary completion XFAILs
All the cases now work correctly and just need a small tweak to the
expectation.
2025-09-29 16:55:34 +01:00
Ahmed Mahmoud
491b3a0fbc [IDE] Erase archetypes with no declaration generic signature
This generalization enables curried functions with generic parameters coming from the initial declaration to be printed with the archetype's upperbound rather than '_' unresolved type.

As an added benefit, T.self and T.Type for generic parameters now get shown as the upperbound of the generic parameter provided
2025-08-29 13:39:58 +03:00
Nate Cook
e68069f891 [stdlib] Allow a default for optional interpolations (#80547)
This adds an `appendInterpolation` overload to
`DefaultStringInterpolation` that includes a parameter for providing a
default string when the value to interpolate is `nil`. This allows this
kind of usage:

```swift
let age: Int? = nil
print("Your age is \(age, default: "timeless")")
// Prints "Your age is timeless"
```

The change includes an additional fixit when optional values are
interpolated, with a suggestion to use this `default:` parameter.
2025-05-07 12:47:02 -05:00
Alexander Cyon
a80b8ae06f [test/IDE] Fix typos 2024-08-08 19:35:00 +02:00
Alex Hoppen
6f86e24acf [CodeCompletion] Use Self type in extension for generic substitution when doing implicit member lookup on protocol type
This fixes the regression introduced in https://github.com/swiftlang/swift/pull/75068#discussion_r1679449260.
2024-08-02 10:41:37 -07:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -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
4f5743c8c6 [IDE] Adjust more test cases 2023-07-07 19:51:01 +02:00
Alex Hoppen
00eaed3af9 [CodeCompletion] Migrate postfix expr completion to solver-based 2023-07-07 19:51:01 +02:00
Alex Hoppen
4c186c0a9a [CodeCompletion] Fix code completion issues related to type checking tap expressions in the cosntraint system 2023-07-07 19:50:46 +02: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
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
cbb32aae86 [Sema] Don't abort the result builder transform if the builder contained an ErrorExpr
When matching a result builder in the constraint system, the closure has already been pre-checked so we don’t need to pre-check it again. Also, we can continue type checking even if the closure contained an `ErrorExpr` since result builders are now type checked like regular closures.
2023-03-13 15:51:39 -07:00
Alex Hoppen
3ad1ac75b9 [IDE] Change -NEXT to -DAG in test cases
We don’t actually care about the order of the results here. Change `-NEXT` to `-DAG` to make the test cases more robust to changes  in code completion.
2023-02-16 09:39:01 +01:00
Rintaro Ishizaki
0e2e315633 Remember nominal type decls for member lookup
`lookupVisibleMemberDecls` visits nominal type decls to find visible
members of the type. Remembering what decls are visited can be useful
information for the clients.

* Add a 'VisibleDeclConsumer' callback function that is called when
  'lookupVisibleDecls' visits each nominal type decls
* Remember the decl names in 'CodeCompletionContext' for future use
2022-10-21 16:46:06 -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
Rintaro Ishizaki
504e73d5ad [CodeCompletion] Remove a hack adding type matching nominal types
Previously we didn't give any type relations to cached imported symbols.
So there was a hack to add the type matching nominal type with type
relation manually, which caused duplicated candiates, one from the
cache, one added manually.
Now that we have type relations for cached symbols so we don't need this
hack anymore.

rdar://90136020
2022-03-11 10:51:02 -08: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
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
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
1d28287f4b [CodeCompletion] Migrate some tests to batch completion test 2021-04-01 21:01:01 -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
Nathan Hawes
ca7fb37aba [CodeCompletion][Sema][Parse] Migrate unresolved member completion to the solver-based completion implementation
Following on from updating regular member completion, this hooks up unresolved
member completion (i.e. .<complete here>) to the typeCheckForCodeCompletion API
to generate completions from all solutions the constraint solver produces (even
those requiring fixes), rather than relying on a single solution being applied
to the AST (if any). This lets us produce unresolved member completions even
when the contextual type is ambiguous or involves errors.

Whenever typeCheckExpression is called on an expression containing a code
completion expression and a CompletionCallback has been set, each solution
formed is passed to the callback so the type of the completion expression can
be extracted and used to lookup up the members to return.
2020-11-13 15:37:14 -08:00
Rintaro Ishizaki
84f0c5723a [CodeCompletion] Calculate type relation when adding type annotation
Calculate and set the type relation in each result building logic which
knows the actual result type.

CodeCompletionResultBuilder couldn't know the actual result type. From
the declaration alone, it cannot know the correct result type because it
doesn't know how the declaration is used (e.g. calling? referencing by
compound name? curried?)
2020-09-16 22:04:50 -07:00
Rintaro Ishizaki
f7aaf5bc94 [CodeCompletion] Stop suggesting static member on a protoco meta type
static members cannot be used on protocol metatypes.

rdar://problem/58886969
2020-05-20 23:08:15 -07:00
Rintaro Ishizaki
75a0c9f819 [CodeCompletion] Add 'IsSystem' flag to code completion result item
'key.is_system: 1' is added if the associated declaration is from a
system module.

rdar://problem/62617558
2020-05-11 12:24:36 -07:00
Rintaro Ishizaki
e9c438cdd5 [CodeCompletion] Dont mark type mismatching items 'not recommended'
func foo() {}
let a: Int = #^HERE^#

Previously, we marked 'foo()' as 'NotRecommented' because 'Void' doesn't
have any member hence it cannot be 'Int'. But it wass confusing with
'deprecated'.

Now that we output 'typerelation' which is 'invalid' in this case. So clients
can deprioritize results, or even filter them out.

rdar://problem/57726512
2020-05-05 10:40:05 -07:00
Anthony Latsis
cf43a564a2 [CodeCompletion] Check visibility in lookup mode when collecting value requirements 2020-03-23 21:40:41 +03:00
Rintaro Ishizaki
9225cfce3d [CodeCompletion] Type annotation for call argument completions 2020-03-12 17:46:25 -07:00
Rintaro Ishizaki
f8147f72d9 [CodeCompletion] Call argument label with value placeholder
When a completion happens at a call argument position, insert
'label: <#T##TypeName#>' instead of just 'label: '.

rdar://problem/60379654
2020-03-12 11:48:15 -07:00
Rintaro Ishizaki
78d0f6aa4a [CodeCompletion] Primarily use getPossibleCallees() for call signature completion
instead of the pre-typechecked type and the referenced decl in the AST
so that we can suggests all overloads even if it happen to be
typechecked to a method. For example

    struct MyType {
      func foo() {}
      func foo(_ int: Int) {}
      func foo(name: String, value: String) {}
    }
    func test(val: MyType) {
      value.foo(#^COMPLETE^#)
    }

In this case, the call is typechecked to 'MyType.foo(_:)', but we want
to suggest all overloads.

rdar://problem/59285399
2020-02-28 16:44:03 -08:00
Doug Gregor
e1c0decdf0 Update some test cases for the OptionalSomePattern change. 2020-02-24 00:48:14 -08:00
Slava Pestov
44827bbde4 IDE: Refactor LookupVisibleDecls to use standard override/shadowing logic
There's no need to roll our own implementation here.

Fixes <rdar://problem/16724197>.
2019-09-25 22:17:56 -04:00
Nathan Hawes
7bcba14cac [IDE] Fix code completion crash after already-typechecked ApplyExprs
This was due to us unconditionally setting the referenced decl to be the decl
referenced by the ApplyExpr's function, when we should only do that for
SelfApplyExprs. This caused a crash for calls returning a function type due to
a mismatch between the resulting type (the function type the applied function
returns) vs the referenced decl signature (the signature of the applied
function itself).

Resolves rdar://problem/53034130.
2019-08-19 14:06:16 -07:00
Rintaro Ishizaki
35f8686574 [CodeCompletion] Remove getTypeContextEnumElementCompletions()
Use getUnresolvedmemberCompletion() instead.

rdar://problem/45219937
2019-06-25 11:58:12 -07:00
Rintaro Ishizaki
0c38f2df8a [CodeCompletion] Associate callee declaration with call patterns
In `addFunctionCallPattern()` we should associate declarations if
possible.

rdar://problem/49158044
2019-04-01 11:21:43 -07:00
Ted Kremenek
fe215edb9b Merge pull request #19743 from Azoy/smarter-struct-init
[Sema] Synthesize default values for memberwise init
2019-03-25 17:31:01 -07:00
Rintaro Ishizaki
15f2b00c9a Merge pull request #23490 from rintaro/ide-completion-emptycall-rdar46215262
[CodeCompletion] Provide empty call pattern after paren
2019-03-25 16:30:46 -07:00
Rintaro Ishizaki
bfaa6af57e [CodeCompletion] Look through implicit exprs to find the parsed expr
The parsed expression may be wrapped with various implicit expressions.
For example, if the expression is only element in array literal, it's
wrapped with `(arrayLiteral: <expr>)`.
2019-03-22 15:01:18 -07:00
Rintaro Ishizaki
7ae18ff0f1 [CodeCompletion] Provide empty call pattern after paren
This improves discoverability of no-parameter initializers.

rdar://problem/46215262
2019-03-22 10:56:40 -07:00
Ben Langmuir
f5a216fc04 Merge pull request #23411 from benlangmuir/cc-1-close
[code-completion] Add type context for single-expression closures
2019-03-20 06:56:49 -07:00
Ben Langmuir
b817cbb9bc [code-completion] Add type context for single-expression closures
When completing in the only expression of closure, use the return type
of the closure as the type context for the code-completion. However,
since code-completion may be on an incomplete input, we only use the
return type to improve the quality of the result, not to mark it
invalid, since (a) we may add another statement afterwards, or (b) if
the context type is Void it doesn't need to match the value.
2019-03-18 17:04:33 -07:00
Rintaro Ishizaki
4a4ee3fe89 [CodeCompletion] Filter out inapplicable member from callee analysis
rdar://problem/48604807 / https://bugs.swift.org/browse/SR-9938
2019-03-15 23:38:07 -07:00
Rintaro Ishizaki
b006c7c9b8 [AST] Don't return inapplicable decls in lookupVisibleDecls
rdar://problem/45340583 / https://bugs.swift.org/browse/SR-9027
rdar://problem/36594731
2019-03-14 12:57:37 -07:00
Azoy
6f7d20b99e Synthesize default values for memberwise init
Introduce stored property default argument kind

Fix indent

Assign nil to optionals with no initializers

Don't emit generator for stored property default arg

Fix problem with rebase

Indentation

Serialize stored property default arg text

Fix some tests

Add missing constructor in test

Print stored property's initializer expression

cleanups

preserve switch

complete_constructor

formatting

fix conflict
2019-03-13 18:57:36 -05:00
Rintaro Ishizaki
1038011c67 [CodeCompletion] Force type check pattern binding for initializer
Also, do not handle parsed decls in first-pass. Because they are
re-parsed, they used to case duplicated decls in AST.

rdar://problem/40944761
2019-03-08 13:42:08 -08:00
Rintaro Ishizaki
97d7111fa6 [CodeCompletion] Clean-up result build for method/function all
Don't emit duplicate results for implicitly curried instance methods
with defaulted parameters.
2019-03-05 16:45:48 -08:00