Commit Graph

41 Commits

Author SHA1 Message Date
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
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
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
56ea3341f5 [CodeCompletion] Migrate ForEachSequence and PostfixExprBeginning to solver-based 2022-03-23 13:03:56 +01:00
Rintaro Ishizaki
db48ace197 [CodeCompletion] Let FindLocalVal walk into ExprPattern
Some conditions in control flow statements are parsed as expression
patterns, then resolved to "pattern"s later in Sema. In code completion,
they might not be type checked at all. Previously, when collecting
visible declarations for completion candidates, it didn't walk into
`ExprPattern`, and thus it didn't find the variables inside. Use
standard `Pattern::forEachVariable()` that looks into `ExprPattern`
instead of using its custom recursive logic.

rdar://86050684
2022-01-27 12:56:23 -08:00
Alex Hoppen
12ff361ec3 [CodeCompletion] Explicitly support enum pattern matching
Pattern matching in Swift can either be expression pattern matching by comparing two instances using the `~=` operator or using enum matching by matching the enum case and its associated types (+ tuple pattern matching, but that’s not relevant here). We currenlty only consider the expression pattern matching case for code completion. To provide enum pattern matching results, we thus need to have a `~=` operator between the code completion token and the match expression

For example, when we are completing

```swift
enum MyEnum {
  case myCase(String)
}

switch x {
case .#^COMPLETE^#
}
```
then we are looking up all overloads of `~=` and try to match it to the call arguments `(<Code Completion Type>, MyEnum)`.
The way we currently get `#^COMPLETE^#` to offer members of `MyEnum`, is that we are trying to make sure that the `~=<T: Equatable>(T, T)` operator defined in the standard library is the best solution even though it has fixes associated with it. For that we need to carefully make sure to ignore other, more favourable overloads of `~=` in `filterSolutions` so that `~=<T: Equatable>(T, T)` has the best score.

This poses several problems:
- If the user defines a custom overload of `~=` that we don't prune when filtering solutions (e.g. `func ~=(pattern: OtherType, value: MyEnum) -> Bool`), it gets a better score than `~=<T: Equatable>(T, T)` and thus we only offer members of `OtherType` instead of members from `MyEnum`
- We are also suggesting static members of `MyEnum`, even though we can't pattern match them due to the lack of the `~=` operator.

If we detect that the completion expression is in a pattern matching position, also suggests all enum members of the matched type. This allows us to remove the hack which deliberately ignores certain overloads of `~=` since we no longer rely on `~=<T: Equatable>(T, T)`. It thus provides correct results in both of the above cases.

Fixes rdar://77263334 [SR-14547]
2021-09-01 13:58:56 +02: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
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
Rintaro Ishizaki
b348bf60e1 [CodeCompletion] Complete pattern introducer for 'for'
After 'for', suggest 'try', 'await', 'var' and 'case'.

rdar://76355581
2021-05-06 18:13:43 -07:00
Rintaro Ishizaki
6e84c01a48 [CodeCompletion] Migrate some tests to batch completion test 2021-04-01 22:18:49 -07: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
Frederick Kellison-Linn
db33dfa3a1 [IDE] Offer unresolved member completions with non-matching types
Since the user can now write additional member accesses off of an UnresolvedMemberExpr, we should offer all available completions rather than just those that match the contextual type.
2020-08-26 22:42:30 -04:00
Rintaro Ishizaki
f107a15c63 [CodeCompletion] Skip to '=' after completion in 'if'/'guard' pattern
So that the parser can parse the subject expresion which provides the
expected type of the pattern.

rdar://problem/56802036
2020-05-29 13:56:30 -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
Rintaro Ishizaki
e947512875 [CodeCompletion] Prioritize type matching overload for unresovled member
For exmaple:

    func foo(_: Int, _: IntOption)
    func foo(_: Float, _: FloatOption)

    foo(intVal, .<HERE>)

