Commit Graph

1148 Commits

Author SHA1 Message Date
Rintaro Ishizaki
858ecb8413 [CodeCompletion] Don't suggest underscored module name
Underscored modules (e.g. '_Concurrency') are usually not meant be
written explicitly in source code. They are usually implicitly imported
with other modules. Code completion should not suggest them in type and
expression context.

rdar://78232229
2021-05-19 23:09:29 -07: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
Slava Pestov
131d3f4bce Sema: Pass down a ModuleDecl instead of a DeclContext to conformsToProtocol()
... and a bunch of follow-up simplifications pushing ModuleDecls further
up, since I couldn't resist the yak shave.
2021-05-17 16:34:18 -04:00
Rintaro Ishizaki
861bbcc5d6 Merge pull request #37330 from rintaro/ide-completion-rdar75620636
[CodeCompletion] Make object literals optional
2021-05-10 13:47:10 -07:00
Rintaro Ishizaki
5ea60e8044 Merge pull request #37300 from rintaro/ide-completion-rdar76355581
[CodeCompletion] Complete pattern introducer for 'for'
2021-05-07 18:41:04 -07:00
Rintaro Ishizaki
da96ef1f51 [CodeCompletion] Make object literals optional
For example, non-Darwin platforms probably don't want
`#colorLiteral(red:green:blue":alpha:)` and `#imageLiteral(named:)`.
Add an completion option to include them, which is "on" by default.

rdar://75620636
2021-05-07 16:27:13 -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
Alex Hoppen
3d1f9b6402 [CodeCompletion] Remove unresolved type from ParsedExpr during operator completion
When doing operator completion, we re-type-check the sequence expression. If we have an unresolve type already applied to `ParsedExpr`, which is the last element of the sequence, the type checker crashes in `validation-test/IDE/crashers_2_fixed/0008-must-conform-to-literal-protocol.swift`, because there are still inactive constraints in the constraint system when it finishes solving.

Previously, we would ignore because we allowed free type variables, which we no longer do since the last commit.
2021-05-06 18:35:00 +02:00
Rintaro Ishizaki
3c2433c942 [CodeCompletion] Prioritize call argument pattern
struct Foo {
    init(_ arg1: String, arg2: Int) {}
    init(label: Int) {}
  }
  func test(strVal: String) {
    _ = Foo(<HERE>)
  }

In this case, 'strVal' was prioritized because it can use as an argument
for 'init(_:arg2:)'. However, argument labels are almost always
preferable, and if the user actually want 'strVal', they can input a few
characters to get it at the top. So we should always prioritize call
argument patterns.

rdar://77188260
2021-04-29 13:23:15 -07:00
Alejandro Alonso
444c35cf8d Merge pull request #36953 from Azoy/isType-isDeclType
[NFC] Introduce isDecl and getDeclType
2021-04-20 15:33:00 -04:00
Azoy
9ed732f0ab Introduce isDecl and getDeclType
fix enum logic issue

fix tests

guard against null types
2021-04-20 02:22:16 -04:00
Ben Langmuir
fa476b0a8d [completion] Make NotRecommendedReason an enum class 2021-04-19 13:22:13 -07:00
Ben Langmuir
be7b5a7179 [completion] Clarify and simplify not-recommended state
Combine IsNotRecommended with NotRecommendedReason and improve the names
of the existing cases to more clearly identify the cases they apply to.
Now all not-recommended completions are required to have a reason.
2021-04-19 09:43:28 -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
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
Rintaro Ishizaki
8c2e54bbde Merge pull request #36634 from rintaro/ide-completion-rdar75963052
[CodeCompletion] Stop suggesting initializer calls on unresolved member
2021-03-30 09:48:24 -07:00
Alex Hoppen
4faa3973ac Merge pull request #36607 from ahoppen/pr/suggest-self-in-where-clause
[CodeComplete] In where clauses, suggest Self and the current type
2021-03-30 13:17:45 +02: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
Alex Hoppen
943d7ee621 [CodeComplete] In where clauses, suggest Self and the current type
Fixes rdar://70058225
2021-03-26 17:43:35 +01:00
Doug Gregor
9d8fde8c18 [Concurrency] Remove actorIndependent(unsafe) isolation.
Don't treat `actorIndependent(unsafe)` as its own kind of isolation.
It was only really used as a bring-up hack to break the isolation model,
but shouldn't be in the user model of the language and causes
complications to the implementation.
2021-03-26 08:27:36 -07:00
Rintaro Ishizaki
3434463368 [CodeCompletion] Factor out actor isolation analysis 2021-03-24 15:53:37 -07:00
Rintaro Ishizaki
b2174da15b [CodeCompletion] Update for actor isolation
For cross actor references:
* Annotate with 'async'
* "Not recommended" if there're non-'Sendable' arguments or return types

