Commit Graph

1205 Commits

Author SHA1 Message Date
Pavel Yaskevich
5fb3a8ba30 Merge pull request #24754 from xedin/diag-missing-contextual-conformances
[ConstraintSystem] Use `missing conformance` fix to diagnose contextual failures
2019-05-17 11:40:49 -07:00
Hamish Knight
894a1e50bf [CS] Consolidate logic forming locators to callees
This commit adds `ConstraintSystem::getCalleeLocator`, which forms a
locator that describes the callee of a given expression. This function
is then used to replace various places where this logic is duplicated.

This commit also changes the conditions under which a ConstructorMember
callee locator is formed. Previously it was formed for a CallExpr with a
TypeExpr function expr. However, now such a locator is formed if the
function expr is of AnyMetatypeType. This allows it to be more lenient
with invalid code, as well as work with DotSelfExpr.

Resolves SR-10694.
2019-05-16 12:07:40 +01:00
Pavel Yaskevich
d5c561b44e [ConstraintSystem] Move missing contextual protocol detection to matchExistentialTypes 2019-05-15 15:37:36 -07:00
Pavel Yaskevich
577e6291a6 [ConstraintSystem] Use missing conformance fix to diagnose contextual failures
Extend use of `missing protocol conformance` fix to cover contextual
failures, such as:

- Assignment mismatches, where destination requires source to conform
  to certain protocol (or protocol composition);
- Incorrect returns where returned type doesn't conform to the
  protocol specified in the signature.
2019-05-15 14:49:17 -07:00
Pavel Yaskevich
1cc6f774a1 [ConstraintSystem] Improve contextual mismatch diagnostics for for ... in <expr> loop 2019-05-15 14:34:24 -07:00
Pavel Yaskevich
b9a0ca6afb [ConstraintSystem] Detect and diagnose conversion failures related to collection element types
Detect and diagnose a contextual mismatch between expected
collection element type and the one provided (e.g. source
of the assignment or argument to a call) e.g.:

```swift
let _: [Int] = ["hello"]

func foo(_: [Int]) {}
foo(["hello"])
```
2019-05-14 17:33:11 -07:00
Sam Lazarus
3d432fea65 Sema / Test: Fix misplaced fix-it for .init calls on instances 2019-05-09 19:17:39 -04:00
Sam Lazarus
2309ee580a Merge pull request #24617 from sl/sl/sr-10297
Add a Diagnostic for .init calls which should be transformed into assignments (SR-10297)
2019-05-09 18:05:38 -04:00
Sam Lazarus
8ad51b298e Sema: Get constraint locator for TupleExpr inside ApplyExpr branch
Additionally has some other minor cleanup.
2019-05-09 11:58:28 -04:00
Pavel Yaskevich
ce406fca04 Merge pull request #24615 from xedin/fix-autoclj-returning-fn-type
[ConstraintSystem] Allow arguments to be passed by value to `@autoclo…
2019-05-09 00:48:53 -07:00
Sam Lazarus
743d8fb0ee Sema: Change isImmutable to isMutable (thanks De Morgan) and negated call 2019-05-08 23:49:58 -04:00
Sam Lazarus
c498f38e5f Sema: Changed name of isInsideCall to isCallArgument and formatted changes 2019-05-08 23:44:29 -04:00
Sam Lazarus
edd8f70330 Sema: Change the diagnostic to choices made by the constraint system 2019-05-08 23:37:51 -04:00
Pavel Yaskevich
860cddfd34 [ConstraintSystem] Allow arguments to be passed by value to @autoclosure parameters
Instead of always requiring a call to be made to pass argument
to `@autoclosure` parameter, it should be allowed to pass argument
by value to `@autoclosure` parameter which can return a function
type.

```swift
func foo<T>(_ fn: @autoclosure () -> T) {}
func bar(_ fn: @autoclosure @escaping () -> Int) { foo(fn) }
```
2019-05-08 19:41:35 -07:00
Sam Lazarus
ad38117427 Sema: Add a diagnostic for attemting .init call on mutable value 2019-05-08 15:42:14 -04:00
Slava Pestov
7d96de54e8 Merge pull request #24601 from slavapestov/remove-old-inout-expr-check
Remove MiscDiagnostics diagnosis of invalid InOutExprs
2019-05-08 08:11:44 -04:00
Slava Pestov
b6985acc23 Sema: More flexible InvalidUseOfAddressOf 2019-05-07 23:10:48 -04:00
Pavel Yaskevich
cb3252e055 [ConstraintSystem] Extend use of missing explicit call fix
Now covers following new areas (alongside simple assignments):

- Contextual type coercions:
  - In assignment e.g. `let _: X = foo`
  - In return type positions e.g. `func foo() -> A { return bar }`

- Argument-to-parameter applications (including @autoclosure)
2019-05-07 16:41:19 -07:00
Pavel Yaskevich
4f63c4af71 [ConstraintSystem] Detect and diagnose extraneous returns from void functions
Diagnose an attempt return something from a function which
doesn't have a return type specified e.g.

```swift
func foo() { return 42 }
```
2019-05-07 13:43:14 -07:00
Pavel Yaskevich
6cd476be47 Merge pull request #24522 from xedin/diag-extraneous-addr-of
[ConstraintSystem] Detect and fix extraneous use of `&`
2019-05-06 13:56:04 -07:00
Pavel Yaskevich
3af163a94c [ConstraintSystem] Detect and fix extraneous use of &
Diagnose extraneous use of address of (`&`) which could only be
associated with arguments to `inout` parameters e.g.

```swift
struct S {}