Previously code completion suggests static member from 'IntOption' and
'FloatOption' without any prioritization. Prioritize members from
'IntOption' because the user probably wants to input them.

In such cases, 'CodeCompletionExpr' at the cursor position is
pre-typechecked to 'IntOption'. So mark results with matching type with
'ExprSpecific'.

rdar://problem/62121221
2020-04-24 18:01:59 -07:00
Rintaro Ishizaki
71aeffdf17 [CodeCompletion] Calculate type relation for EnumElementDecl 2020-02-03 17:20:50 -08:00
Rintaro Ishizaki
90f7f34a35 [CodeCompletion] Suggest local variables in a multi-clause guard statement
Previously, local optional binding values aren't suggested in subsequent
conditions in guard statement. That was because, if the condition
contains code-completion token, the parser stops parsing and add implicit
body with the end loc of the condition which is, in this case, the
position of the code-completion token. As a result, since code-completion
location is in the body range, namelookup::FindLocalVal refuses to emit
valuses in conditions.

This patch fixes the issue by ignoring implict body in `FindLocalVal`.

rdar://problem/28482216
2020-01-08 22:45:49 -08:00
Rintaro Ishizaki
6bd888fde1 [Parse] Recover missing initializer in stmt condition
Fix a crash introduced in aac92da252.

rdar://problem/43395202
2018-08-17 15:49:54 +09:00
Rintaro Ishizaki
eb6914ccfc Merge pull request #11346 from rintaro/rip-cfor
Eliminate C-style ForStmt
2017-08-08 07:49:42 -07:00
Rintaro Ishizaki
546aeb2336 [Parse] Remove Parser support for C-style for statement. 2017-08-04 23:27:15 +09:00
Rintaro Ishizaki
07957070cd [CodeComplete] Remove C-style-for specific facilities 2017-08-04 23:25:26 +09:00
Ben Langmuir
3279cf5ced [code-completion] Add Bool type context for if/guard/while conditions
When completing in an if/while/guard statement condition that expects a
boolean, add the code-completion type relation for Bool. We already had
this for repeat-while.

rdar://problem/26509084
2017-08-02 14:24:07 -07:00
Ben Langmuir
e32f9c56c1 Don't throw out AST for if/guard bindings during code-completion
It's hard to type-check `x` in `if let x = foo(), x.` after you throw
out the whole binding.

rdar://problem/32818340
2017-07-27 09:38:57 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
Ben Langmuir
75619c02c4 [CodeCompletion] Improve StmtCondition recovery for code-completion
In the condition expressions of if, while and guard statements we were
throwing away the AST if there was a parse error in the condition, or
the brace statement was missing.  This led to poor code-completion for
unresolved members (enums, options sets) because we couldn't find the
parent expression to type-check.

There are a few minor diagnostic changes because we now do more
type-checking of these conditions, particularly if they end up
containing an unused closure.

SR-2001
2016-07-06 14:08:53 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Ben Langmuir
968b0491b8 [CodeCompletion] Make Void-returning functions "NotRecommended" in typed context
When we know we're expecting an expression that returns a value, make
the completion "NotRecommended" to deprioritize it.

rdar://problem/22810741
2016-03-29 14:20:25 -07:00
David Farler
3f635d04c7 Reinstante var bindings in refutable patterns, except function parameters.
This reverts commits: b96e06da44,
                      8f2fbdc93a,
                      93b6962478,
                      64024118f4,
                      a759ca9141,
                      3434f9642b,
                      9f33429891,
                      47c043e8a6.

