Commit Graph

203 Commits

Author SHA1 Message Date
Pavel Yaskevich
dcbcd5f014 [CSBindings] Coalesce ExpressibleBy{Integer, Float}Literal early
The only possible default which could satisfy both protocols is
`Double`, otherwise it has to be some custom type which conforms
to both protocols. Either way it's best to leave `ExpressibleByFloatLiteral`
in place to get to `Double` faster or make sure that custom type
conforms to it even if it would later fail `ExpressibleByIntegerLiteral`
conformance, at least that wouldn't introduce any unviable bindings.
2020-12-17 12:29:31 -08:00
Pavel Yaskevich
715ef50a27 [CSBindings] Don't track ExpressibleByNilLiteral as a coverable protocol
Since `ExpressibleByNilLiteral` doesn't have a default type,
there is no need to track it in the set of protocols that could
produce defaultable bindings if not "covered" by existing bindings.
2020-12-17 12:29:05 -08:00
Pavel Yaskevich
b7bdda4db5 [CSBindings] Infer l-value type for object of ForceValueExpr only if var allows it
Otherwise l-value is going to get stripped from the type by
`addPotentialBinding` and we'd end up with duplicate bindings.
2020-12-15 17:07:54 -08:00
Pavel Yaskevich
9649b76042 [CSBindings] Don't generate bindings for defaults
Let's keep defaults separate from direct and transitive bindings,
that would make it easier to handle them in incremental model.

Instead of generating bindings for defaults and adding to the main
set, let's allow producer to choose what to do with them once type
variable has been picked for attempting.
2020-12-15 13:17:41 -08:00
Pavel Yaskevich
c6bdeea852 [ConstraintSystem] Strengthen dependent member type checks while inferring bindings
Look through specifier (inout, l-value) and optional types while
checking for presence of dependent member types to avoid inferring
incorrect bindings (which could lead to infinite recursion in the
solver).

Resolves: SR-13856
Resolves: rdar://problem/71383770
2020-12-14 16:39:13 -08:00
Pavel Yaskevich
22b34e8f36 [CSBindings] Let producer record a binding for a hole
As a step towards making binding inference more incremental, let's
make producer responsible for adding hole type binding instead of
doing so in `finalize`.
2020-12-10 15:39:38 -08:00
Pavel Yaskevich
e6784292cc [ConstraintSystem] Make binding producer responsible for attemping Any last
Instead of doing that while collecting bindings, let's move `Any`
to the end of the list when type variable has been selected to be
attempted next.
2020-12-09 13:34:01 -08:00
Pavel Yaskevich
ef673c74f1 [ConstraintSystem] Handle binding nullability in producer instead of collector
Wrapping bindings into optional type based on presence of
an `ExpressibleByNilLiteral` conformance requirement should
be done after type variable has been selected for attempting.
Otherwise such upfront work would be wasteful since it doesn't
affect binding ranking in any way.
2020-12-09 13:34:01 -08:00
Pavel Yaskevich
2619e2d8ac [ConstraintSystem] Track adjacent type variables while inferring bindings
Replaces `InvolvesTypeVariables` flag with a set of adjacent type
variables found during binding inference.

This approach is more suitable for incremental binding computation
because it allows to maintain a list of type variables that affect
ranking and check whether something has been resolved without having
to re-evaluate constraints associated with the given type variable.
2020-12-08 13:22:30 -08:00
Pavel Yaskevich
c1d178a9e0 [ConstraintSystem] NFC: Rename isFullyBound -> isDelayed and clarify associated comment 2020-12-08 00:54:09 -08:00
Pavel Yaskevich
38d25e99a0 [CSBindings] Refactor handling of disjunction and member constraints
Use `DelayedBy` in more places:

- If type variable is associated with a disjunction constraint let's
  mark it as been delayed by it (until the disjunction is actually
  attempted);

- Type variable is "delayed" by presence of member constraint
  (of any kind) only if it stands for a member type. Such type
  variable would be resolved once the member constraint is
  simplified.
