Commit Graph

434 Commits

Author SHA1 Message Date
Alex Hoppen
eb8df860e8 Merge pull request #58522 from ahoppen/pr/no-circular-supertypes
[CodeCompletion] Avoid creating circles in the USRBasedType supertype hierarchy
2022-05-03 09:43:08 +02:00
Alex Hoppen
b4fca1a40e [CodeCompletion] Avoid creating circles in the USRBasedType supertype hierarchy
You would think that superclass + conformances form a DAG. You are wrong!
We can achieve a circular supertype hierarcy with

```swift
protocol Proto : Class {}
class Class : Proto {}
```

USRBasedType is not set up for this. Serialization of code completion results from global modules can't handle cycles in the supertype hierarchy
because it writes the DAG leaf to root(s) and needs to know the type offsets. To get consistent results independent of where we start
constructing USRBasedTypes, ignore superclasses of protocols. If we kept track of already visited types, we would get different results depending on
whether we start constructing the USRBasedType hierarchy from Proto or Class.
Ignoring superclasses of protocols is safe to do because USRBasedType is an under-approximation anyway.

rdar://91765262
2022-04-29 12:53:17 +02:00
Alex Hoppen
0a79ab7637 [IDE] Add test case for SR-14708
SR-14708 is no longer happening in the stress tester. Add its test case to the test suite.

rdar://78781625
2022-04-22 09:50:24 +02: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
f2e1f30f66 [CodeCompletion] Add test case for SR-15502 2022-04-06 09:19:12 +02:00
Alex Hoppen
458ce70245 Merge pull request #41633 from ahoppen/pr/solver-based-global-completions
[CodeCompletion] Migrate expression completions to solver-based
2022-03-21 20:02:47 +01:00
Alex Hoppen
e2a62f1a60 [CodeCompletion] Migrate expression completions to solver-based 2022-03-21 13:00:33 +01:00
Alex Hoppen
1680ec3f08 Merge pull request #41853 from ahoppen/pr/add-fixed-test-cases
[CodeCompletion] Add test cases
2022-03-18 17:09:11 +01:00
Alex Hoppen
f538d33e5f [CodeCompletion][Sema] Migrate CallArgurment position completion to the solver-based implementation
This hooks up call argument position completion to the typeCheckForCodeCompletion API to generate completions from all the solutions the constraint solver produces (even those requiring fixes), rather than relying on a single solution being applied to the AST (if any).

Co-authored-by: Nathan Hawes <nathan.john.hawes@gmail.com>
2022-03-17 15:15:54 +01:00
Alex Hoppen
4af49e3479 [CodeCompletion] Add test cases
Add test cases for three issues that have been fixed on main
- SR-14693
- SR-14704
- SR-14739
2022-03-17 09:03:17 +01:00
Alex Hoppen
f42f961faf [Sema] Copy key path component types when merging solutions
In 2eeff365b1 I forgot to copy key path component types when applying a solution to the constraint system. That caused a crash in key path code completion.

Fixes rdar://81118700 [SR-14979]
2021-07-30 11:07:21 +02:00
Alex Hoppen
1a7d98795c Merge pull request #38474 from ahoppen/pr/dont-precheck-twice
[CodeCompletion] Allow preChecking an expression twice in code completion
2021-07-28 21:51:16 +02:00
Alex Hoppen
fec7f6c3c0 [CodeCompletion] Fix issue causing the completion status to not be set correctly for pattern completion
We weren’t setting the code completion token status correctly when parsing patterns with code completion tokens.

Because of this, in the added test case, the `searchSubject` gets added as a member of `Foo` twice - once in the first pass and once in the second pass, causing an assertion failure.

Fixes rdar://80575116 [SR-14687]
2021-07-26 20:50:10 +02:00
Hamish Knight
2d951efd8b [CS] Don't bail out of CollectVarRefs early
Upon encountering an ErrorExpr, we were previously
bailing from the walk. However, for multi-statement
closures, that could result in us missing some
variable references required to connect to the
closure to its enclosing context. Make sure to
continue walking to catch those cases.

