Commit Graph

483 Commits

Author SHA1 Message Date
Pavel Yaskevich
9406edc017 [Diagnostics] Tailored diagnostic for missing argument in property wrapper init 2019-09-23 13:00:37 -07:00
Pavel Yaskevich
60bcc94575 [Diagnostics] Implement single missing argument diagnosis
If call is missing a single argument we can provide a tailored
diagnostic and suggest a fix-it to add it at the appropriate
position.
2019-09-23 13:00:37 -07:00
Pavel Yaskevich
ee8c78eef5 [Diagnostics] Tailored diagnostic when single tuple used instead of N distinct arguments
Diagnose cases when instead of multiple distinct arguments
call got a single tuple argument with expected arity/types:

```swift
func foo(_: Int, _: Int) {}
foo((0, 1)) // expected 2 arguments, got 1 tuple with 2 elements
```
2019-09-23 13:00:37 -07:00
Pavel Yaskevich
4203c2f9f3 [Diagnostics] Store synthesized arguments in missing arguments diagnostic
Since we are about to start diagnosing more than just closures,
we need information about what synthesized arguments look like.
2019-09-23 12:59:27 -07:00
Pavel Yaskevich
96598d8ebf Merge pull request #26207 from owenv/new-vararg-conversion-diag
[Diagnostics] Improve diagnostic when attempting to pass an Array to a variadic argument
2019-09-23 12:57:44 -07:00
Owen Voorhees
6c5185f2e3 Improve diagnostic when attempting to pass an Array to a variadic argument
- Give a more specific diagnostic which indicates the parameter is variadic
- If the argument is an Array literal, offer to drop the brackets
2019-09-20 17:30:23 -07:00
Holly Borla
29044e5d34 Merge pull request #27176 from hborla/type-cannot-conform-diag
[Diagnostics] Generalize the "protocol type cannot conform" error to work for all types that cannot conform to protocols.
2019-09-18 10:38:00 -07:00
Holly Borla
bf80250c84 Merge remote-tracking branch 'upstream/master' into type-cannot-conform-diag 2019-09-17 11:54:03 -07:00
Pavel Yaskevich
0aeaf7f15e [Diagnostics] NFC: Remove "trailing" from method name for closures with missing args
`MissingArgumentsFailure::diagnoseClosure` can actually diagnose both
closures in argument positions as well as when their type comes from
context e.g. `let _: (Int) -> Void = {}`.
2019-09-17 10:34:44 -07:00
Holly Borla
2a7e0099d2 [Diagnostics] Add notes for the type_cannot_conform error that point
to the declaration that requires protocol conformance.
2019-09-17 09:22:07 -07:00
Pavel Yaskevich
2f325fa1d8 [Diagnostics] Don't store contextual type in missing argument fix/diagnostic
Instead of storing contextual function type in the fix/diagnostic,
let's fetch it from context (solution and/or locator) because it's
only used when it is a trailing closure missing some arguments anyway.
2019-09-16 16:50:31 -07:00
Holly Borla
173e4526ee [Diagnostics] Adjust wording of type_cannot_conform error message. 2019-09-16 14:55:38 -07:00
Pavel Yaskevich
c20e4ac3c2 [Diagnostics] Fix usages of getChoiceFor 2019-09-13 22:35:53 -07:00
Pavel Yaskevich
b898eaf326 [Diagnostics] Tailored diagnostics for reference equality operator mismatches 2019-09-13 22:35:52 -07:00
Pavel Yaskevich
973d58d56f [Diagnostics] Provide a tailored diagnostic for operator mismatch on missing conformance
If missing conformance is between two stdlib defined types which
are used in operator invocation, let's produce a generic diagnostic
about operator reference and a note about missing conformance.
2019-09-13 22:35:52 -07:00
Pavel Yaskevich
90d725f701 [Diagnostics] Add special ambiguity diagnostics for operators
In cases when all of the fixed solutions have only one problem in
common - different overloads of a certain operator, let's
produce a tailored diagnostic and suggest matching partial
overloads along side diagnostic notes which point to each choice.
2019-09-13 22:35:52 -07:00
Pavel Yaskevich
015129cb36 [Diagnostics] Allow requirement failure to diagnose operators
In absence of general argument conversion failures requirement
errors associated with operators couldn't be diagnosed properly,
now this restriction could be lifted.
2019-09-13 22:35:51 -07:00
Pavel Yaskevich
93b39c9c23 [Diagnostics] Port pattern-matching mismatch diagnostic
Port diagnostics associated with implicit use of `~=` operator
which is used in `case` statements.
2019-09-13 22:35:50 -07:00
Pavel Yaskevich
65c966ba16 [Diagnostics] Port a custom diagnostic for archetype mismatch in argument position 2019-09-13 22:35:50 -07:00
Pavel Yaskevich
34a12aa30e [Diagnostics] Add generic argument-to-parameter conversion failure
No specialized diagnostics yet, just a basic infrastructure to
produce generic a `cannot convert X to expected argument type Y`
message.
2019-09-13 22:35:50 -07:00
Hamish Knight
b9ad96a944 [CS] Add locator version of findSelectedOverloadFor 2019-09-10 12:06:35 +01:00
Hamish Knight
c745c17c38 Revert naming changes to getCalleeLocator & getChoiceFor
In addition, add a document comment to `getCalleeLocator` to clarify
its semantics.
2019-09-09 22:08:52 +01:00
Hamish Knight
dba850f16c [CSDiagnostics] Use getAnchormostCalleeLocator
Have FailureDiagnostic::getChoiceFor take a ConstraintLocator argument
which is passed through to getAnchormostCalleeLocator, and rename to
getAnchormostChoiceFor to make the semantics clear. In addition, add
a convenience getAnchormostChoice member for the common case of getting
the choice for the anchor of the failure's locator.

