Commit Graph

44 Commits

Author SHA1 Message Date
Hamish Knight
14608cb059 [Sema] Skip type-checking catch bodies when computing the bound error type
Make sure we only ever type-check the `do` body of a `do-catch`
statement when lazily type-checking the bound error type, which we can
do for completion.

rdar://164481242
2025-11-11 23:04:30 +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
Tony Allevato
67d9eecd50 [AST] Make IsNonUserModuleRequest consider SourceFile inputs as well.
We're using a small custom frontend tool to generate indexstore data for `.swiftinterface` files in the SDKs. We do this by treating the `.swiftinterface` file as the input of an interface compilation, but this exits early because it treats it as a `SourceFile` instead of an external `LoadedFile`. This happens even if we call `setIsSystemModule(true)` unless we skip setting the SDK path, but that causes other problems. It seems harmless to check for `SourceFile`s as well, so that a tool processing an SDK interface as a direct input still gets the right state.
2024-09-17 09:44:42 -04:00
Rintaro Ishizaki
0e122544ca [CodeCompletion] Fix completion for 'catch' pattern bound values
Previously code completion for 'catch' pattern bound values didn't work
correctly because code completion type checker fails to type check the
value decl in the pattern.
That was because the body of the 'do' statement is not type checked, so
the thrown error is not determined, then falled backed to the default
'Never', which doesn't matches any patterns.
To resolve this, always type check the body when typechecking 'catch'
patterns. Also, pretends 'do {}' throws 'any Error' even without
any throwing expressions in the body.

rdar://126699879
2024-04-22 11:09:12 -07:00
Hamish Knight
f4b928fd0a Remove FindLocalVal
Replace with an ASTScope lookup. This also lets
us simplify UsableFilteringDeclConsumer, and fixes
a couple of completion bugs.
2024-02-07 23:02:37 +00:00
Doug Gregor
be6393b7ca Use any Error for caught error type of an exhaustive, non-throwing do..catch
Prior to the introduction of typed throws, a `do..catch` always had a caught
error type of `any Error`, even if there were no throwing sites within
the `do` body. With typed throws, such a `do..catch` would have a
caught error type of `Never`, because it never throws. Unfortunately,
this causes code like the following to produce an error, because
`Never` cannot be pattern-matched to `HomeworkError.forgot`:

    func test() {
      do {
        try nonthrowing()
      } catch HomeworkError.forgot {
      } catch {
      }
    }

For source-compatibility reasons, adjust the caught error type to
`any Error` in this case, so we continue to accept the code above.
Don't do this adjustment under `FullTypedThrows`, because it's only
there for compatibility.

Fixes rdar://121526201.
2024-01-24 14:42:56 -08:00
Doug Gregor
87f97c5696 Temporarily hack test case involving code completion with "catch" clauses 2024-01-04 09:47:38 -08: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
56ea3341f5 [CodeCompletion] Migrate ForEachSequence and PostfixExprBeginning to solver-based 2022-03-23 13:03:56 +01: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
1855e1a143 [CodeCompletion] Add flairs to cached items
* Starting a statement with a protocol name is rare
* Starting a statemnet at top-level of non-script file is invalid

rdar://77934897
2021-06-08 14:09:39 -07:00
Rintaro Ishizaki
154cd88c86 [CodeCompletion] Use 'Flair' to describe "is argument labels" 2021-06-07 17:25:01 -07:00
Nathan Hawes
b4553cdd4f [CodeCompletion][test] Update test/IDE/complete_exception.swift with improved type relation. 2020-08-28 22:24:25 -07:00
Nathan Hawes
5100de4293 [CodeCompletion] Fallback to typechecking just the completion expression in cases where typeCheckExpression is never called.
This happens when, e.g. an expression being switched on is invalid so
expression patterns in the switch cases (which may contain the completion
expression) are not checked.

Also setup the Lookup object to handle member completion in ObjC selector
expressions correctly, and fix passing the wrong expression when computing
isStaticallyDerivedMetatype().
2020-08-28 22:24:23 -07:00
Nathan Hawes
00994f1147 [Parse] Stop early exiting when parsing catch statements and ObjCSelector expressions that contain code completions. 2020-08-28 17:09:37 -07:00
Nathan Hawes
c4f05052da [Parse] Don't drop throws containing a code completion expression. 2020-08-28 17:09:37 -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
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
e0cc7db4d4 [CodeCompletion] Enable already fixed test case for toplevel 'catch'
rdar://problem/21001526
2018-10-12 16:56:52 +09:00
fischertony
7b41a41fb6 updated tests & added completions for postfixExpr 2018-05-12 09:05:57 +03:00
Ben Langmuir
642ae90a86 [code-complete] Reduce the priority of function call patterns
Constructor call patterns already get a real priority, but because of
the way we do function call patterns we don't have enough information,
and previously we were setting it to "expression specific", which is
unnecessarily high, particularly since functions (unlike inits) have
other better ways to code-complete already.

