Commit Graph

489 Commits

Author SHA1 Message Date
Pavel Yaskevich
8e90882ee1 [CSBindings] Allow retracting constraints previously used to compute potential bindings
This allows to avoid having to recompute bindings when solver backtracks.
2021-02-17 17:25:42 -08:00
Pavel Yaskevich
64323a0149 Revert "[SR-12033] [Sema] Do not allow inferring defaultable closure () -> $T for autoclosure arguments result" 2021-02-17 10:28:30 -08:00
Frederick Kellison-Linn
e4ea1678dc Rename HoleType to PlaceholderType
HoleType basically served the same purpose as PlaceholderType. This commit unifies the two.
2021-02-16 22:59:19 -05:00
Frederick Kellison-Linn
fbb78cd006 [Sema] Convert placeholder types to type vars wherever we open generics
Rename `openUnboundGenericTypes` to `convertInferableTypes`. In addition to unbound generics, this method also converts placeholder types to fresh type variables.
2021-02-16 22:59:19 -05:00
Pavel Yaskevich
655aeef7ca [CSBindings] NFC: Associate isCoveredBy(PotentialBinding) with literal requirement 2021-02-16 16:57:50 -08:00
Pavel Yaskevich
b3284c6a9d [CSBindings] Track source constraint of an adjacent variable
This makes it much easier to remove adjacencies when constraint
is retracted from the bindings.
2021-02-11 15:40:19 -08:00
Pavel Yaskevich
bdfd4a8863 Merge pull request #35833 from xedin/couple-minor-incrementality-adjustments
[CSBindings] NFC: Couple minor incrementality adjustments
2021-02-09 18:22:00 -08:00
Pavel Yaskevich
daa4ffffa2 Merge pull request #35503 from LucianoPAlmeida/SR-12033-autoclosure
[SR-12033] [Sema] Do not allow inferring defaultable closure `() -> $T` for autoclosure arguments result
2021-02-09 16:23:51 -08:00
Pavel Yaskevich
c7169edf8f [CSBindings] NFC: Drop result type from since it's no longer useful 2021-02-09 09:09:31 -08:00
Pavel Yaskevich
b004dce44b [CSBindings] Attempt l-value fallback for force unwrap only after r-value type failed
Previously l-value binding would be paired with discovered r-value
one but that is not necessary and can cause issues with ranking
since there is no way to filter this "fallback" binding from the
set.
2021-02-09 09:04:25 -08:00
Pavel Yaskevich
f7432f820b [CSBindings] Attempt Void fallback for closure result only when other choices have failed
Since there is a conversion from `() -> T` to `() -> Void` for closures,
solver has to attempt a `Void` when other bindings (inferred from the body)
have failed.

Let's try it post factum instead of adding `Void` fallback to the
binding set after every discovered supertype binding. Old scheme
wouldn't work in incremental mode anyway because it's possible to
find a subtype binding (which makes it clear that body has to have
a result type) after a supertype and end up attempting `Void` although
it would be incorrect.
2021-02-08 13:35:51 -08:00
Pavel Yaskevich
8726d8889c [CSBindings] Filter out some of the constraints which can't delay hole propagation
If member type is unknown (determined to be a hole) `applicable function`
of any kind or `overload choice` cannot delay attempting it because doing
so would allow solver to make forward progress.
2021-02-08 12:29:49 -08:00
Luciano Almeida
fccb3e00e8 [CSBindings] Prevent solver from infering default binding to auto closures arguments 2021-02-05 20:20:57 -03:00
Pavel Yaskevich
8803281d6b Merge pull request #35701 from xedin/var-cannot-cover-literal
[CSBindings] Literal coverage checking should account for type variab…
2021-02-02 17:19:50 -08:00
Pavel Yaskevich
1965f3ec3c [CSBindings] Literal coverage checking should account for type variable/hole embedded in optional
Currently `isLiteralCoveredBy` only checks for top-level type variable or hole.

That check has to be sunk down so it could be used after optionality is stripped
away (when possible), otherwise bindings like `$T0?` are (incorrectly) determined
to cover literal requirements (because conformance check always succeeds when
attempted on a type variable).
2021-02-01 17:52:29 -08:00
Pavel Yaskevich
50a76c895f [CSBindings] New binding formed by join operation should refer to constraint that triggered join
Using existing binding with updated type would result in incorrect
behavior in incremental mode since when "originating" (new) constraint
gets retracted it would leave a stale binding behind.
2021-02-01 16:55:05 -08:00
Pavel Yaskevich
b03dc63634 [AST] Adjust TypeBase::getTypeVariables to accept a set
Currently the pattern is to collect the type variables and then unique
them. Instead of asking clients to do uniquing, let's just accept a set
as an argument.
2021-01-26 18:13:34 -08:00
Pavel Yaskevich
ac8897cc44 Merge pull request #35529 from xedin/never-fail-inference
[CSBindings] Inference cannot fail
2021-01-21 12:39:00 -08:00
Pavel Yaskevich
df43268929 [CSBindings] Inference cannot fail
Any constraints which would previously cause binding inference to
fail should instead delay associated type variable and preserve
all of the collected information.