This change means we can now resolve callees for failures associated
with key path subscript components.

Resolves SR-11435.
2019-09-09 14:14:35 +01:00
Suyash Srijan
ab1184208a [CSDiagnostics] Offer a fix-it to conform the decl context to the missing protocols during contextual failure 2019-09-05 02:50:24 +01:00
Suyash Srijan
bd53fe355d Revert "[CS] Don't crash when default argument is magic literal and types don't match" 2019-08-31 00:54:37 +01:00
Pavel Yaskevich
b9cf4fa6c2 [Diagnostics] Add a diagnostic for invalid conversion of inout argument/parameter
Since there is no subtyping allowed in `inout` positions, let's
produce a tailored error message and a note about that.
2019-08-28 12:19:02 -07:00
Hamish Knight
86995f732f [CSDiagnostics] Add a couple of asserts to RequirementFailure
This will make it slightly easier to spot cases where we've resolved
to the wrong affected decl.
2019-08-27 15:59:08 +01:00
Pavel Yaskevich
b813976fd1 [Diagnostics] Port diagnostic for CTP_YieldByReference
Last special case from `FailureDiagnosis::diagnoseContextualConversionError`
has been ported to the new diagnostic framework.
2019-08-21 13:47:39 -07:00
Pavel Yaskevich
8296b554f4 [Diagnostics] Port tailored contextual diagnostic when thrown type doesn't conform to Error
`throw` statements are type-checked as having contextual `Error`
type to make sure that thrown type conforms to `Error` protocol.
Let's make sure that's correctly handled by new diagnostics framework.

```swift
func foo() throws {
  throw 0 // `Int` doesn't conform to `Error` protocol.
}
```
2019-08-20 17:03:19 -07:00
Pavel Yaskevich
0a8f596736 [Diagnostics] Port contextual mismatches involving nil to new framework
Detect and diagnose contextual failures originating in an attempt
to convert `nil` to some other non-optional type e.g.

```swift
let _: Int = nil // can't initialize `Int` with `nil`

func foo() -> Int {
  return nil // can't return `nil` from `foo`
}

_ = 1 + nil // there is no `+` overload which accepts `Int` and optional
```
2019-08-20 14:23:54 -07:00
Pavel Yaskevich
98a54a1e73 Merge pull request #26297 from hamishknight/pass-by-value-by-name
[CS] Add specific accessors for path element info
2019-08-20 01:04:59 -07:00
Pavel Yaskevich
7ac754acc4 [Diagnostics] Transform incorrect generic arguments into a contexual mismatch
Since this kind of failure is really a conversion failure, let's
inherit from `Contextual{Mismatch, Failure}` which also helps with
storage for from/to types and their resolution.

Also let's use original types involved in conversion to form
this fix, which helps to perserve all of the original sugar.
2019-08-19 09:56:48 -07:00
Hamish Knight
c225d52873 [CS] Add locator last element casting members
These members provide a convenient way of casting the last element of
a locator to a given path element type.
2019-08-19 11:58:50 +01:00
Hamish Knight
a0919f73b0 [CS] Use subclasses to expose locator element info
Instead of adding specific accessors directly to
ConstraintLocator::PathElement, add subclasses that expose these
accessors.
2019-08-19 11:58:49 +01:00
Hamish Knight
b5b41a5bbe [CS] Add specific accessors for path element info
This commit replaces the `getValue()` and `getValue2()` members on
`ConstraintLocator::PathElement` with specific accessors for each
expected path component kind. IMO this adds some clarity to the call
sites, especially for `getArgIdx()` and `getParamIdx()`.