SR-14709
rdar://78781677
2021-07-21 23:04:06 +01:00
Alex Hoppen
202905fe40 [CodeCompletion] Allow preChecking an expression twice in code completion
In code completion we might call `preCheckExpression` twice - once for the first pass and once for the second pass. This is fine since `preCheckExpression` idempotent, so don't assert if we are in code completion mode.

Fixes rdar://79136653 [SR-14755]
2021-07-21 11:16:16 +02:00
Hamish Knight
74b6dd8404 [test] Fix validation-test/IDE/slow/rdar45511835.swift 2021-07-19 15:24:06 +01:00
Rintaro Ishizaki
aa182ac8e0 [Parse] Don't skip '}' when recovering from 'default' outside 'switch'
That caused assertion failures in code completion.

rdar://78781163
2021-06-23 08:44:32 -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
3b2372307c Merge pull request #37421 from ahoppen/pr/fix-crash-complete-in-result-builder-with-nil
[CodeComplete] Fix crash when completing inside a result builder containing a variable initialized with `nil`
2021-05-17 18:56:45 +02:00
Alex Hoppen
ad5dc2d76f [CodeComplete] Fix crash when completing inside a result builder containing a variable initialized with nil
Resolves rdar://78017503
2021-05-14 17:21:26 +02:00
Alex Hoppen
4566bf3136 [Sema] Remove TypeCheckExprFlags::AllowUnresolvedTypeVariables
Remove the `TypeCheckExprFlags::AllowUnresolvedTypeVariables` flag, fixing another occurance of rdar://76686564 (https://github.com/apple/swift/pull/37309)

This does not break anything in the test suite, so I think the removal of the flag is fine.

Resolves rdar://76686564 and rdar://77659417
2021-05-10 20:11:43 +02:00
Alex Hoppen
759419190e Merge pull request #36992 from ahoppen/pr/wrapping-lvalue
[TypeChecker] Clear param specifiers before re-typechecking expression for completion
2021-05-07 14:10:45 +02:00
Alex Hoppen
74ff6923a1 [Sema] Don’t allow unresolved type variables if LeaveBraceStmtBodyUnchecked is true
According to Pavel, we want to eliminate allowing unresolved variables as much as possible. Removing this flag doesn’t break any test cases (at least not in a meaningful way) and fixes a crasher, so it seems reasonable to remove it.

Fixes rdar://76686564
2021-05-06 10:19:05 +02:00
Alex Hoppen
b1a4708782 [TypeChecker] Clear param specifiers before re-typechecking expression for completion
Because we are completing inside a result builder, we are never calling into `typeCheckExpression` and thus never call into `typeCheckForCodeCompletion` before `fallbackTypeCheck` (SR-14601).

This works fine in most cases, but in the added test case, we are hitting an assertion because the specifiers are not correctly erased from the `ClosureExpr` before re-typechecking for completion. Erasing them before fixes the test case until the underlying issue described above is fixed.

Fixes rdar://76710904 [SR-14494]
2021-05-06 10:00:25 +02:00
Nathan Hawes
b8f5bf3434 [CodeCompletion][Parse] Don't drop the contained expression when completing within an InOutExpr
When completing within an InOutExpr like `&foo.<complete>`, we were forming a
CodeCompletionExpr with a base when parsing the content of the InOutExpr and
storing it in CodeCompletionCallbacksImpl::CodeCompleteTokenExpr. When the
result of that parse contained a code completion though, we were dropping the
sub-expression completely and forming an empty code completion result in place
of the inout expression, which resulted in later code inserting a code
completion expression with no base into the AST. The solver based completion
was later asking for the type of the code completion and its base using the
expression stored in CodeCompletionCallbacksImpl::CodeCompleteTokenExpr, but
that never ended up in the AST so we hit an assertion about the expression not
have a type in the formed solutions.

