Commit Graph

1297 Commits

Author SHA1 Message Date
Hamish Knight
cefc03f0e2 [CS] Resolve callees for key path dynamic members
Change the locator for the applicable fn
constraint for the inner subscript reference in an
implicit `outer[dynamicMember: \Inner.[0]]` expr
such that it uses the member locator with a
KeyPathDynamicMember element.

Then add a case to `getCalleeLocator` to handle
these locators. We also need to handle this
specially in `getArgumentInfoLocator` due to the
fact that the argument labels for the inner
subscript reference correspond to those written
by the user for the outer subscript reference.

Resolves SR-11933.
Resolves rdar://problem/57824025.
2019-12-16 10:04:24 -08:00
Hamish Knight
f1e690c07e Merge pull request #28775 from hamishknight/dynamic-lookup-attr-req
Requestify hasDynamicMemberLookupAttribute for Sema too
2019-12-13 14:40:13 -08:00
Pavel Yaskevich
5ba2e59c63 Merge pull request #28752 from xedin/simplify-potential-binding
[ConstraintSystem] Record originator constraint for each type variable binding
2019-12-13 11:50:02 -08:00
Hamish Knight
36cecb4015 Requestify hasDynamicMemberLookupAttribute for Sema too
Previously we had a request for this in
IDETypeChecking, but this wasn't used for queries
made from Sema. Move the request into Sema, and
move `hasDynamicMemberLookupAttribute` onto
TypeBase.
2019-12-13 11:04:00 -08:00
Pavel Yaskevich
fb764969c0 Merge pull request #28770 from xedin/rdar-57843297
[Diagnostics] Upon keypath result contextual mismatch try to match ob…
2019-12-13 10:12:27 -08:00
Pavel Yaskevich
47c7b9b910 [Diagnostics] Upon keypath result contextual mismatch try to match object types
If there was a mismatch between last component type and contextual
key path result type, let's try to re-match the types with all optionals
stripped off. In cases where the problem is optionality difference
e.g. `[Int] vs. [Int]?` that propagates contextual information to
the last component which facilitates better diagnostics.

Resolves: rdar://problem/57843297
2019-12-13 00:50:54 -08:00
Pavel Yaskevich
f259286bd7 [CSFix] Convert missing explicit @escaping fix to be a contextual mismatch
Sometimes diagnostic needs both sides of the conversion e.g.
when there is an attempt to bind generic argument to non-escaping type.
2019-12-12 12:42:08 -08:00
Holly Borla
3d1ab4da67 Merge pull request #28712 from hborla/function-parameter-mismatch-diagnostics
[ConstraintSystem] Port function parameter type mismatch diagnostics.
2019-12-11 14:33:07 -08:00
Holly Borla
51c7c8c8f1 [ConstraintSystem] Port function parameter type mismatch diagnostics. 2019-12-11 10:45:52 -08:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Holly Borla
edb4e70d66 Merge pull request #28600 from hborla/tuple-mismatch-diagnostics
[ConstraintSystem] Port tuple type mismatch diagnostics
2019-12-09 13:26:34 -08:00
Holly Borla
50abedd45c [Diagnostics] Keep track of the indices of the tuple elements whose
types do not match in AllowTupleTypeMismatch/TupleContextualFailure.
2019-12-09 11:04:34 -08:00
Pavel Yaskevich
3d7306a066 Merge pull request #28630 from xedin/dynamic-member-diagnostics
[Diagnostics] Port/Improve diagnostics for `@dynamicCallable` and `callAsFunction`
2019-12-09 10:24:27 -08:00
Pavel Yaskevich
61b7b3b28d Merge pull request #28626 from xedin/rdar-57668763
[ConstraintSystem] Fix a case of too eager force optional unwrap with…
2019-12-06 17:10:56 -08:00
Pavel Yaskevich
21a8b3c57d [Diagnostics] Port tailored diagnostics for missing dynamicallyCall methods 2019-12-06 16:43:18 -08:00
Pavel Yaskevich
328143ad4d [ConstraintSystem] Align @dynamicCallable argument locators with regular calls
Having the same locators for `@dynamicCallable` and the regular calls
make it much easier to produce diagnostics.
2019-12-06 16:43:18 -08:00
Pavel Yaskevich
08baf67677 [ConstraintSystem] Fix a case of too eager force optional unwrap with optional contextual type
If return of optional chaining is assigned to an optional type
or discarded there should be no force optional unwrap attempt
on it because type variable associated with context can be
bound to optional of any depth.

Resolves: rdar://problem/57668873
2019-12-06 15:03:19 -08:00
Holly Borla
f9a1ab28f4 [ConstraintSystem] Port tuple type mismatches to the new framework 2019-12-06 13:12:57 -08:00
Hamish Knight
a97328dcbf [CS] Use a MapVector to cache resolved overloads
Rather than maintaining a linked list of overload
choices, which must be linearly searched each time
we need to lookup an overload at a given callee
locator, use a MapVector which can be rolled back
at the end of a scope.

