Commit Graph

78 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
7061a20edd [CS] Remove ConstraintSystem::getVarType
The logic here for completion wasn't actually
helping things since it would result in adding the
var overload to the system, which would result
in an ErrorType binding. We could turn the ErrorType
into a placeholder when resolving the overload,
but the simpler solution is to just allow CSGen
to turn the reference into a PlaceholderType. This
matches what we do for regular solving, and fixes
a crash with an IUO completion.

rdar://89369091
2024-11-04 17:08:20 +00: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
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
c7e0bfae02 [IDE] Adjust test cases for migrating all completion kinds to solver-based 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
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
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
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
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
c21a75def4 [CodeCompletion] Migrate CaseStmtBeginning to solver-based 2022-04-06 09:59:33 +02: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
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
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
Rintaro Ishizaki
4df17c5bd2 [CodeCompletion] Stop suggesting initializer calls on unresolved member
For example, for

  class C {
    class D: C {
        init() {}
    }
  }
  let _: C = .#^HERE^#

We used to suggest 'D()' (not just 'D') because it was invalid without
initialization. However, after SE-0287, it can be valid. For example, if
'D' has:

  extension C.D {
    static var staticC: C { ... }
  }

Users can write:

  let _: C = .D.staticC

So we should not suggest constructor calls. Also, this is consistent with
normal type name completion. Users still can get initializer completions
by adding '(' after the type name.

Although initialization call completion with type names was convenient
in some cases, it's too annoying when we have unrelated member types
like:

  enum MyEnum {
    typealias Content = Int
    case value(Content)
  }

We don't want `.Content(bitPattern)` etc for implicit member completion
for 'MyEnum' context type.

rdar://75963052
2021-03-29 15:37:13 -07: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
Nathan Hawes
9b8fb7d37d [CodeCompletion] Rename a few types/methods for clarity plus other small refactorings (NFC)
Also put subclasses of TypeCheckCompletionCallback into their own header.
2020-09-08 16:16:39 -07:00
Nathan Hawes
6e657e8615 [CodeCompletion] Fix incorrect type relations in complete_enum_elements test.
We were reporting methods that return function types that return void (rather
than returning void directly) as being invalid in contexts that expect non-void
expressions and testing for that incorrect behavior.
2020-08-28 22:24:24 -07:00
Frederick Kellison-Linn
8905f9735c [Tests] Update completions for unresolved enum members 2020-08-27 12:59:11 -04: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
3ec250f701 [CodeCompletion] Wrap base expression with CodeCompletionExpr
For example for:

  funcName(base.<HERE>)

Wrap 'base' with 'CodeCompletionExpr' so that type checker can check
'base' independently without preventing the overload choice of 'funcName'.

This increases the chance of successful type checking.

rdar://problem/63965160
2020-06-05 15:51:07 -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
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
35f8686574 [CodeCompletion] Remove getTypeContextEnumElementCompletions()
Use getUnresolvedmemberCompletion() instead.

rdar://problem/45219937
2019-06-25 11:58:12 -07:00
Rintaro Ishizaki
c99af83901 [CodeCompletion] Consolidate parameter list processing funcitons
There were 2 functions to output argument list. Consolidate them and
consistently use it from every call like production (i.e. function call,
constructor call, enum with associated values, subscript)
2019-03-05 16:45:48 -08:00
Rintaro Ishizaki
dc8508435b [CodeCompletion] Show bound generic types for enum elements
We weren't displaying bound generic types for enum elements. It's
inconsistent with functions and initializiers.
2019-02-20 11:31:04 -08:00
Rintaro Ishizaki
25f9b8843c [CodeCompletion] Show the enum type for enum element with assoc values
enum MyEnum {
    case null
    case str(String)
}

When completing elements for enum like this, the former shows `MyEnum`,
but the latter shows `(String) -> MyEnum`. This is inconsistent with
function call pattern which only shows the result type.

