Commit Graph

968 Commits

Author SHA1 Message Date
Pavel Yaskevich
329ed30b71 [ConstraintSystem] Make sure that read-only keypath is preferred for read-only properties
This fixes an oversight related to `ReferenceWritableKeyPath` overloads
of the KeyPath dynamic member lookup. Original check filtered out only
`WritableKeyPath` if the storage is read-only, but it should check for
both `WritableKeyPath` and `ReferenceWritableKeyPath`, otherwise program
is going to crash at run time.

Resolves: rdar://problem/51456614
2019-06-05 17:09:03 -07:00
Saleem Abdulrasool
731c31f9a5 MSVC: litter the code with llvm_unreachable (NFC)
Add `llvm_unreachable` to mark covered switches which MSVC does not
analyze correctly and believes that there exists a path through the
function without a return value.
2019-06-01 19:02:46 -07:00
Pavel Yaskevich
2f3809c5b3 Merge pull request #25149 from xedin/diag-missing-generic-args
[ConstraintSystem] Detect and diagnose missing generic arguments
2019-05-31 13:04:00 -07:00
Sam Lazarus
dc886ebe47 Sema: Move call to getRValueType inside of getBaseType 2019-05-30 19:07:25 -04:00
Sam Lazarus
147280ac3b Sema: Renamed invalid member ref on existential fix, and refactored inheritance 2019-05-30 18:48:58 -04:00
Sam Lazarus
6af91cc08d Sema: Removed unnecessary ValueDecl from AllowProtocolTypeMember
Additionally removed the same unnecessary ValueDecl from
AllowProtocolTypeMemberFailure allowing it to extend MissingMemberFailure.
2019-05-30 01:03:46 -04:00
Sam Lazarus
049ec0f851 Sema: Port member access on existentials diagnostic to new diagnostic framework 2019-05-30 00:32:01 -04:00
Pavel Yaskevich
c30845fa74 [ConstraintSystem] Detect and diagnose missing generic arguments
Introduce a fix to detect and diagnose situations when omitted
generic arguments couldn't be deduced by the solver based on
the enclosing context.

Example:

```swift
struct S<T> {
}

_ = S() // There is not enough context to deduce `T`
```