Resolves rdar://75366814
2021-04-03 13:39:23 +10:00
Alex Hoppen
2c0b8ac479 Merge pull request #36517 from ahoppen/pr/rdar64141399
[CodeComplete] Fix code completion of initialization for variable wrapped by generic property wrapper
2021-03-26 11:24:52 +01:00
Alex Hoppen
ef75cf7c77 Merge pull request #36534 from ahoppen/pr/rdar64265821
Add test case for rdar://64265821
2021-03-23 08:29:33 +01:00
Alex Hoppen
efbfbc4200 Merge pull request #36495 from ahoppen/pr/rdar71566576
Add test case for rdar://71566576
2021-03-22 17:32:52 +01:00
Alex Hoppen
86e5e7dbf3 Add test case for rdar://64265821
rdar://64265821 [SR-12985] appears to have been fixed already. Let’s add a test case for it.
2021-03-22 14:02:11 +01:00
Alex Hoppen
28ee60e893 [CodeComplete] Fix code completion of initialization for variable wrapped by generic property wrapper
If completing the initialization of a variable that’s wrapped in a generic, unbound property wrapper, the expression's type is an `UnboundGenericType`. AFAICT that’s expected and the correct type to assign.

We hit the first assertion failure by trying to retrieve that type's substitution map. `UnboundGenericType`s were not handled here, so we crashed. AFAICT we can't get any substitutions out of an unbound generic type, so we should just continue looking into the parent type.