rdar://72200120
2021-03-24 15:52:44 -07:00
Rintaro Ishizaki
c991347160 [IDE] Explicitly specify 'SerializedOK=false' to 'getRawComment()'
This ensures that we don't try getRawComment() from serialized locations

rdar://problem/75010520
2021-03-18 17:30:36 -07:00
Ben Langmuir
222e2c7f4c Lift ImportDepth utility out of SourceKit into IDE (NFC) 2021-03-09 13:52:56 -08:00
Doug Gregor
5013d20b86 Merge pull request #35784 from DougGregor/se-0296 2021-02-05 20:10:48 -08:00
Doug Gregor
238290cdc4 [SE-0296] Enable async/await by default.
Always parse `async` and `await`, allowing the definition and use of
asynchronous functions without the "experimental concurrency" flag.

Note that, at present, use of asynchronous functions requires one to
explicitly import or link against the `_Concurrency` library. We'll
sort this out in a follow-up change.

Tracked by rdar://73455330.
2021-02-05 09:51:46 -08:00
shreyaa-sharmaa
2b11d7acc7 Task SR-14138 Replace std::transform with llvm::transform 2021-02-04 12:58:33 +05:30
Rintaro Ishizaki
b856c15513 [CodeCompletion] Mark async calls "NotRecommended" in non-async context
rdar://problem/72198532
2021-01-13 11:46:54 -08:00
Rintaro Ishizaki
8ef504acf3 Merge pull request #35050 from rintaro/ide-completion-rdar72198530
[CodeCompletion] Annotate 'async' functions/initializers in results
2020-12-18 09:29:07 -08:00
Rintaro Ishizaki
4c38ec5a4a Merge pull request #35069 from rintaro/ide-completion-rdar72199413
[Parse/CodeCompletion] Implement effects specifier completion
2020-12-15 13:11:18 -08:00
Rintaro Ishizaki
4284a51589 [Parse/CodeCompletion] Implement effects specifier completion
Rewrote and rename 'parseAsyncThrows' to 'parseEffectsSpecifiers'.
Implemented 'CodeCompletionCallbacks::completeEffectsSpecifier()'
2020-12-14 12:38:15 -08:00
Rintaro Ishizaki
4286c44e72 [CodeCompletion/SourceKit] Consolidate some chunk kinds
Consolidate ThrowsKeyword, RethrowsKeyword, and AsyncKeyword to
EffectsSpecifierKeyword.
Abolish 'key.throwsoffset' and 'key.throwslength' as they aren't used.
2020-12-14 12:25:30 -08:00
Rintaro Ishizaki
5a7af5c673 [CodeCompletion][NFC] Rename addAsyncThrows to addEffectsSpecifiers 2020-12-14 12:24:44 -08:00
Rintaro Ishizaki
be7466eb3f [CodeCompletion] Annotate 'async' functions/initializers in results
Similar to how it shows "throws" today.

Introduced CodeCompletionStringChunk::ChunkKind::AsyncKeyword

