Commit Graph

148 Commits

Author SHA1 Message Date
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
2e93165040 [ConstraintSystem] Detect use of declaration with mutating getter as a key path component
Such use is currently not allowed so variables or subscripts with
mutating getters should be rejected and diagnosed.
2019-04-16 17:56:13 -07:00
Pavel Yaskevich
306507931a [CSFix] Introduce a fix for different kinds of ref failures in key path 2019-04-16 17:31:03 -07:00
Pavel Yaskevich
03ea5dce5a [ConstraintSystem] Detect and fix use of static members in a key path in the solver
Previously it was possible to create an invalid solution where
static members would be referenced in a key path, which is not
currently supported and would only be diagnosed while applying
such solution to AST e.g.

```swift
struct S {
  static var foo: Int = 42
}

_ = \S.Type.foo
```
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
c982706f0d [CSFix] Fix keypath subscript index to conform to Hashable 2019-04-10 13:51:04 -07:00
Pavel Yaskevich
196f732cc8 [CSFix] Introduce a fix for inaccessible members
If there are no other choices, let's attempt to
use any available inaccessible candidates.
2019-03-18 13:44:30 -07:00
Pavel Yaskevich
1d2c3633fb [ConstraintSystem] Fix out-of-order arguments
Detect and fix out-of-order arguments by moving them into correct
positions.
2019-03-04 20:03:48 -08:00
Pavel Yaskevich
8f85b848cc [ConstraintSystem] Fix closure parameter destructuring
Detect and fix closure parameter destructuring where
it's not currently allowed e.g. free standing closures
with contextual type `let _: ((Int, Int)) -> Void = { $0 + $1 }`
2019-03-04 11:02:15 -08:00
Pavel Yaskevich
0b12c664b0 [ConstraintSystem] Fix missing arguments
While trying to match function types, detect and fix any missing
arguments (by introducing type variables), such arguments would
get type information from corresponding parameters and aid in
producing solutions which are much easier to diagnose.
2019-02-25 17:07:26 -08:00
Suyash Srijan
34f8670d2a [CS] Use fixes to diagnose instance member on type (or vice versa) access (#21830)
This PR migrates instance member on type and type member on instance diagnostics handling to use the new diagnostics framework (fixes) and create more reliable and accurate diagnostics in such scenarios.
2019-02-22 16:57:26 -08:00
Pavel Yaskevich
4a2e889853 [CSFix] NFC: Fixes should only be created via create(...)
This is a small access control cleanup where some of the
fixes had public constructors, which is not a preferred
way to create fixes, `create(...)` should be used instead.
2019-02-12 12:16:30 -08:00
Pavel Yaskevich
1d42e16ad2 [ConstraintSystem] Detect invalid implicit ref to initializer on non-const metatype
Situations like:

```swift
struct S {}
func foo(_ s: S.Type) {
  _ = s()
}
```

Used to be diagnosed in solution application phase, which means that
solver was allowed to formed an incorrect solution.
2019-02-07 00:17:07 -08:00
Pavel Yaskevich
1d8cee9cb4 [ConstraintSystem] Detect invalid initializer references early
Currently invalid initializer references are detected and
diagnosed in solution application phase, but that's too
late because solver wouldn't have required information while
attempting to determine the best solution, which might result
in viable solutions being ignored in favour of incorrect ones e.g.

```swift
protocol P {
  init(value: Int)
}

class C {
  init(value: Int, _: String = "") {}
}

func make<T: P & C>(type: T.Type) -> T {
  return T.init(value: 0)
}
```

In this example `init` on `C` would be preferred since it
comes from the concrete type, but reference itself is invalid
because it's an attempt to construct class object using
metatype value via non-required initalizer.

Situations like these should be recognized early and invalid
use like in case of `C.init` should be ranked lower or diagnosed
if that is the only possible solution.

Resolves: rdar://problem/47787705
2019-02-05 10:25:36 -08:00
Pavel Yaskevich
462cb1ffb1 Merge pull request #22124 from xedin/diagnose-partial-applies
[CSDiagnostics] Diagnose invalid partial application
2019-01-28 10:44:56 -08:00
Pavel Yaskevich
1c79380a12 [CSFix] Couple of small cleanups related to ForceOptional
* Make sure that base and unwrapped types aren't null
* Don't allocate `ForceOptional` fix if nothing has been unwrapped
  in `simplifyApplicableFnConstraint`
* Add sugar reconstitution support to `FailureDiagnostic::resolveType`
* Format diagnostics a bit better
2019-01-26 00:48:01 -08:00
Pavel Yaskevich
7d830cee8b [ConstraintSystem] Add a fix to detect invalid partial application
Currently supported only partial applications of instance methods
marked as `mutating`.
2019-01-25 13:29:11 -08:00
Suyash Srijan
76b8209b25 [cs] store base type and unwrapped type in the fix 2019-01-25 10:13:37 +00:00
Pavel Yaskevich
aa8ccfd57d [CSFix] Introduce non-fatal "warning" fixes
The intention here is to be able to detect warnings earlier and
move some of the logic from CSApply and MiscDiagnostics to solver.

Warning fixes still lead to solution being applied to AST.
2019-01-18 14:08:08 -08:00
Pavel Yaskevich
60fb2bf94e [CSFix] Try to fix missing member by defining it based on use
If lookup couldn't find anything matching given name, let's try to
fake its presence based on how member is being used. This is going
to help (potentially) type-check whole expression and diagnose the
problem precisely.
2019-01-09 14:35:27 -08:00
Pavel Yaskevich
f032b9c57c [CSFix] Attempt to replace .subscript(...) with subscript operator
If there are no members named "subscript" available, let's try to
replace it with subscript operator with might be what was intended.
2019-01-08 12:06:40 -08:00
Pavel Yaskevich
f086d41094 [CSFix] Call a function/member which is incorrectly used as a property
If the base type for the member lookup is a function, let's try to
use its result type as a base instead, maybe there is just a call
missing.
2018-12-11 16:58:43 -08:00
Pavel Yaskevich
e043e2b2b3 [CSFix] Add a fix to remove invalid optional unwrap
If the base type is not optional, trying to unwrap it is
incorrect. Introduce a fix to make it look like base was
an optional type which leads solver to move forward
towards possible solution.
2018-12-04 18:40:04 -08:00
Pavel Yaskevich
f9dae942f8 [CSFix] Add fix to track invalid @autoclosure forwarding 2018-11-21 12:17:25 -08:00
Pavel Yaskevich
66a79301b4 [CSDiagnostics] Diagnose contextual closure result mismatches via fixes
Let's keep track of type mismatch between type deduced
for the body of the closure vs. what is requested
contextually, it makes it much easier to diagnose
problems like:

```swift
func foo(_: () -> Int) {}
foo { "hello" }
```

Because we can pin-point problematic area of the source
when the rest of the system is consistent.

Resolves: rdar://problem/40537960
2018-11-07 14:28:50 -08:00
gregomni
939de4fb4a Extend candidate missing conformance checking to other types of requirements so that we check superclass and same type requirements in the same way. 2018-10-19 10:02:30 -07:00
gregomni
f2a80c4a9d Let CS::solveSingle() optionally allow fixes in the solution. Use this in protocol inference to give better candidate failure notes. 2018-10-16 15:27:09 -07:00
Pavel Yaskevich
81afed9700 [Diagnostics] Add asNote flag to ConstraintFix 2018-08-24 11:20:49 -07:00
Pavel Yaskevich
fa45b3b675 [Diagnostics] NFC: ConstraintFix::{print, dump} no longer need SourceManager passed-in
Since `ConstraintFix` references `ConstraintSystem` directly now,
we can get `SourceManager` from `ASTContext` associated with that
`ConstraintSystem` instead of passing it in every time.
2018-08-22 00:15:24 -07:00
Pavel Yaskevich
3eeab38606 [Diagnostics] NFC: Attach ConstraintSystem to ConstraintFix directly
There is no longer any reason to attach solution to `ConstraintFix`
because solution applied to the constraint system before any of the
related fixes are diagnosed, so instead let's attach `ConstraintSystem`
to `ConstraintFix` directly, because it would have all of the required
information.
2018-08-22 00:07:34 -07:00
Pavel Yaskevich
16dfa6be72 [Diagnostics] Add superclass requirement fix/diagnostic
Extend new requirement failure diagnostics by adding "superclass"
generic requirement failures.
2018-08-21 00:39:21 -07:00
Pavel Yaskevich
3cc613497c [ConstraintSystem] Add same-type requirement fix/diagnostic
Extend new requirement failure diagnostics by adding "same-type"
generic requirement failures.
2018-08-18 13:05:32 -07:00
gregomni
eaf8c6d232 Conflict resolution and conversion to ConstraintFix class 2018-08-16 18:21:43 -07:00
Pavel Yaskevich
dd9c28b456 [ConstraintSystem] Add proper printing (name + locator) for fixes 2018-08-13 18:10:50 -07:00
Pavel Yaskevich
1983431591 [ConstraintSystem] Make it so re-labeling fix holds correct labels as trailing objects 2018-08-13 18:09:01 -07:00
Pavel Yaskevich
8f9631bf6c [ConstraintSystem] Add unwrap optional base with optional result fix kind 2018-08-13 01:23:28 -07:00
Pavel Yaskevich
c3ac038a66 [ConstraintSystem] Add anchor expression accessor to ConstraintFix 2018-08-13 01:23:28 -07:00
Pavel Yaskevich
c005e0084d [ConstraintSystem] Allocate constraint fixes using constraint system
Since constraint fix life span is tightly coupled with particular
constraint system, it makes sense to allocate fixes using the same
allocator as used for constraints.
2018-08-13 01:23:28 -07:00
Pavel Yaskevich
d00d73aff4 [ConstraintSystem] Move FixKind to new abstraction
Make is so `FixKind` lives in the new `CSFix` header and
all new `ConstraintFix` classes get a kind. That's useful
when trying to identify if locator has multiple fixes of
the same kind attached to it.
2018-08-13 01:23:28 -07:00
Pavel Yaskevich
df9614177a [ConstraintSystem] Add 'missing conformance' fix 2018-08-13 01:23:28 -07:00
Pavel Yaskevich
8221354ce5 [ConstraintSystem] Add 're-label arguments' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
a1651f9b3c [ConstraintSystem] Add 'mark explicitly @escaping' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
2ced603bc3 [ConstraintSystem] Add 'replace as with as!' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
48c4eb4539 [ConstraintSystem] Add 'add address-of' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
a17c1cb409 [ConstraintSystem] Add 'unwrap optional base' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
6fc5ce36fd [ConstraintSystem] Add 'force optional' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
428e9b5926 [ConstraintSystem] Add 'force downcast' fix 2018-08-13 01:23:27 -07:00
Pavel Yaskevich
ac0509729a [ConstraintSystem] Introduce ConstraintFix abstraction
A fix is related to one of the constraints through its locator,
and contains information required to "fix" a failure associated with
given constraint, each of the fixes also includes diagnostic.
2018-08-13 01:23:27 -07:00