2020-12-07 12:02:09 -08:00
Pavel Yaskevich
9df83750ed [CSBindings] Delay attempting type variable if it's involved in unresolved call
If type variable is still connected to `ApplicableFunction` that
means it's either an argument, result or represents a function
(member reference, operator) being applied. Regardless of exact
location such type variable should be delayed until call is
completely resolved to get additional context from parameters,
result type of a function being applied.
2020-12-04 21:50:04 -08:00
Pavel Yaskevich
54406b49de [ConstraintSystem] Track constraints that caused bindings to be "fully bound"
- Keep track of all constraints which caused a particular type variable to
  be de-prioritized.

- Convert a property to a method which would determine whether
  given set of potential bindings is "fully bound" and has to
  be delayed until certain constraints are simplified.
2020-12-04 21:50:04 -08:00
Pavel Yaskevich
85181360b1 Merge pull request #34810 from xedin/rdar-66709164
[ConstraintSystem] Detect and diagnose extraneous return(s) in result build body
2020-12-02 15:51:10 -08:00
Holly Borla
1a20cbe3e1 Merge pull request #34918 from xedin/rdar-71858936
[ConstraintSystem] Mark type variable representing closure parameter …
2020-12-02 09:06:55 -05:00
Pavel Yaskevich
5ebba42268 [ConstraintSystem] Mark type variable representing closure parameter (in the body) as incomplete
Restore recently removed logic to mark type variable representing
closure parameter used in the body of a closure as potentially
incomplete to delay attempting it until `BindParam` is simplified.

Resolves: rdar://problem/71858936
2020-12-01 20:35:34 -08:00
Pavel Yaskevich
96415b237e [CSBinding] Attempt to join any existing and viable bindings with new binding
Instead of attempting to join only the last recorded supertype
binding, let's attempt to join any previously recorded supertype
binding with an incoming one to make sure that set contains only
the most viable types.
2020-11-19 18:06:35 -08:00
Pavel Yaskevich
d912e12ba7 [ResultBuilders] Use new fix to detect and diagnose use of return statements in a result builder body 2020-11-19 17:21:00 -08:00
Nathan Hawes
1eb05b7760 Merge pull request #34733 from nathawes/migrate-unresolved-completion
[CodeCompletion][Sema][Parse] Migrate unresolved member completion to the solver-based completion implementation
2020-11-19 01:26:43 -08:00
Nathan Hawes
ca7fb37aba [CodeCompletion][Sema][Parse] Migrate unresolved member completion to the solver-based completion implementation
Following on from updating regular member completion, this hooks up unresolved
member completion (i.e. .<complete here>) to the typeCheckForCodeCompletion API
to generate completions from all solutions the constraint solver produces (even
those requiring fixes), rather than relying on a single solution being applied
to the AST (if any). This lets us produce unresolved member completions even
when the contextual type is ambiguous or involves errors.

Whenever typeCheckExpression is called on an expression containing a code
completion expression and a CompletionCallback has been set, each solution
formed is passed to the callback so the type of the completion expression can
be extracted and used to lookup up the members to return.
2020-11-13 15:37:14 -08:00
Pavel Yaskevich
afa11989c9 [ConstraintSystem] Determine whether bindings are potentially incomplete on demand 2020-11-13 13:45:58 -08:00
Pavel Yaskevich
7eb623d72c [ConstraintSystem] Marking closure parameter as potentially incomplete is not longer necessary
Since closure body isn't opened until its contextual type is resolved
there is no need to explicitly delay attempting bindings for closure
parameter used in the body anymore.
2020-11-12 17:44:08 -08:00
Pavel Yaskevich
6a7cc79f83 [ConstraintSystem] Determine whether type variable is a hole on demand 2020-11-11 15:31:24 -08:00
Pavel Yaskevich
4ed9794547 [ConstraintSystem] Compute whether bindings are bound only by existentials on demand 2020-11-11 14:19:49 -08:00
Pavel Yaskevich
7f228b0e31 [ConstraintSystem] Compute # of defaultable bindings on demand 2020-11-11 12:35:53 -08:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Pavel Yaskevich
8024b7bef3 [CSBindings] Improve equivalence class handling in transitive protocol inference
Let all of the members of the equivalence class be represented by
the first type variable encountered during the depth-first walk.
This means that supertypes are inferred from the members and all
of the transitive protocol requirements are distributed among the
members upon return back to the "representative".
2020-10-15 16:27:36 -07:00
Pavel Yaskevich
d011bf3d7d [CSBindings] Implement transtive protocol requirement inference
Implements iterative protocol requirement inference through
subtype, conversion and equivalence relationships.