Resolves: rdar://problem/51203824
2019-05-29 16:39:41 -07:00
Pavel Yaskevich
636b4ceeb2 [ConstraintSystem] Default generic parameters associated with missing member to Any
As part of the `DefineBasedOnUse` fix introduced in places where
there is a reference to non-existent member, let's also add
constraints which allow to default any generic parameters found
in base type to `Any`.
2019-05-29 14:58:48 -07:00
Pavel Yaskevich
b9d360cf73 [ConstraintSystem] NFC: Unify logic used to fix requirement failures
There was duplicate code used in `simplifyConformsToConstraint` and
`repairFailures`, both places should be using `fixRequirementFailure`
instead.
2019-05-23 10:24:04 -07:00
Pavel Yaskevich
866e8f51fa [Diagnostics] Let invalid member ref carry member declaration instead of looking it up
All places where `invalid member ref` fix/diagnostic is used already
have a reference to the potential member choice declaration, which
diagnostic could take advantage of.
2019-05-21 11:49:03 -07:00
Pavel Yaskevich
e76ae9c343 [ConstraintSystem] Use missing protocol fix for sequence element mismatches related to protocols 2019-05-17 14:32:04 -07:00
Pavel Yaskevich
17643a30e0 [ConstraintSystem] Move missing contextual protocol detection to matchExistentialTypes 2019-05-17 12:53:27 -07:00
Pavel Yaskevich
ada6ab599a [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-17 12:53:22 -07:00
Pavel Yaskevich
ace86e81cf [ConstraintSystem] Switch "missing conformance" fix/diagnostic to use protocol type
Switch type allows "missing conformance" diagnostic to be
used not only for protocols by for protocol compositions as well.
2019-05-17 12:52:15 -07:00
Pavel Yaskevich
d4e8d583ae Revert "[ConstraintSystem] Use missing conformance fix to diagnose contextual failures" 2019-05-17 12:45:55 -07:00
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
8aa7401ba5 [Sema] Add getDeclOrNull to OverloadChoice
This helps clean up a bunch of call sites.
2019-05-16 12:07:41 +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
a23bc3e189 [ConstraintSystem] Switch "missing conformance" fix/diagnostic to use protocol type
Switch type allows "missing conformance" diagnostic to be
used not only for protocols by for protocol compositions as well.
2019-05-15 14:21:37 -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
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
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
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
5e4afc2503 Sema: Remove some dead code from matchTypes() 2019-05-07 23:10:48 -04:00
Slava Pestov
fd2dd9f8a4 Sema: Fix OperatorArgumentConversion with LHS of InOutType
We don't require or allow '&' for the mutable parameters in
operator calls, since we want to write 'x += 10' and not
'&x += 10'.

The constraint sovler accepted '&x += 10' though, and we had
a separate pass in MiscDiagnostics for rejecting it.

Instead, let's just reject this in the solver.

The main difficulty is that we must now be prepared to fail
certain OperatorArgumentConversion and ApplicableFunction
constraints even when both the LHS and RHS types are equal.
2019-05-07 23:10:48 -04:00
Slava Pestov
1dedba1f12 Sema: Pass argument match kind down to matchCallArguments() 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
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
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
Pavel Yaskevich
0275689cd3 [ConstraintSystem] Tighten "missing call" fix conditions by checking whether other side is an optional
If "convertTo" type is an optional let's look through it to see
whether it contains another function type which, if so, would
rule out possibility of missing explicit call.

Resolves: rdar://problem/50438071
2019-05-03 13:02:28 -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
e0f81a8efc Merge pull request #24407 from xedin/diag-explicit-calls-via-fixes
[ConstraintSystem] Diagnose missing explicit calls in assignment
2019-05-01 10:44:01 -07:00
Doug Gregor
4e86c8ff52 Merge pull request #24403 from DougGregor/constraint-solver-trailing-closure-pruning
[Constraint solver] Reject trailing closures matching non-closure-parameters
2019-05-01 06:45:24 -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
Doug Gregor
a1af0e45cb [Constraint solver] Reject trailing closures matching non-closure-parameters.
Enhance call-argument matching to reject trailing closures that match up
with parameters that cannot accept closures at all.

Fixes rdar://problem/50362170.
2019-04-30 23:12:32 -07:00
Pavel Yaskevich
0ad0a844f3 [ConstraintSystem] NFC: Move repairFailure to be a member of ConstraintSystem
That makes it much easier to use various facilities provided by
`ConstraintSystem`.
2019-04-30 16:40:06 -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
Nate Chandler
b2ad56223f Eliminated conversion from uninhabited. 2019-04-24 10:09:17 -07:00
Pavel Yaskevich
ca6cf0c022 [ConstraintSystem] Use special locator for expr representing return of single expr function 2019-04-24 10:06:44 -07:00
Nate Chandler
0143ce25c2 Implicitly convert single exprs from uninhabited.
When type-checking a return statement's result, pass a new
ContextualTypePurpose when that return statement appears in a function
with a single expression.  When solving the corresponding constraint
system, the conversion constraint will have a new ConstraintKind.  When
matching types, check whether the constraint kind is this new kind,
meaning that the constraint is between a function's single expression
and the function's result.  If it is, allow a conversion from
an uninhabited type (the expression's type) to anything (the function's
result type) by adding an uninhabited upcast restriction to the vector
of conversions.  Finally, at coercion time, upon encountering this
restriction, call coerceUninhabited, replacing the original expression
with an UninhabitedUpcastExpr.  Finally, at SILGen time, treat this
UninhabitedUpcastExpr as a ForcedCheckedCastExpr.

Eliminates the bare ConstraintSystem usage from
typeCheckFunctionBodyUntil, ensuring that the same code path is followed
for all function bodies.
2019-04-24 10:04:17 -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
Joe Groff
a8c2b50bd8 Merge pull request #22072 from jckarter/opaque-type-runtime
Opaque types with resilience
2019-04-18 14:52:31 -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
Argyrios Kyrtzidis
86e4467c52 Merge pull request #24072 from benlangmuir/kpdml-ide
Code-completion, indexing, cursor-info, etc. for KeyPath dynamic member lookup
2019-04-17 16:12:58 -07:00
Joe Groff
399332b75b Parsable interface and type reconstruction support for opaque types.
When printing a swiftinterface, represent opaque result types using an attribute that refers to
the mangled name of the defining decl for the opaque type. To turn this back into a reference
to the right decl's implicit OpaqueTypeDecl, use type reconstruction. Since type reconstruction
doesn't normally concern itself with non-type decls, set up a lookup table in SourceFiles and
ModuleFiles to let us handle the mapping from mangled name to opaque type decl in type
reconstruction.

(Since we're invoking type reconstruction during type checking, when the module hasn't yet been
fully validated, we need to plumb a LazyResolver into the ASTBuilder in an unsightly way. Maybe
there's a better way to do this... Longer term, at least, this surface design gives space for
doing things more the right way--a more request-ified decl validator ought to be able to naturally
lazily service this request without the LazyResolver reference, and if type reconstruction in
the future learns how to reconstruct non-type decls, then the lookup tables can go away.)
2019-04-17 14:46:22 -07:00