This commit leaves 'var' on function parameters as a warning to be
merged into Swift 2.2. For Swift 3, this will be an error, to be
converted in a follow-up.
2016-01-29 15:27:08 -08:00
Dmitri Gribenko
9d5cbc4428 Revert "Removed the ++ and -- operators" 2015-12-26 12:48:02 +01:00
David Walter
23772b1a21 Merge remote-tracking branch 'apple/master'
# Conflicts:
#	stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift
#	stdlib/public/core/ArrayCast.swift
#	stdlib/public/core/Collection.swift
#	stdlib/public/core/ContiguousArrayBuffer.swift
#	stdlib/public/core/StringBuffer.swift
#	stdlib/public/core/StringUnicodeScalarView.swift
#	test/1_stdlib/Builtins.swift
#	test/1_stdlib/Collection.swift
#	test/1_stdlib/ErrorType.swift
#	test/1_stdlib/ErrorTypeBridging.swift
#	test/1_stdlib/ExistentialCollection.swift
#	test/1_stdlib/Float.swift
#	test/1_stdlib/Map.swift
#	test/1_stdlib/Mirror.swift
#	test/1_stdlib/Optional.swift
#	test/DebugInfo/arg-debug_value.swift
#	test/DebugInfo/closure.swift
#	test/DebugInfo/for.swift
#	test/DebugInfo/return.swift
#	test/Interpreter/availability_weak_linking.swift
#	test/Interpreter/break_continue.swift
#	test/SILGen/sil_locations.swift
#	test/SILGen/statements.swift
#	test/SILGen/unreachable_code.swift
#	test/SILPasses/definite_init_diagnostics.swift
#	test/SILPasses/return.swift
#	test/SILPasses/switch.swift
#	test/SILPasses/unreachable_code.swift
2015-12-24 13:18:29 +01:00
Xi Ge
b797871ed2 [CodeCompletion] Add type relation indicators to completion results at the conditions of repeat-while statements.
Boolean expressions should have higher priority at loop conditions.
2015-12-11 15:00:13 -08:00
David Walter
9513d1797c Removed some post-, preincrement/decrements from test
Removed some post-, preincrement/decrements from test
2015-12-06 23:56:08 +01:00
Ben Langmuir
54a28a40db [CodeCompletion] Split literals out into their own completion kind
For rdar://problem/21923069

Swift SVN r32906
2015-10-27 13:34:54 +00:00
David Farler
a759ca9141 Disallow 'var' bindings in case patterns
Make the following illegal:

switch thing {
  case .A(var x):
    modify(x0
}

And provide a replacement 'var' -> 'let' fix-it.

rdar://problem/23172698

Swift SVN r32883
2015-10-25 18:53:02 +00:00
Chris Willmore
c7c7388cf2 Change do-while to repeat-while.
Change all uses of "do { ... } while <cond>" to use "repeat" instead.
Rename DoWhileStmt to RepeatWhileStmt. Add diagnostic suggesting change
of 'do' to 'repeat' if a condition is found afterwards.

<rdar://problem/20336424> rename do/while loops to repeat/while & introduce "repeat <count> {}" loops

Swift SVN r27650
2015-04-23 22:48:31 +00:00
Denis Vnukov
152df92966 [CodeCompletion] Code Completion String getName should always return non-empty string value for
valid code completions. Code completion tests call and validate getName’s result. 

A minor fix for code completion fro subscripts.



Swift SVN r25101
2015-02-09 22:32:05 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Doug Gregor
2f3f6acf21 Make "true" and "false" Boolean literal constants for the BooleanLiteralConvertible protocol.
Introduce the new BooleanLiteralConvertible protocol for Boolean
literals. Take "true" and "false" as real keywords (which is most of the
reason for the testsuite churn). Make Bool BooleanLiteralConvertible
and the default Boolean literal type, and ObjCBool
BooleanLiteralConvertible. Fixes <rdar://problem/17405310> and the
recent regression that made ObjCBool not work with true/false.


Swift SVN r19728
2014-07-09 16:57:35 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Dmitri Hrybenko
ba3cec0871 Code completion: add tests for completion inside statements' controlling expressions
These work OK, except that we don't include local variables in the results --
this is a general limitation now.


Swift SVN r6856
2013-08-02 21:31:35 +00:00