Remove ResolvedOverloadSetListItem in favor of
using SelectedOverload, which avoids the need to
convert between them when moving from
ConstraintSystem to Solution.
2019-12-05 14:47:52 -08:00
Pavel Yaskevich
cafa338ae3 Merge pull request #28594 from xedin/rdar-50420029
[ConstraintSystem] Guard against infinite recursion in key path dynam…
2019-12-05 14:34:10 -08:00
Pavel Yaskevich
1d393ebdd4 [ConstraintSystem] Guard against infinite recursion in key path dynamic member lookup
It's possible to construct subscript member responsible for key path
dynamic member lookup in a way which is going to be self-recursive
and an attempt to lookup any non-existent member is going to trigger
infine recursion.

Let's guard against that by making sure that the base type of the
member lookup is different from root type of the key path.

Resolves: rdar://problem/50420029
Resolves: rdar://problem/57410798
2019-12-05 12:42:17 -08:00
Pavel Yaskevich
d8997c6223 Merge pull request #28573 from xedin/remove-closure-handling-from-csdiag
[Diagnostics] Port last remaining closure expression diagnostics
2019-12-05 11:37:22 -08:00
Pavel Yaskevich
bbe0c098ba [ConstraintSystem] Avoid member lookup if base type is a "hole"
Member lookup on a "hole" is not going to produce any results
and it makes sense to detect early and short-circuit the constraint.
2019-12-04 14:23:12 -08:00
Hamish Knight
66afe772d5 [CS] Fix resolution of key path component callees (#28569)
[CS] Fix resolution of key path component callees
2019-12-04 13:43:07 -08:00
Hamish Knight
7e1a963cce [CS] Fix resolution of key path component callees
Previously we were just searching for a resolved
overload with the same component index and anchor
for the key path. However unfortunately that found
dynamic member lookup overloads in certain cases,
leading to an incorrect mutability capability.

Change the logic such that it uses the callee
locator for a given key path component, ensuring
we only match against the "direct" callee, and
not any dynamic members it might be also referring
to.

Resolves SR-11896.
2019-12-04 11:47:35 -08:00
Pavel Yaskevich
5cacd1bb36 [ConstraintSystem] Fix situations when contextual base type can't be inferred
It might be either impossible to infer the base because there is
no contextual information e.g. `_ = .foo` or there is something
else wrong in the expression which disconnects member reference
from its context.
2019-12-03 12:07:16 -08:00
Pavel Yaskevich
8b1debf907 [ConstraintSystem] Detect contextual type mismatches associated with optional evaluation 2019-12-03 12:07:16 -08:00
Pavel Yaskevich
918ff24767 [ConstraintSystem] Repair type mismatches between function types
If it couldn't be determined precisely what is the structural
difference between two function types, let's still repair the
mismatch by ignoring contextual type.
2019-12-03 12:07:16 -08:00
Pavel Yaskevich
15c58d19d5 [ConstraintSystem] Detect passing array to variadic argument in function conversions
Initially this problem was only detected and diagnosed for calls.
So let's extend it to function conversions as well e.g.:

```swift
func foo<T>(_: [T]) {}
func bar<T>(_ f: (T...) -> ()) {}

bar { foo($0) }
```
2019-12-03 12:07:15 -08:00
Pavel Yaskevich
a9f11445c0 [Diagnostics] Diagnose an attempt to assign a value to an overloaded name
Example:

```swift
struct X {
}

func X(_: Int) -> Int {
  return 42
}

X = 42
```
2019-12-03 12:07:15 -08:00
Pavel Yaskevich
adf83fe9d5 [ConstraintSystem] Fix single tuple vs. N arguments in function conversions 2019-12-03 12:07:15 -08:00
Hamish Knight
bf0e9c727d [CS] Resolve callees for implicit callAsFunction
Add a case to getCalleeLocator to return the
appropriate locator for a call to an implicit
callAsFunction member reference.

Resolves SR-11386 & SR-11778.
2019-12-02 15:29:21 -08:00
Hamish Knight
778e130f31 [Sema] Add TypeBase::isCallableNominalType
This checks whether a type supports being called
via callAsFunction. A request is used in order to
cache the result of this query.
2019-12-02 15:29:21 -08:00
Hamish Knight
a014f15e75 [CS] Enforce that withPathElement is called on an lvalue
Locator builders keep a pointer to their underlying
locator, so it's not generally sound to extend an
rvalue locator builder.

This commit enforces that withPathElement is called
on an lvalue, and adds a couple more overloads of
getConstraintLocator to make it more convenient to
extend locators with multiple elements.
2019-11-27 17:13:50 -08:00
Pavel Yaskevich
addc2b06ab [ConstraintSystem] Introduce notion of constraint system phase
Some constraint transformations require knowledge about what state
constraint system is currently in e.g. `constraint generation`,
`solving` or `diagnostics` to make a decision whether simplication
is possible. Notable example is `keypath dynamic member lookup`
which requires a presence of `applicable fn` constraint to retrieve
some contextual information.

Currently presence or absence of solver state is used to determine
whether constraint system is in `constraint generation` or `solving`
phase, but it's incorrect in case of `diagnoseFailureForExpr` which
tries to simplify leftover "active" constraints before it can attempt
type-check based diagnostics.

To make this more robust let's introduce (maybe temporarily until
type-check based diagnostics are completely obsoleted) a proper
notion of "phase" to constraint system so it is always clear what
transitions are allowed and what state constraint system is
currently in.

