Commit Graph

1513 Commits

Author SHA1 Message Date
Pavel Yaskevich
1683fb91b6 [ConstraintSystem] Remove RValueAdjustment locator element
It was used for unresolved member and `.dynamicType` references
as well as a couple of other places, but now unresolved member
references no longer need that due to new implicit "chain result"
AST node and other places could use more precise locators
e.g. new `.dynamicType` locator or `sequence element` for `for in`
loops.
2020-10-26 00:40:45 -07:00
Pavel Yaskevich
860e14ecdf [CSGen] Remove redundant r-value adjustment constraint
Since unresolved members now introduce an implicit expression
during pre-check, there is no need to add a separate r-value
adjustment constraint to connect "tail" of the chain with
chain result.
2020-10-23 17:04:57 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
Nathan Hawes
15f5222bbd [CodeCompletion][Sema] Allow missing args when solving if the completion location indicates the user may intend to write them later.
func foo(a: Int, b: Int) {}
func foo(a: String) {}

// Int and String should both be valid, despite the missing argument for the
// first overload since the second arg may just have not been written yet.
foo(a: <complete here>

func bar(a: (Int) -> ()) {}
func bar(a: (String, Int) -> ()) {}

// $0 being of type String should be valid, rather than just Int, since $1 may
// just have not been written yet.
bar { $0.<complete here> }
2020-10-19 12:16:19 -07:00
gregomni
2edba9dfbd Instead of chaining binops, favor disjunctions with op overloads whose types match existing binding choices 2020-10-15 09:03:45 -07:00
gregomni
e0199f2d98 Add condition for optimizing series of binops that every possible overload is (T,T)->T, with no (T,T)->U 2020-10-15 09:03:45 -07:00
Pavel Yaskevich
1b708acede Merge pull request #34235 from xedin/cs-header-to-include
[ConstraintSystem] NFC: Move `ConstraintSystem.h` and related headers to `include/swift/Sema`
2020-10-09 16:44:16 -07:00
Nathan Hawes
be231208f3 [CodeCompletion][CSGen] Treat ErrorExprs as holes when generating constraints for code completion
This lets us still provide member completions when the base expression contains parse errors or unresolved decls
e.g. returnsAString(undefined).<complete here>
2020-10-08 19:02:39 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Pavel Yaskevich
ab951c208a [ConstraintSystem] NFC: Move ConstraintGraph{Scope}.h to include/swift/Sema 2020-10-08 10:42:58 -07:00
Pavel Yaskevich
f94be56468 [Sema] Decouple ConstraintSystem and TypeChecker headers 2020-10-06 13:18:49 -07:00
Slava Pestov
ffe21d46dd Sema: Diagnose duplicate capture list entires when parser lookup is off
Another spot where we have to check for redeclaration manually now.
As before, we can use the existing mechanism though.
2020-10-03 09:37:55 -04:00
Pavel Yaskevich
e254f1c3e2 [ConstraintSystem] Infer whether locator is related to return of a single-expression function
This information could be inferred from state recorded in a
constraint system, so to need to record that in the
constraint locator as well.
2020-10-01 11:49:30 -07:00
Pavel Yaskevich
4d0ab13c36 Merge pull request #34089 from xedin/nil-as-a-hole
[Diagnostics] Diagnose cases when it's impossible to infer type for `nil` literal
2020-09-28 12:13:25 -07:00
Pavel Yaskevich
a2b3b54523 [CSGen] Rework constraint generation for nil to avoid failing 2020-09-25 15:01:00 -07:00
Slava Pestov
8ec878bb43 Sema: Use ASTScope::lookupSingleLocalDecl() in CSGen's CollectVarRefs pass
When parse-time lookup is disabled, we have to resolve UnresolvedDeclRefExprs
inside the closure body, since there's no guarantee that preCheckExpression()
has run yet.
2020-09-25 17:59:20 -04:00
Pavel Yaskevich
4c50c00b4d Merge pull request #34046 from xedin/nil-with-context
[CSGen] Check whether parent has a contextual type before diagnosing …
2020-09-23 15:19:45 -07:00
Pavel Yaskevich
90e6fd4792 [CSGen] Check whether parent has a contextual type before diagnosing nil use
Before considering `nil` to be used without a context, let's
check whether parent expression (semantic significance of
which is not important) has a contextual type associated with it,
otherwise it's possible to misdiagnose cases like:

```swift
func test() -> Int? {
  return (nil)
}
```
2020-09-23 11:23:54 -07:00
Pavel Yaskevich
bf58b0c08d [CSGen] Use special accessor to get a type of VarDecl for solver use
Solver needs to handle invalid declarations but only in
"code completion" mode since declaration in question might
not be associated with code completion, otherwise (if constraint
generation fails) there is going to be no completion results.
2020-09-22 16:26:58 -07:00
Slava Pestov
d4cc35a938 AST: Remove VarDecl::hasNonPatternBindingInit() 2020-09-18 16:11:06 -04:00
Nathan Hawes
a1ef6e4dac Merge pull request #33749 from nathawes/new-member-completion
[CodeCompletion] Update member completion to handle ambiguous and invalid base expressions
2020-09-09 18:51:22 -07:00
Suyash Srijan
49e7c6126c [Diagnostics] Diagnose comparisons with '.nan' and suggest using '.isNan' instead (#33860)
* [AST] Add 'FloatingPoint' known protocol kind

* [Sema] Emit a diagnostic for comparisons with '.nan' instead of using '.isNan'

* [Sema] Update '.nan' comparison diagnostic wording

* [Sema] Explicitly check for either arguments to be '.nan' and tweak a comment

* [Test] Tweak some comments

* [Diagnostic] Change 'isNan' to 'isNaN'

* [Sema] Fix a bug where firstArg was checked twice for FloatingPoint conformance and update some comments

* [Test] Fix comments in test file

* [NFC] Add a new 'isStandardComparisonOperator' method to 'Identifier' and use it in ConstraintSystem

* [NFC] Reuse argument decl extraction code and switch over to the new 'isStandardComparisonOperator' method

* [NFC] Update conformsToKnownProtocol to accept DeclContext and use it to check for FloatingPoint conformance
2020-09-09 22:08:42 +01:00
Doug Gregor
b5759c9fd9 [Concurrency] Allow overload 'async' with non-async and disambiguate uses.
Allow an 'async' function to overload a non-'async' one, e.g.,

    func performOperation(_: String) throws -> String { ... }
    func performOperation(_: String) async throws -> String { ... }

Extend the scoring system in the type checker to penalize cases where
code in an asynchronous context (e.g., an `async` function or closure)
references an asychronous declaration or vice-versa, so that
asynchronous code prefers the 'async' functions and synchronous code
prefers the non-'async' functions. This allows the above overloading
to be a legitimate approach to introducing asynchronous functionality
to existing (blocking) APIs and letting code migrate over.
2020-09-08 16:51:10 -07:00
Pavel Yaskevich
20f8a37357 [ConstraintSystem] Allow code completion type to be a hole
Code completion expression type could be a hole if and only
if there is obosolutely no contextual information available
e.g. `let _ = .#^MEMBER^#`
2020-08-31 16:56:05 -07:00
Anthony Latsis
6b1b8377f8 [NFC] CSGen: Tighten up resolveTypeReferenceInExpression
Now that all sites have been refactored to pass a dedicated OpenUnboundGenericType callable,
we can consistently move the construction of the opener into resolveTypeReferenceInExpression
and have it take a ConstraintLocatorBuilder parameter instead
2020-08-31 00:03:33 +03:00
Anthony Latsis
f326927f45 CSGen: Infer generic arguments in typed placeholders 2020-08-30 21:40:42 +03:00
Nathan Hawes
3d8561502b [CodeCompletion] Move CompletionCollector to ASTContext + bug fixes. 2020-08-28 22:24:23 -07:00
Anthony Latsis
5525a7853a Merge pull request #33663 from AnthonyLatsis/unbound-closure-retty
CSGen: Infer generic arguments for explicit closure result types
2020-08-29 03:46:30 +03:00
Rintaro Ishizaki
c48a676a1c Merge pull request #31679 from Jumhyn/implicit-member-chains-different-types
[SE-0287] [Sema] Implementation for implicit member chains
2020-08-28 10:47:53 -07:00
Pavel Yaskevich
4847ec9a6b [AST/TypeChecker] Add more asserts to make sure types don't get into AST 2020-08-27 13:18:45 -07:00
Anthony Latsis
61d86d5fd2 [NFC] CSGen: Clean up some flow in inferClosureType 2020-08-27 22:25:07 +03:00
Anthony Latsis
80560dab25 CSGen: Infer generic arguments in explicit closure result types 2020-08-27 22:17:58 +03:00
Frederick Kellison-Linn
352adc3b5d Remove Argument from UnresolvedMemberExpr
Instead, an expresison like `.foo()` is represented as an `UnresolvedMemberExpr` nested inside a `CallExpr`.
2020-08-26 22:42:30 -04:00
Frederick Kellison-Linn
5de23f5cfc [Sema] Rebase and address feedback for implicit member chains 2020-08-26 22:42:30 -04:00
Frederick Kellison-Linn
0055501670 [Sema] Add FIXME for Optional binding inference bug 2020-08-26 22:42:30 -04:00
Frederick Kellison-Linn
189235678f [Sema] Move member chain transformation into precheck
Move the analysis and transformation of unresolved member chains into the PreCheckExpression pass in order to make sure that contextual types get hooked up properly before we actually begin generating constraints.
2020-08-26 22:42:30 -04:00
Frederick Kellison-Linn
26f0a80599 [Sema] Transfer contextual type info when we inject UnresolvedMemberChainResultExpr
If we create an UnresolvedMemberChainResultExpr at the top level, we may end up losing contextual type information that was already attached to the end of the chain. To avoid this, we fetch any existing contextual type info and transfer it to the newly injected expression.
2020-08-26 22:42:30 -04:00
Frederick Kellison-Linn
1e27f26b01 [Sema] Always cache the unresolved chain base
Even when we’ve already visited the expression for constraint generation (signified by the presence of an UnresolvedMemberChainResultExpr) we should still cache the chain base.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
c33ad11ff6 [Sema] Apply clang-format 2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
4331e26bcd [Sema] Move UnresolvedMemberChainResultExpr creation to walkToExprPre
The old method of generating this placeholder expression caused issues when constraints had to be recursively regenerated during solving. Now, we generate UnresolvedMemberChainResultExprs in walkToExprPre, and use ASTWalker::Parent instead of ConstraintSystem::getParentExpr to find the parent of the chain tail.

This also lets us generate the chain constraints as part of the normal ConstraintGenerator visit, rather than as an extra walkToExprPost step.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
e0e3babcd0 [Sema] Change anchor of unresolved chain result type
Instead of creating the type variable for the unresolved member chain at the location of the last member, we now create it at the associated UnresolvedMemberChainResultExpr.

Previously, when the final element of a chain was a ForceValueExpr, the chain result type got caught up in the logic used to allow ForceValueExprs to properly infer lvalue types. By separating the result type variable from the last member of the chain, we make sure to keep that logic focused only on ForceValueExpr.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
f5845666e6 [AST] Introduce UnresolvedMemberChainResultExpr
Introduce a new expression type for representing the result of an unresolved member chain. Use this expression type instead of an implicit ParenExpr for giving unresolved member chain result types representation in the AST during type checking.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
4e9b7b20db [Sema] Inject implicit ParenExpr for unresolved member chains
In order to give unresolved member chain result types visibility in the AST, we inject an implicit ParenExpr in CSGen that lives only for the duration of type checking, and gets removed during solution application.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
aedde34fec [Sema] Move unresolved base tracking into CSGen
Remove the tracking of unresolved base types from the constraint system, and place it entirely within the generation phase. We have other ways of getting at the base types after generation.
2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
1069fe280f Fixup diagnostics 2020-08-26 22:42:29 -04:00
Frederick Kellison-Linn
5b5e30b6d7 Implement implicit member chains 2020-08-26 22:42:29 -04:00
Pavel Yaskevich
9bc2819317 Merge pull request #33590 from xedin/fail-on-error-if-single-stmt
[CSGen] Abort constraint generation on error only if closure part…
2020-08-24 11:21:08 -07:00
Pavel Yaskevich
94bf392e0e [CSGen] Abort constraint generation on error only if closure participates in type-check
If reference collection discovered at least `ErrorExpr` in the body
of a closure, let's fail constraint generation only if it's a
single-statement closure, decision about multi-statement closures
should be delayed until body is opened.

This helps code completion because `ErrorExpr` could belong to
a statement unrelated to a completion, so it wouldn't affect
its correctness in any way.
2020-08-21 15:37:12 -07:00
Doug Gregor
471f1ee3af [Constraint solver] Disable pattern type "optimization" involving weak types.
Fix a regression introduced by moving the type checking of closure
captures into the constraint system. The pattern-type optimization for
initializations was causing inference of a double-optional where there
shouldn't be one, manifesting in a failure involving implicitly
unwrapped optionals and `weak self` captures.

Fixes rdar://problem/67351438.
2020-08-18 20:58:39 -07:00
Holly Borla
81d87f9b5d Merge pull request #33412 from hborla/remove-default-literal-favoring
[CSGen] Remove favoring based on literals from computeFavoredTypeForExpr
2020-08-14 12:14:55 -07:00