This algorithm doesn't depend on a type variable finalization
order (which is currently the order of type variable introduction).

If a given type variable doesn't yet have its transitive protocol
requirements inferred, algorithm would use iterative depth-first
walk through its supertypes and equivalences and incrementally
infer transitive protocols for each type variable involved,
transferring new information down the chain e.g.

  T1   T3
   \   /
    T4    T5
     \    /
       T2

Here `T1`, `T3` are supertypes of `T4`, `T4` and `T5`
are supertypes of `T2`.

Let's assume that algorithm starts at `T2` and none of the involved
type variables have their protocol requirements inferred yet.

First, it would consider supertypes of `T2` which are `T4` and `T5`,
since `T5` is the last in the chain algorithm would transfer its
direct protocol requirements to `T2`. `T4` has supertypes `T1` and
`T3` - they transfer their direct protocol requirements to `T4`
and `T4` transfers its direct and transitive (from `T1` and `T3`)
protocol requirements to `T2`. At this point all the type variables
in subtype chain have their transitive protocol requirements resolved
and cached so they don't have to be re-inferred later.
2020-10-15 16:27:35 -07:00
Pavel Yaskevich
a9cce605a4 [CSBindings] Shrink binding "sources" to supertype and equivalence only
Instead of recording all of the binding "sources" let's only record
subtype, supertype and equivalence relationships which didn't materialize
as bindings (because other side is a type variable).

This is the only information necessary to infer transitive bindings
and protocol requirements.
2020-10-15 16:27:35 -07:00
Pavel Yaskevich
0c51159228 [CSBindings] Record constraint associated with a subtype relationship
While inferring bindings, let's record not only the fact that current
type variable is a subtype of some other type variable but track
constraint which establishes this relationship.
2020-10-15 16:27:35 -07:00
Pavel Yaskevich
17f7ad82ca [CSBindings] Record all of the protocol requirements (not just literal) 2020-10-15 16:27:35 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Pavel Yaskevich
ab951c208a [ConstraintSystem] NFC: Move ConstraintGraph{Scope}.h to include/swift/Sema 2020-10-08 10:42:58 -07:00
Pavel Yaskevich
f94be56468 [Sema] Decouple ConstraintSystem and TypeChecker headers 2020-10-06 13:18:49 -07:00
Pavel Yaskevich
a6c44bfc54 [CSBindings] If hole originates from code completion token avoid fixing it
If the hole is originated from code completion expression
let's not try to add a fix, anything connected to a
code completion is allowed to be a hole because presence
of a code completion token makes constraint system
under-constrained due to e.g. lack of expressions on the
right-hand side of the token, which are required for a
regular type-check.
2020-09-29 12:54:38 -07:00
Pavel Yaskevich
c22027260d [CSBindings] Infer any type variable connected to a code completion token as a potential hole
If type variable is associated with a code completion token
it's possible that it doesn't have enough contextual information
to be resolved to anything, so let's add a hole type which originates
from type variable associated with code completion expression to
make this relationship explicit and avoid "fixing" problems rooted
in fact that type variable is underconstrained due to code completion.
2020-09-29 12:53:50 -07:00
Pavel Yaskevich
427c6478bc [ConstraintSystem] ConstraintSystem cannot be const when inferring new bindings
It's possible that bindings would need to mutate constraint system
e.g. by allocating new locators.
2020-09-29 12:46:26 -07:00
Pavel Yaskevich
225d87a9bd [CSBindings] NFC: Simplify TypeVariableBinding::attempt but extracting fix computation
Add a separate method `fixForHole` on `TypeVariableBinding`
responsible for determining whether fix is required and if so,
what kind of fix to apply when a particular type variable is
resolved to a hole.
2020-09-28 15:40:19 -07:00
Pavel Yaskevich
e30bdacd57 [CSBindings] Adjust impact of an event when nil is bound to a hole 2020-09-25 15:00:47 -07:00
Pavel Yaskevich
1b5ce2b88f [CSBindings] Start recording SpecifyContextualTypeForNil fix when nil is bound to a hole 2020-09-25 15:00:32 -07:00
Nathan Hawes
a1ef6e4dac Merge pull request #33749 from nathawes/new-member-completion
[CodeCompletion] Update member completion to handle ambiguous and invalid base expressions
2020-09-09 18:51:22 -07:00
Pavel Yaskevich
b7d5ea844a Merge pull request #33740 from xedin/rdar-68094328
[CSBindings] Delay binding chain result if type is an optional of a…
2020-09-01 10:17:36 -07:00
Pavel Yaskevich
6a99fde6a6 [CSBindings] Delay binding chain result if type is an optional of a type variable
If subtyping is allowed for a result type of an implicit member chain,
let's delay binding it to an optional until its object type resolved too or
it has been determined that there is no possibility to resolve it.
Otherwise we might end up missing solutions since it's allowed to
implicitly unwrap base type but it can't be done early - type variable
representing chain's result type has a different l-valueness comparing
to generic parameter of an optional.