rdar://problem/48220244
2019-02-20 10:29:59 -08:00
Rintaro Ishizaki
01b8fbc1bf [CodeCompletion] Implement .Type completion in expression context
rdar://problem/22072865
2019-02-12 10:40:08 -08:00
Karoy Lorentey
811d3d710e [test] Update for RawRepresentable hashing changes 2018-11-22 17:16:07 +00:00
Rintaro Ishizaki
b1ae8efd77 [CodeCompletion] Enable case enum pattern completion in top level
rdar://problem/21001526
2018-10-12 16:56:52 +09:00
Rintaro Ishizaki
18582d3406 [CodeCompletion] Handle ExprPattern in context type analysis
This improves type inference for code completion in argument position of
EnumElementPattern.
2018-08-09 19:49:25 +09:00
fischertony
7b41a41fb6 updated tests & added completions for postfixExpr 2018-05-12 09:05:57 +03:00
Karoy Lorentey
130bcfd32d [SE-0206][test] Fix tests that look at Hashable requirements
hash(into:) needs to be included in expectations; tests looking at synthesized Hashable implementation bodies need to be updated for resilient hashing.
2018-04-24 17:42:42 +01:00
Robert Widmann
03580d2fe5 Add a parameter list to EnumElementDecl
This models, but does not plumb through, default arguments.
2018-03-28 00:05:56 -04:00
Nathan Hawes
781539c5c3 [code-completion] Use the SanitizeExpr ASTWalker along all paths to the constraint based type checker
Code completions calls typecheckUnresolvedExpression when completing unresolved members.
This calls an overload of solve() that bypasses sanitization, and without it we hit an
assertion failure in getTypeOfReference.

Resolves rdar://problem/38144409.
2018-03-22 10:21:11 -07:00
Robert Widmann
dac06898e9 [SE-0194] Deriving Collections of Enum Cases
Implements the minimum specified by the SE-proposal.

* Add the CaseIterable protocol with AllCases associatedtype and
allCases requirement
* Automatic synthesis occurs for "simple" enums
    - Caveat: Availability attributes suppress synthesis.  This can be
              lifted in the future
    - Caveat: Conformance must be stated on the original type
              declaration (just like synthesizing Equatable/Hashable)
    - Caveat: Synthesis generates an [T].  A more efficient collection
              - possibly even a lazy one - should be put here.
2018-03-09 00:22:55 -05:00
Pavel Yaskevich
c0f5711ee5 [ConstraintSystem] Don't apply types to expressions until solution is found
Resolves: rdar://problem/36744895
2018-02-13 00:08:45 -08:00
Pavel Yaskevich
f09d2ad397 [ConstraintSolver] Forbid forming solutions with free generic type parameters
`FreeTypeVariableBinding::GenericParameters` mode allowed to bind
all free type variables with fresh generic parameter types, which
is incorrect (at least) if there are multiple generic solutions
present, because such parameters couldn't be compared.

This mode was used for code completion, which is now switched to use
`FreeTypeVariableBinding::UnresolvedType` instead.
2017-06-01 14:05:05 -07:00
Ben Cohen
38903764df Revert "Revert "[stdlib] One-sided ranges and RangeExpression (#8710)"" 2017-04-30 16:47:23 -07:00
Arnold Schwaighofer
7d5d63eaf8 Revert "[stdlib] One-sided ranges and RangeExpression (#8710)"
This reverts commit 946b776e37.
2017-04-30 15:51:16 -07:00
Ben Cohen
946b776e37 [stdlib] One-sided ranges and RangeExpression (#8710)
* One-sided ranges and RangeExpression

* Remove redundant ClosedRange methods from String

* Fix up brittle tests

* Account for Substring update

* XFAIL range diagnostics on Linux
2017-04-28 12:59:04 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
Doug Gregor
b9363fe6bd [SE-0111] Enable SE-0111 by default. 2016-07-29 17:28:24 -07:00
Joe
3938d5682a [SE-0095] [Runtime], [Demangler], & AST printer updated to new composition syntax
- All parts of the compiler now use ‘P1 & P2’ syntax
- The demangler and AST printer wrap the composition in parens if it is
in a metatype lookup
- IRGen mangles compositions differently
    - “protocol<>” is now “swift.Any”
    - “protocol<_TP1P,_TP1Q>” is now “_TP1P&_TP1Q”
- Tests cases are updated and added to test the new syntax and mangling
2016-07-19 12:01:37 -07:00