Commit Graph

1287 Commits

Author SHA1 Message Date
Holly Borla
51c7c8c8f1 [ConstraintSystem] Port function parameter type mismatch diagnostics. 2019-12-11 10:45:52 -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
Holly Borla
7f2d4c0a99 [CSApply] When applying constraint fixes for a solution, only coalesce
fixes of the same kind.
2019-11-11 10:08:25 -08:00
Robert Widmann
7bad9aacc3 Drop the TypeChecker out of ConstraintSystem 2019-11-10 13:26:47 -08:00
Robert Widmann
1123b1f897 Move constraint satisfiability utilities 2019-11-10 13:12:50 -08:00
Pavel Yaskevich
ef98b39de9 Merge pull request #28141 from xedin/sr-8411
[TypeChecker] Disambiguite cases of implicit pointer conversions with…
2019-11-08 10:18:46 -08:00
Hamish Knight
f519505c8b Remove some more TypeChecker uses (#28132)
Remove some more TypeChecker uses
2019-11-07 17:56:45 -08:00
Pavel Yaskevich
3feb25c958 [TypeChecker] Disambiguite cases of implicit pointer conversions with optionals
Currently `{inout, array, string}-to-pointer` conversion doesn't
track whether there was a difference in optionality between involved
types which leads to ambiguity when different overload choices
have different optionality requirements.

Let's fix that by increasing a score in cases if pointer type
is itself optional e.g.:

```swift
func foo(_ x: UnsafeMutablePointer<Int>) {}
func foo(_ x: UnsafeMutablePointer<Int>?) {}

foo(&foo) // Should pick the least optional overload choice.
```

Resolves: [SR-8411](https://bugs.swift.org/browse/SR-8411)
2019-11-07 17:00:59 -08:00
Pavel Yaskevich
3d10f33e87 Merge pull request #28131 from xedin/remove-argument-matcher
[CSDiag] Remove obsolete `ArgumentMatcher` from `FailureDiagnosis`
2019-11-07 15:31:40 -08:00
Robert Widmann
41ab235797 [CS] Remove some TypeChecker uses 2019-11-07 12:41:37 -08:00
Pavel Yaskevich
c3acce4d69 [CSDiag] Remove obsolete ArgumentMatcher from FailureDiagnosis
All of the argument diagnostics have been ported to the new diagnostic
framework, so now is the time to remove `ArgumentMatcher` and the only
place where it was used - `diagnoseSingleCandidateFailures`.
2019-11-07 11:17:01 -08:00
Hamish Knight
18088b04f0 [AST] Consolidate Obj-C types on ASTContext
This commit moves the getNSObjectType and
getObjCSelectorType methods from TypeChecker
onto ASTContext. In addition, it moves the
FOR_KNOWN_FOUNDATION_TYPES macro into a separate
file to define each of the Obj-C type decls
we want to have access to.
2019-11-07 08:26:08 -08:00