Commit Graph

1266 Commits

Author SHA1 Message Date
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
Holly Borla
b560261838 [ConstraintSystem] Allow binding metatypes to succeed if one contains
a hole.
2019-11-06 14:02:36 -08:00
Pavel Yaskevich
97773baf71 [ConstraintSystem] Allow function mismatch in assignment to be diagnosed 2019-11-05 12:45:31 -08:00
Pavel Yaskevich
4eaf159eab [ConstraintSystem] Impact of r-value as l-value fix is higher for functions/subscripts
If the fix is applied to result type of a function or subscript
invocation increase its impact because such uses are invalid
e.g. assigning a value to a function call `foo() = 42`, or
using read-only subscript as mutating.
2019-11-05 12:43:32 -08:00
Pavel Yaskevich
c800bde8e1 [Diagnostics] Try to find overlap between src/dst protocols in assignment mismatch 2019-11-05 12:43:32 -08:00
Pavel Yaskevich
6670d628ca [Diagnostics] Diagnose an attempt to assign to immutable IUO declaration 2019-11-05 12:43:32 -08:00
Pavel Yaskevich
11644b3f31 [Diagnostics] Diagnose assignment type mismatches related to unresolved members 2019-11-05 12:43:32 -08:00
Pavel Yaskevich
6f919786b8 [ConstraintSystem] Detect and diagnose assignment type mismatches related to existentials 2019-11-05 12:38:13 -08:00
Pavel Yaskevich
3586af6e8a [Diagnostics] Properly diagnose assignment type mismatches when either side is optional 2019-11-05 12:38:13 -08:00
Pavel Yaskevich
a587278162 [ConstraintSystem] Detect and diagnose assignment failures related to opaque result types
An attempt to assign result type of one declaration to opaque result
of another is not supported and should be diagnosed as assignment failure.
2019-11-05 12:38:13 -08:00
Pavel Yaskevich
d23adafd3c [ConstraintSystem] Handle assignment to AnyObject as a contextual type mismatch
Contextual diagnostics already have a special case for `AnyObject`
conversions which is going to be diagnosed as a missing conformance.
2019-11-05 12:38:13 -08:00
Pavel Yaskevich
723e438c78 [ConstraintSystem] Record contextual mismatch if type is missing conformance in assignment
```swift
struct S {}

protocol P {}

func foo(_ value: S) {
  var p: P? = nil
  p = value
}
```

Assignment destination is a protocol `P` and source is a type `S`
which doesn't conform to `P`. Allow solver to detect this situation
and record `ignore assignment destination type` fix which would
cover a single missing conformance as well as protocol composition.
2019-11-05 12:38:13 -08:00
Pavel Yaskevich
cb3a0fbcc8 [ConstraintSystem] Extend use of the treat r-value as l-value fix to more cases
Cover not only immutability but also type mismatch cases and clarify
behavior when one of the sides of the type conversion is optional.
2019-11-05 12:38:13 -08:00
Holly Borla
cc65d2112d [ConstraintSystem] Fix a few places where we should consider
`matchTypes` a success without applying new constraint fixes
due to the presence of holes.
2019-11-05 09:16:57 -08:00
Holly Borla
4fd1377c81 [ConstraintSystem] With the new approach for holes, hole propagation happens
automatically.

This commit also renames `ConstraintSystem::recordHole/isHole` to
`recordPotentialHole` and `isPotentialHole` to make it clear that
we don't know for sure whether a type variable is a hole until it's
bound to unresolved.
2019-11-05 09:15:13 -08:00
Holly Borla
e840d52258 [Diagnostics] Allow fixes with the same locator to be coalesced before
application rather than throwing away subsequent fixes.

The "primary fix" decides whether or not to coalesce fixes that have
the same locator.
2019-11-05 09:15:13 -08:00
Holly Borla
561e527848 [ConstraintSystem] Extend the ExplicitlySpecifyGenericArguments fix to cover
all cases of missing generic parameters.

In `ComponentStep::take` when there are no bindings or disjunctions, use hole
propagation to default remaining free type variables that aren't for generic
parameters and continue solving. Rather than using a defaultable constraint for
holes, assign a fixed type directly when we have no bindings to try.
2019-11-05 09:15:13 -08:00
Suyash Srijan
5ff654d055 Merge pull request #27995 from theblixguy/fix/SR-11684
[PropertyWrappers] Mark the property as invalid when there is a mismatch with wrappedValue type
2019-11-05 01:25:51 +00:00
Suyash Srijan
573aceeba3 [ConstraintSystem] Don't create a AllowArgumentMismatch fix when there is an argument type mismatch in synthesized wrappedValue init
This is because we already emit a diagostic to tell the user that the property's type does not match the wrappedValue type, so this diagnostic can be a bit confusing especially because the initializer is synthesized
2019-11-04 22:29:13 +00:00
Hamish Knight
e7fe2a7a48 Prevent non-ephemeral fix from affecting overload resolution
This commit changes the behaviour of the error for
passing a temporary pointer conversion to an
@_nonEphemeral parameter such that it doesn't
affect overload resolution. This is done by recording
the fix with an impact of zero, meaning that we don't
touch the solution's score.

In addition, this change means we no longer need
to perform the ranking hack where we favour
array-to-pointer, as the disjunction short-circuiting
will continue to happen even with the fix recorded.
2019-11-03 08:42:26 -08:00
Hamish Knight
7077a68b83 [Sema] Diagnose unsound pointer conversions
Diagnose ephemeral conversions that are passed to @_nonEphemeral
parameters. Currently, this defaults to a warning with a frontend flag
to upgrade to an error. Hopefully this will become an error by default
in a future language version.
2019-11-03 08:42:25 -08:00
Pavel Yaskevich
88c477b08d [ConstraintSystem] Accept trailing closure if the last parameter is defaulted
If the last parameter is defaulted, there might be
an attempt to use a trailing closure with previous
parameter that accepts a function type e.g.

```swift
func foo(_: () -> Int, _ x: Int = 0) {}
foo { 42 }
```

Resolves: rdar://problem/55102498
2019-11-01 14:00:11 -07:00