var a: S = ...
var b: S = ...

a = &b
```
2019-05-06 11:18:47 -07:00
Sam Lazarus
4fac0b31c8 Merge pull request #24450 from sl/sl/sr-10202
Add useful diagnostic when trying to write through key path on private(set) var
2019-05-06 13:09:22 -04:00
Sam Lazarus
501cd91d10 Sema: Add additional assertion before providing keypath application diagnostic 2019-05-06 11:57:11 -04:00
Pavel Yaskevich
43526d031c Merge pull request #24431 from xedin/diag-noescape-param-assignment
[ConstraintSystem] Improve @escaping parameter diagnostics
2019-05-04 20:09:31 -07:00
Sam Lazarus
ed3eaef179 Sema / Test: Revert the error for assignment through read-only key path
This updates the error message so that in the case where we can find a
Decl, it gives the error "cannot assign through subscript: 'name' is a
read-only key path", and in the case where there's no associated Decl, gives the
error message "cannot assign through subscript: key path is read-only".

Additionally updates tests with the new error messages and formats all changes.
2019-05-04 14:03:06 -04:00
Sam Lazarus
6a7c378cbf Sema: Move getting key-path argument logic to a closure 2019-05-03 22:16:56 -04:00
Sam Lazarus
2f94a2132e Sema: Change cast to use dyn_cast instead of dyn_cast_or_null 2019-05-03 22:16:56 -04:00
Sam Lazarus
1a1e77077a Test / Sema: Change the wording of assignment through read-only key path error 2019-05-03 22:16:56 -04:00
Sam Lazarus
1d22e987c8 Sema: Add a diagnostic for assigning using a subscript with a read-only keypath 2019-05-03 22:16:56 -04:00
Pavel Yaskevich
83d6f027f5 [AST] NFC: Add ValueDecl::getOpaqueResultTypeRepr accessor
Follow-up cleanup to simplify code related to diagnosing of
opaque return type conformance mismatches.
2019-05-03 16:49:36 -07:00
Pavel Yaskevich
d275c8afbb [ConstraintSystem] Add a tailored diagnostic for conversion of non-escaping parameter to dependent member type 2019-05-02 21:21:58 -07:00
Pavel Yaskevich
21216d8ecd [ConstraintSystem] Detect and fix invalid refs in dynamic key path member lookup
KeyPath dynamic member lookup is limited to what key path itself
could do, so let's detect and diagnose invalid references just
like we do for regular key path expressions.

Resolves: rdar://problem/50376224
2019-05-02 15:02:11 -07:00
Pavel Yaskevich
62b2da803c [ConstraintSystem] Improve @escaping parameter diagnostics
Detect difference in escapiness while matching function types
in the solver and record a fix that suggests to add @escaping
attribute where appropriate.

Also emit a tailored diagnostic when non-escaping parameter
type is used as a type of a generic parameter.
2019-05-02 00:40:30 -07:00
Pavel Yaskevich
b82a8c544d [ConstraintSystem] Diagnose missing explicit calls in assignment
If the source of the assignment is a function type which has
a result that could be converted to expected destination type,
let's diagnose that as missing call if such function doesn't
have any parameters.
2019-05-01 01:40:11 -07:00
Pavel Yaskevich
1f1e8d99d7 [Diagnostics] Improve missing conformance diagnostics for opaque return
New diagnostic framework can already identify contextual failures
related to opaque return types, `RequirementFailure` just needs
to get adjusted to identify correct affected declaration and provide
tailored diagnostic.

Resolves: rdar://problem/49582531
2019-04-29 16:17:45 -07:00
Pavel Yaskevich
94977ee175 [TypeChecker] Improve contextual mismatch diagnostics for key path
Detect situations where key path doesn't have capability required
by the context e.g. read-only vs. writable, or either root or value
types are incorrect e.g.

```swift
struct S { let foo: Int }
let _: WritableKeyPath<S, Int> = \.foo
```

Here context requires a writable key path but `foo` property is
read-only.
2019-04-26 10:59:01 -07:00
Suyash Srijan
20cfa14423 [CSDiagnostics] Removes lazy attributes when applying computed property fix-it 2019-04-25 17:59:07 +01:00
Pavel Yaskevich
6fdb5482d4 [Diagnostics] Diagnose invalid method references in key path 2019-04-22 16:40:14 -07:00
Pavel Yaskevich
d6a3a31ae9 [ConstraintSystem] Detect and fix use of method references in key path
Referencing (instance or static) methods in the key path is not
currently allowed, solver should be responsible for early detection
and diagnosis of both standalone e.g. `\.foo` and chained
e.g. `\.foo.bar` (where foo is a method) references in key path
components.

```swift
struct S {
  func foo() -> Int { return 42 }
}