We hit the second assertion when trying to retrieve the property wrapper’s backing type, which is null (becuase it couldn't be resolved). However, we haven’t emitted a diagnostic because the variable declaration is perfectly valid. Hence I’m removing the assertion.

Fixes rdar://64141399
2021-03-19 20:51:48 +01:00
Alex Hoppen
dc88b4b735 Add test case for rdar://71566576
rdar://71566576 appears to have been fixed already. Still, add a test case for it to ensure we don’t hit it again.
2021-03-19 19:54:20 +01:00
Alex Hoppen
180dd2b2c9 [IDE] Add test cases for compiler crashes fixed by reverting 9d3c8ca 2021-03-19 19:38:52 +01:00
Robert Widmann
318f772e91 Check The DeclContext When Retrieving Conforming Decls
Checking for staticness is not enough because top-level decls count as
non-static yet do not have a sufficient curry level to satisfy this
check. Make sure we're looking for decls that reside inside of types.

rdar://75299825, SR-14327
2021-03-11 14:26:38 -08:00
Rintaro Ishizaki
d78bf22413 [CodeCompletion] Fix crashes in TypeCheckASTNodeAtLocRequest
receive {
    switch <HERE>
  }

In the current Parser implementation, if there's a error in 'switch'
subject, the 'SwitchStmt' is not placed in the parsed AST.

When the type checker searches AST node at the location, it used to find
'receive { ... }' because that is the *innermost* node in the AST that
contains the loc. But the decl context is the closure. This behavior used
to cause crashes because 'receive { ... }' is re-typechecked.

For solution, when finding the innermost AST node, clear the found node
in parent context before walking into brace statement.

rdar://problem/69246891
2020-10-30 15:56:31 -07:00
Rintaro Ishizaki
84205e3c97 [CodeCompletion] Fix a crash in collectPossibleReturnTypesFromContext
Avoid re-typechecking for nested closures. For example:

  Something {
    Other {
      #^COMPLETE^#
    }
  }

If 'Something' is successfully type checked but 'Other' is failed, the
outer closure has type, but the inner closure doesn't. In such state,
when the type of 'Other' is requested, the outer closure used to be
re-typechecked. That may cause crash because it may contain expressions
CSGen doesn't expect.

rdar://problem/69246891
2020-10-28 15:21:13 -07:00
Rintaro Ishizaki
d9a9b59634 [CodeCompletion] Skip an ASTScope assertion for labeled statements
Code completion performs 'typeCheckASTNodeAtLoc()' which ignores
'StmtChecker::ActiveLabeledStmts'. Since this is not necessary for code
completions, skip an assertion to verify the correctness of ASTScope.

rdar://problem/67102611
2020-08-14 16:01:47 -07:00
Rintaro Ishizaki
f7fc1ed7b5 [CodeCompletion] Fix a crash in CCExprRemover
- Handle cases where getArgumentLabelLocs().size() == 0
- Add some assertions to verify invariants
- Explicit handling of 'llvm::Optional' for 'getUnlabeledTrailingClosureIndex()'
- Avoid walking into nodes after the removing happens

rdar://problem/65556791
2020-07-15 09:56:20 -07:00
Rintaro Ishizaki
0e7e4baa71 [CodeCompletion] Add an already-fixed crashing test case
rdar://problem/58470999
2020-05-14 09:18:29 -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
Argyrios Kyrtzidis
bbdada475b [Parse] Try to preserve the invariant that anything that contains KeyPathDotExpr must be wrapped in KeyPathExpr
Even in the presence of invalid code. Fixes typechecker crash in rdar://50666427
2020-02-20 15:07:00 -08:00
Rintaro Ishizaki
0ca869e40e Merge pull request #29118 from rintaro/ide-completion-rdar56834798
[CodeCompletion] Allow type variable in MakeAbstractConformanceForGenericType
2020-02-19 09:30:08 -08:00
Hamish Knight
5ed2847f4e Create a new module and SourceFile for REPL completion
Rather than attempting to temporarily insert decls
into the last source file, just create a new module
and source file and carry across the imports from
the last module. This matches how the REPL deals
with new lines of input.
2020-02-05 14:01:20 -08:00
Marc Rasi
d608220360 negative test for SR-12076: completion crash 2020-01-24 10:12:39 -08:00
Rintaro Ishizaki
4ced714e25 [CodeCompletion] Allow type variable in MakeAbstractConformanceForGenericType
When typesSatisfyConstraint() is called with 'openArchetypes=true',
archetypes are substituted with type variables. If they have
conformances, they used to hit assertion in
'MakeAbstractConformanceForGenericType::operator()'.

Adjust the assetion to accept 'TypeVariableType'.

rdar://problem/56834798
2020-01-09 23:18:27 -08:00
Rintaro Ishizaki
091c36bc82 [CodeCompletion] Evaluate 'PatternBindingEntry' before checking the init
If a completion happens in an 'PatternBindingInitializer' context for
'TypedPattern' without any 'VarDecl', e.g.:

    let _: Int = <COMPLETION>

it crashes because 'typeCheckPatternBinding()' requires that the
'TypedPattern' has the type.

rdar://problem/52105899
2019-12-25 16:30:52 -08:00
Rintaro Ishizaki
cb00b8faf9 Revert "Merge pull request #27120 from rintaro/parser-rdar55267292"
This reverts commit e1b51f32aa, reversing
changes made to 086eb07ede.
2019-10-14 13:39:48 -07:00
Rintaro Ishizaki
da95961de2 [SyntaxParse] Fix a crash in TokenReceiver
rdar://problem/55267292
2019-09-11 10:51:07 -07:00
Slava Pestov
1caf2c7544 Sema: Fix code completion crash when a ParamDecl hasn't had its type set yet
We set the type of ParamDecls when applying solutions in the normal path, but
sometimes code completion will type check an expression inside a closure without
checking the outer expression. In this case, we may have inferred a type for
the ParamDecl, but we don't write it back.

Instead, just look at the DeclRefExpr's type.

Fixes <rdar://problem/42098113>.
2019-04-02 00:25:24 -04:00
Rintaro Ishizaki
d01e80d499 Merge pull request #23308 from rintaro/ide-completion-rdar48896424
[CodeCompletion] Fix an assertion failure
2019-03-14 14:53:54 -07:00
Rintaro Ishizaki
71aeea93f5 [CodeCompletion] Fix an assertion failure
If the type has unbound generic parameter, we cannot substitute types
for member decls.

rdar://problem/48896424
2019-03-14 12:22:19 -07:00