In addition, this commit adds a private `getValue` member that can
access a value at a given index, which will make it easier to add a
third value in the future.
2019-08-19 11:58:49 +01:00
Pavel Yaskevich
fc3d9755fb Merge pull request #26677 from xedin/dedup-generic-requirements
[Diagnostics] Correctly identify location of requirement failure
2019-08-17 00:02:08 -07:00
Pavel Yaskevich
cbadd7ffc2 [Diagnostics] Correctly identify location of requirement failure
Previously in situations like:

```swift
protocol P {}

struct S<T: P> {
  var value: T
}

_ = S(value: 42)
```

Diagnostic has reported a problem as related to "reference" to `init`
but the failing generic type requirement belongs to `S`, so a
better diagnostic in such case should mention `generic struct S`.
2019-08-16 22:02:37 -07:00
Holly Borla
cc1082146d [Diagnostics] Unify DiagnosticFailure and ConstraintFix classes for extra & failures.
Make `InvalidUseOfAddressOf` a `ContextualFailure`, make `ReturnAddressOf`
a `ContextualMismatch`, and extend this failure to cover using `&` with a
non-inout argument.
2019-08-16 17:43:59 -07:00
Holly Borla
d299925ae3 [Diagnostics] Port the "extra address of agrument" diagnostic to the new
framework.
2019-08-16 11:55:11 -07:00
Pavel Yaskevich
253abad789 [Diagnostics] Port remaining contextual failures (expect associated with nil)
Remove the rest of the obsolete code from `diagnoseContextualConversionError`
and port diagnostic about string indexing to new diagnostic framework.
2019-08-13 14:33:38 -07:00
Pavel Yaskevich
15ae692da0 [ConstraintSystem] Repair and diagnose failures relared to throws mismatch
If the only difference between two functions is `throws` and it
is not a subtype relationship, let's repair the problem by dropping
`throws` attribute and letting solver continue to search for
a solution, which would later be diagnosed.
2019-08-13 11:55:08 -07:00
Pavel Yaskevich
fd85bfe045 [Diagnostics] Port tailored diagnostic for dictionary conversions
Detect that contextual conversion is between a dictionary type
and an array type which comes form an array literal and produce
a tailored diagnostic.
2019-08-13 11:55:08 -07:00
Pavel Yaskevich
90e38af306 [Diagnostics] Port type coercion fix-it to contextual failure 2019-08-13 11:55:08 -07:00
Pavel Yaskevich
69589f01a6 [Diagnostics] Port integer cast fix-it to contextual failure 2019-08-13 11:55:08 -07:00
Pavel Yaskevich
27b9d37a82 [Diagnostics] Port raw representable fix-it to contextual failure 2019-08-13 11:55:08 -07:00
Pavel Yaskevich
fba0cfa2a2 [Diagnostics] Port tailored diagnostic for optional -> bool contextual conversion 2019-08-13 11:55:08 -07:00
Pavel Yaskevich
a4d7cbd794 [Diagnostics] Extend contextual failure diagnostic to support all basic mismatches 2019-08-13 11:55:08 -07:00
Pavel Yaskevich
385fb0c665 [Diagnostics] Make force downcast fix contextual and move it to repairFailures
This way it covers a lot more ground and doesn't conflict with
other fixes.

Another notable change is related to check for IUO associated
with source type, that covers cases like:

```swift
func foo(_ v: NSString!) -> String {
  return v
}
```

Instead of general conversion failure check for IUO enables solver
to introduce force downcast fix.
2019-08-09 01:09:52 -07:00
Pavel Yaskevich
a34fe9ae55 [Diagnostics] Extend missing & diagnostic to cover pointer conversions
Example:

```swift
func foo(_: UnsafePointer<Int>) {}

var x: Int = 0
foo(x) <- should suggest adding `&`
```
2019-08-06 17:52:24 -07:00
Holly Borla
edcf4055d2 [Diagnostics] Use ContextualFailure and ContextualMistatch as base classes
for `AutoClosurePointerConversionFailure` and `AllowAutoClosurePointerConversion`
respectively.
2019-08-02 15:23:25 -07:00