Resolves: rdar://problem/57201781
2019-11-20 18:34:51 -08:00
Slava Pestov
2e65a14ed4 Merge pull request #28372 from slavapestov/typecheck-decl-primary
Kill ClosuresWithUncomputedCaptures and split off TypeCheckDeclPrimary.cpp from TypeCheckDecl.cpp
2019-11-20 14:29:31 -05:00
Pavel Yaskevich
595f85fcb3 [Diagnostics] Properly diagnose missing optional unwrap(s) related to closure result 2019-11-19 15:15:30 -08:00
Pavel Yaskevich
e0e13aa97a [ConstraintSystem] Make ForceOptional fix a part of repairFailures
Instead of always attempting an optional unwrap fix if types differ
in optionality, let's make it more targeted for each applicable
locator and conversion.

In addition adjust a couple of uses of `ForceOptional` fix to avoid
recording the same fix multiple times and use appropriate `impact`
instead.
2019-11-19 15:15:29 -08:00
Slava Pestov
ef05accd31 Sema: Remove even more vestigial TypeChecker usages 2019-11-19 17:40:00 -05:00
Holly Borla
5bb50068ac Merge pull request #28243 from hborla/missing-generic-args-refactoring
[ConstraintSystem] Missing generic args/hole refactoring
2019-11-14 13:48:40 -08:00
Pavel Yaskevich
edaf7b0c7a Merge pull request #28240 from xedin/port-clj-explicit-result-mismatch
[Diagnostics] Port explicit closure result contextual mismatch
2019-11-14 00:40:28 -08:00
Holly Borla
c0312b9c0e [ConstraintSystem] Record holes in the constraint system using a new flag in
`TypeVariableOptions` rather than using a separate data structure in the
constraint system.
2019-11-13 15:46:36 -08:00
Holly Borla
ea1a46c84d [ConstraintFix] Rather than creating a coalesced fix right before
diagnosing failures in applySolutionFixes, coalesce fixes and
diagnose failures in one method on ConstraintFix.

This eliminates the need for the `DefaultGenericArgument` fix (which
was renamed from `ExplicitlySpecifyGenericArguments`) to have an
array of missing parameters, which was only used when the fixes were
coalesced. Instead, the coalesced arguments are used to create the
`MissingGenericArgumentsFailure` diagnostic directly.
2019-11-13 14:15:40 -08:00
Pavel Yaskevich
431ca98246 [Diagnostics] Port explicit closure result contextual mismatch
Detect and diagnose a problem when explicitly specified closure
result type doesn't match what is expected by the context:

Example:

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

foo { () -> String in "" } // `Int` vs. `String`
```
2019-11-13 14:05:59 -08:00
Robert Widmann
f4d333d066 Sink a bunch of semantic options into TypeCheckerOptions
Sink
- DebugConstraintSolver
- DebugConstraintSolverAttempt
- DebugConstraintSolverOnLines
- DebugGenericSignatures
- DebugForbidTypecheckPrefix
- SolverMemoryThreshold
- SolverBindingThreshold
- SolverShrinkUnsolvedThreshold
- SolverDisableShrink
- EnableOperatorDesignatedTypes
- DisableConstraintSolverPerformanceHacks
- SolverEnableOperatorDesignatedTypes
2019-11-12 22:39:49 -08:00
Hamish Knight
2a75179849 Remove a couple of unnecessary hasInterfaceType() checks
We call isInvalid() a few lines above, so the
interface type gets computed anyway.
2019-11-12 07:50:30 -08:00
Pavel Yaskevich
a7bc52fd9a Merge pull request #28197 from xedin/handle-single-pd-arg-mismatch
[Diagnostics] Produce a tailored diagnostic for property wrapper argu…
2019-11-11 15:13:56 -08:00
Pavel Yaskevich
08f8f4191d [Diagnostics] Produce a tailored diagnostic for property wrapper argument mismatch
Diagnose an attempt to initialize a property, which has a property
wrapper, with a value of an incorrect type.
2019-11-11 12:59:28 -08:00
Holly Borla
212f20245a [ConstraintSystem] Add TypeBase::hasHole to be used by the constraint
system. Eventually, this will replace TypeBase::hasUnresolvedType.
2019-11-11 10:08:25 -08:00