let _: KeyPath<S, Int> = \.foo
```

Resolves: rdar://problem/49413561
2019-04-22 14:56:15 -07:00
Pavel Yaskevich
23403500bd Merge pull request #24097 from xedin/diag-mutating-getter-in-keypath
[ConstraintSystem] Detect and diagnoses use of members with mutating getters in key path
2019-04-18 13:12:22 -07:00
Pavel Yaskevich
eaf1dc0012 [Diagnostics] Diagnose use of member with mutating getter in key path 2019-04-17 11:51:33 -07:00
Pavel Yaskevich
c33b726486 [Diagnostics] Extract logic common to all key path invalid ref diagnostics 2019-04-17 11:34:13 -07:00
Slava Pestov
eed84abda9 Sema: Add TVO_CanBindToNoEscape
We have a systemic class of issues where noescape types end up bound to
type variables in places that should not. The existing diagnostic for
this is ad-hoc and duplicated in several places but it doesn't actually
address the root cause of the problem.

For now, I've changed all call sites of createTypeVariable() to set the
new flag. I plan on removing enough occurrences of the flag to replicate
the old diagnostics. Then we can continue to refine this over time.
2019-04-16 23:01:33 -04:00
Pavel Yaskevich
55bc16e785 [Diagnostics] Diagnose static member reference as a key path component 2019-04-15 23:42:34 -07:00
Suyash Srijan
072e84acd6 [CSSimplify] Reject key path if root type is AnyObject (#23820)
Detect situations where `AnyObject` is attempted to be used as a root type of the key path
early and diagnose via new diagnostics framework.
2019-04-14 12:18:35 -07:00
Pavel Yaskevich
439335ddb0 [Diagnostics] Extend keypath subscript index missing Hashable to support dynamic member lookup 2019-04-10 15:10:47 -07:00
Pavel Yaskevich
c3460f825d [Diagnostics] Diagnose keypath subscript components with missing Hashable conformances 2019-04-10 13:51:12 -07:00
Pavel Yaskevich
dd810a7b24 [Diagnostics] Improve mutability diagnostics related to dynamic member lookup
Since there is a way to mutate through use of writable keypath
diagnostics have to be adjusted to point to the members found
via keypath member lookup instead to using catch-all
"immutable base" diagnostic.
2019-04-01 12:41:55 -07:00
Pavel Yaskevich
f95d9b092e [TypeChecker] Add new type of overload choice to support keypath dynamic lookup 2019-04-01 12:40:39 -07:00
Pavel Yaskevich
1ee66cd653 Merge pull request #23500 from theblixguy/fix/SR-9267
[CSDiag] Improving the fix-it for defining computed variables
2019-03-25 11:02:45 -07:00