rdar://problem/72198530
2020-12-14 11:03:24 -08:00
Rintaro Ishizaki
82eca57e13 [CodeCompletion] Complete 'await' in expression position
rdar://problem/72199267
2020-12-11 11:53:32 -08:00
Richard Wei
de2dbe57ed [AutoDiff] Bump-pointer allocate pullback structs in loops. (#34886)
In derivatives of loops, no longer allocate boxes for indirect case payloads. Instead, use a custom pullback context in the runtime which contains a bump-pointer allocator.

When a function contains a differentiated loop, the closure context is a `Builtin.NativeObject`, which contains a `swift::AutoDiffLinearMapContext` and a tail-allocated top-level linear map struct (which represents the linear map struct that was previously directly partial-applied into the pullback). In branching trace enums, the payloads of previously indirect cases will be allocated by `swift::AutoDiffLinearMapContext::allocate` and stored as a `Builtin.RawPointer`.
2020-11-30 15:49:38 -08:00
Nathan Hawes
240a26244d [CodeCompletion] Rename isSingleExpressionBody to isImplicitSingleExpressionReturn for clarity
This bool was true only for single-expression closure and function bodies that
do not have an explicit `return` in the source. Rename it and its associated
methods, to avoid confusion with the hasSingleExpressionBody methods on
ClosureExpr and AbstractFuncDecl. Those don't care whether the expression was
explicitly written in the source or not.
2020-11-19 13:33:57 +11: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
Pavel Yaskevich
51bd8d93d6 Merge pull request #34510 from maustinstar/sr-11711
[SR-11711]  [Parse] Single-expression implicit returns within #if declarations
2020-11-05 21:14:44 -08:00
Rintaro Ishizaki
d221f14215 [CodeCompletion] Deduplicate results in ExprContextInfo
Using canonical types. Otherwise 'Optional<T>' and 'T?' aren't
considered the same.

Also, for example, the expected context types are 'T?' and 'T', don't
get results from 'T' twice.

rdar://problem/71063455
2020-11-05 11:32:16 -08:00
maustinstar
f6c9769bf0 Statement setter for last element 2020-11-03 22:20:23 -05:00
maustinstar
37b17338e5 [SR-11711] Single-expression returns for #if declarations within functions 2020-11-03 12:47:39 -05:00
Ben Barham
95ae19255e [CodeCompletion] Do not include decl in own (string interp) initializer
Strings are a single token, so the previous check would treat
completions inside string interpolations as being outside of the
initializer.

Grab the end of the token from the Lexer, but wrap in a context check to
avoid performing that for every declaration found in the lookup.

Resolves rdar://70833348
2020-11-03 16:42:28 +10:00
Ben Barham
4cf7426698 [CodeCompletion] Do not include decl completion in its own initializer
`::lookupVisibleDecls` had an inline consumer in order to remove
"unusable" results. Refactor this method, moving the consumer (now
`UsableFilteringDeclConsumer`) to allow its use when looking up top
level module declarations.

Also use the `AccessFilteringDeclConsumer` in preference to a condition
in `addVarDecl`.

Resolves rdar://56755598
2020-11-02 08:49:05 +10:00
Nathan Hawes
62a5fb1698 [CodeCompletion] Dedupe completion results if multiple lookups were made.
Solver-based member completion performs a lookup per solution, but if the base
types in each solution are variations of the same generic (e.g. Array<Int>,
Array<String>), we can end up with the same result appearing twice (e.g. count)
2020-10-26 15:41:24 -07:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
Brent Royal-Gordon
e843a16649 Merge pull request #33935 from brentdax/these-imports-are-testing-my-patience
Add -testable-import-module frontend flag (+ refactoring)
2020-10-11 12:49:09 -07:00
Brent Royal-Gordon
b440ab7331 [NFC] Move several types/functions to Import.h
To help consolidate our various types describing imports, this commit moves the following types and methods to Import.h:

* ImplicitImports
* ImplicitStdlibKind
* ImplicitImportInfo
* ModuleDecl::ImportedModule
* ModuleDecl::OrderImportedModules (as ImportedModule::Order)
* ModuleDecl::removeDuplicateImports() (as ImportedModule::removeDuplicates())
* SourceFile::ImportFlags
* SourceFile::ImportOptions
* SourceFile::ImportedModuleDesc

This commit is large and intentionally kept mechanical—nothing interesting to see here.
2020-10-09 18:57:07 -07:00
Nathan Hawes
a91cede79b [Parse][CodeCompletion] Don't special case code completion when forming single-expression closures/function bodies (NFC)
Code completion used to avoid forming single expression closures/function
bodies when the single expression contained the code completion expression
because a contextual type mismatch could result in types not being applied
to the AST, giving no completions.

Completions that have been migrated to the new solver-based completion
mechanism don't need this behavior, however. Rather than trying to guess
whether the type of completion we're going to end up performing is one of
the ones that haven't been migrated to the solver yet when parsing, instead
just always form single-expression closures/function bodies (like we do for
regular compilation) and undo the transformation if and when we know we're
going to perform a completion kind we haven't migrated yet.

Once all completion kinds are migrated, the undo-ing code can be removed.
2020-10-09 16:02:13 -07:00