This is vital for incremental binding computation that cannot
re-introduce constraints after "failure" because it's too expensive.
2021-01-20 16:58:05 -08:00
swift-ci
6c41322f0d Merge pull request #35504 from LucianoPAlmeida/update-comment 2021-01-20 05:44:52 -08:00
Luciano Almeida
0f6a4c06c7 [NFC] Update comment that mentioned old mark as FullyBound way to delay bindings 2021-01-19 22:40:12 -03:00
Pavel Yaskevich
48afd65ec2 [CSBindings] NFC: Convert checkTypeOfBinding into a static function 2021-01-15 15:04:28 -08:00
Pavel Yaskevich
cb4e597921 [ConstraintSystem] NFC: Convert getPotentialBindingForRelationalConstraint into a method on PotentialBindings 2021-01-15 15:04:13 -08:00
Pavel Yaskevich
72888ca29b [ConstraintSystem] NFC: Extract PotentialBindings and auxiliary struct from ConstraintSystem
This opens up a posibility of using `PotentialBindings`
in `ConstraintGraphNode` and other places in `ConstraintGraph`.
2021-01-15 15:03:54 -08:00
Pavel Yaskevich
b4f28b4883 [ConstraintSystem] NFC: Move implementation of PotentialBindings::dump(...) into cpp 2021-01-15 15:03:39 -08:00
Pavel Yaskevich
afec25271e [ConstraintSystem] Extract PotentialBinding and its auxiliary classes into a separate header
Create a new namespace - `swift::constraints::inference` and associate
`PotentialBinding` with it. This way it would be possible for constraint
graph to operate on `PotentialBinding(s)` in the future.
2021-01-15 15:03:24 -08:00
Pavel Yaskevich
7617b7418e [CSBindings] Re-introduce joined bindings instead of updating in place
Make `PotentialBinding` immutable and re-introduce products of
type join instead.
2021-01-12 11:09:14 -08:00
Pavel Yaskevich
03b6b41774 [CSBindings] NFC: Don't pass constraint system as an argument to infer* methods
`PotentialBindings` already reference the constraint system they
belong to, so there is no need to pass it as an argument to inference
methods.
2021-01-12 00:53:46 -08:00
Pavel Yaskevich
aa887fd66a [ConstraintSystem] Turn binding storage into a SetVector
Currently potential bindings are stored in a vector (`SmallVector`)
and every call has to pass additional set of unique types to
inference methods to unqiue the bindings. Instead let's merge
these two together and use `SetVector` for binding storage,
which would also be great for incremental mode that can't
pass additional sets around.
2021-01-12 00:52:55 -08:00
Pavel Yaskevich
8356ab430b [ConstraintSystem] NFC: Avoid use of designated initializers to make Windows happy 2021-01-05 13:25:57 -08:00
Pavel Yaskevich
2393a7c9a4 [CSBindings] Represent literal requirements as a struct instead of a tuple
Doing so streamlines access to the information associated with literal
protocol requirements and allows to add more helpers.

Also cache default type in the struct itself for easy access.
2021-01-04 10:32:08 -08:00
Pavel Yaskevich
e676e05f13 [CSBindings] Transform literal requirements into bindings when type variable is attempted 2021-01-04 09:20:14 -08:00
Pavel Yaskevich
731810e184 [CSBindings] Check literal coverage as new bindings/requirements are discovered
One more step towards incrementality of binding inference. Instead of
trying to determine literal protocol coverage during finalization
of the bindings, let's do that as soon as new bindings and/or literal
protocol requirements are discovered.
2021-01-04 09:20:14 -08:00
Pavel Yaskevich
41623abe49 [CSBindings] Compute literal information on demand 2021-01-04 09:20:14 -08:00
Pavel Yaskevich
650c54b822 [CSBindings] Split literals from other protocol requirements
Literal protocol requirements are handled differently from other
protocols, they require additional contextual information (such
as coverage, direct/transitive distinction), and participate in
binding inference (could be turned into default bindings).
2021-01-04 09:20:14 -08:00
Pavel Yaskevich
c4c401b273 [CSBindings] Avoid double-checking whether protocol requirement is direct 2020-12-17 12:29:43 -08:00
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