rdar://31113161
2017-12-18 12:55:20 -08:00
Nate Cook
b6af57a338 Update tests for endian default implementations 2017-05-24 19:16:26 -05:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Ben Langmuir
1992bb08b2 [CodeCompletion] Add keyword kind to code completion results
This lets us reliably distinguish keywords we care about without
resorting to string comparisons.  Also driveby fix throw to be a
statement keyword.
2015-11-02 13:27:34 -08:00
Dmitri Hrybenko
4375a463a7 stdlib: rename Int**.value and Float**.value to _value per naming convention
rdar://21357661

Swift SVN r32096
2015-09-20 00:01:13 +00:00
Argyrios Kyrtzidis
658d852f68 [IDE] When printing stdlib interface, hide underscored members of protocols, and subscript
decls that have underscored parameters.

Dmitri verified that the removals after this change are ok.

Swift SVN r29177
2015-05-31 00:41:09 +00:00
Ben Langmuir
d4448b5c6d Re-enable test/IDE/complete_exception.swift with 32bit fix
Use Int32 instead of Int per suggestion from Dmitri.

Swift SVN r29088
2015-05-27 22:39:04 +00:00
Greg Parker
9ce2b998e5 [test] Disable test IDE/complete_exception.swift.
It fails on 32-bit architectures.


Swift SVN r29087
2015-05-27 22:21:55 +00:00
Ben Langmuir
4c83738657 Address review feedback on r29070
* Make CHECK-NOT lines more robust
* Remove redundant assertion

Swift SVN r29079
2015-05-27 21:25:10 +00:00
Ben Langmuir
8ccaa86806 [CodeCompletion] Complete variables bound by catch
Make sure we build the CatchStmt and DoCatchStmt AST nodes when
code-completing inside the body of a catch so that we can complete the
bindings from the catch.

It's often a good idea to early-exit once we see a code completion
token, but not when we skip building an AST node that provides variable
bindings.  In code completion, we don't have Scope-based lookup, and
rely on having reachable AST nodes for patterns so that we can dig the
out the bindings we need.

Also extend the pattern checking to handle "IsPattern", since we
apparently weren't handling "let x as Foo", and that affects all complex
catch patterns because of an implicit "as ErrorType" or explicit
"as NSError".

rdar://problem/21116164

Swift SVN r29070
2015-05-27 19:01:16 +00:00
Jordan Rose
fa5bd4c6c8 [ClangImporter] Allow apinotes to affect inherited initializers.
Then use that to ban NSError.init(), because it doesn't create a valid
NSError. In the long run Foundation will hopefully add this to their
headers, but they can't yet (rdar://problem/19977891).

rdar://problem/21042412

Swift SVN r28881
2015-05-21 18:11:17 +00:00
Ben Langmuir
fb7dd7698a Fix linux testing after r28716
Cargo cult over a requirement from the clang importer tests.  It seems a
bit more semantic than XFAIL: linux.

Swift SVN r28717
2015-05-18 19:53:45 +00:00
Ben Langmuir
882959b60d [CodeCompletion] Remove incorrect special-casing for throw/catch
It's not okay to filter to only ErrorType results, since we may be
trying to chain to an error type result foo.bar.getError().  And the
existing logic had no way to handle results from other modules, so we
were missing key results like 'NSError'.

Eventually we'll want to bring back something like this that handles all
modules, but as a way to bump the priority of ErrorType results rather
than to filter out everything else.

rdar://problem/20985515

Swift SVN r28716
2015-05-18 19:00:04 +00:00
Ted Kremenek
9f9bb725cf Rename '_ErrorType' to 'ErrorType'.
Swift SVN r28293
2015-05-07 21:59:29 +00:00
Xi Ge
90cf8aec22 [CodeCompletion] Add types whose extensions
conform _ErrorType protocol to the completions of throw statement.

Swift SVN r26691
2015-03-29 05:56:06 +00:00
Xi Ge
256e038edf [CodeCompletion] Add exception handle clauses to catch stmt completion.
Swift SVN r26597
2015-03-26 18:32:13 +00:00
Xi Ge
0d9fb1aaa5 [CodeCompletion] Make sure only instantiable types are included
in code completion options when completing a throw statement.

Swift SVN r26571
2015-03-26 00:58:19 +00:00
Xi Ge
381e9ebe40 [test] Add a missing run line.
Swift SVN r26567
2015-03-26 00:02:56 +00:00
Xi Ge
6f43e545fb [CodeCompletion] Auto-completing the throw statement.
When the code completion token appears after throw keyword,
a set of visible decls and instances conforming _ErrorType are
recommended as completion.

Swift SVN r26565
2015-03-25 23:46:26 +00:00
Xi Ge
8a64deb828 [codecompletion] auto-completing the catch statement.
When the code completion token is seen after the catch keyword, a set of
visible error types are suggested as completion.

Swift SVN r26519
2015-03-25 04:16:57 +00:00