This used to work before due to a hack in constraint generator where
unresolved member with arguments would return base type (which
doesn't allow l-value) instead of a result type.

Resolves: rdar://problem/68094328
2020-09-01 00:51:09 -07:00
Pavel Yaskevich
20f8a37357 [ConstraintSystem] Allow code completion type to be a hole
Code completion expression type could be a hole if and only
if there is obosolutely no contextual information available
e.g. `let _ = .#^MEMBER^#`
2020-08-31 16:56:05 -07:00
Pavel Yaskevich
ebc03a1805 [CSBindings] NFC: Remove dead condition leftover from Swift version 3 support 2020-08-28 17:39:21 -07:00
Pavel Yaskevich
c03d76291c [ConstraintSystem] NFC: Clarify why upward propagation of literal conformances is ncessary at the moment
This is NFC because only literal protocols are tracked at the moment.

Forward propagate (subtype -> supertype) only literal conformance
requirements since that helps solver to infer more types at
parameter positions.

```swift
func foo<T: ExpressibleByStringLiteral>(_: String, _: T) -> T {
  fatalError()
}

func bar(_: Any?) {}

func test() {
  bar(foo("", ""))
}
```

If one of the literal arguments doesn't propagate its
`ExpressibleByStringLiteral` conformance, we'd end up picking
`T` with only one type `Any?` which is incorrect.

This is not going to be necessary once bindings are filtered based
of requirements placed on a type variable.
2020-08-28 17:30:19 -07:00
Pavel Yaskevich
9665091121 [CSBindings] Don't attempt to join types that contain holes 2020-08-27 11:51:00 -07:00
Pavel Yaskevich
d039107bdb [ConstraintSystem] Replace use of UnresolvedType with specialized HoleType 2020-08-26 18:43:10 -07:00
Pavel Yaskevich
56ef379fb8 [ConstraintSystem] Integrate IgnoreInvalidFunctionBuilderBody into the solver 2020-08-17 12:39:29 -07:00
Luciano Almeida
b1eccb594d [Sema] Do not diagnose contextual type mismatches for malformed key path expressions (#33230)
* [AST] Adding hasSingleInvalidComponent to key path expression

* [Sema] Adding a new fix and failure to diagnose missing key path component

* [Sema] Recording new fix for key path missing components and remove diagnose from pre-check

* [tests] Adjusting key path missing component contextual tests

* [Sema] Renaming missing component key path fix and failure

* [Sema] Correcting comments typos
2020-08-16 12:27:21 -03:00