Commit Graph

517 Commits

Author SHA1 Message Date
Pavel Yaskevich
dde501485e [CSBinding] Prefer array literal type over a disjunction in certain cases
If array literal type is not delayed and doesn't have any type variables
associated with it, let's prefer it over a disjunction to facilitate
type propagation through its `Element` type to element expressions.

Resolves: rdar://118993030
2023-11-30 17:43:13 -08:00
Pavel Yaskevich
2701d431cf [CSBindings] Prefer key path type over a disjunction if key path is resolved
If key path type has bindings and is no longer delayed it means
that it's fully resolved and ready to be bound (even though value
type might not be resolved yet).
2023-11-30 16:26:10 -08:00
Pavel Yaskevich
40c8da6616 [ConstraintSystem] InferSendableFromCaptures: Start inferring sendability of key path expressions
When `InferSendableFromCaptures` feature is enabled `inferKeyPathLiteralCapability`
should examine subscript arguments to determine whether the key path type is
sendable or not, and if so, inform inference to produce `& Sendable` existential
type as a binding. Binding key path type is delayed until all subscript arguments
are fully resolved.
2023-11-28 13:02:17 -08:00
Pavel Yaskevich
2e1ca0a32a [ConstraintSystem] Extend KeyPathCapability to support sendability 2023-11-28 13:02:14 -08:00
Pavel Yaskevich
bd4ee4681b Merge pull request #70007 from xedin/keypath-application-improvements
[ConstraintSystem] Modernization of key path application handling
2023-11-28 12:44:42 -08:00
Pavel Yaskevich
32a7afc320 [CSBindings] Let key path subscript index be bound to existential in diagnostic mode
This parameter doesn't allow direct holes because it always replies
on the contextual type (if it's not present the hole should be
propagated to it), so if we know that the argument is some invalid
existential value (one without a superclass) let's allow binding to
it only in diagnostic mode which would be detected and diagnosed as
a contextual mismatch.
2023-11-22 20:30:08 -08:00
Pavel Yaskevich
f6254f7599 [ConstraintSystem] Support existential opening on key path subscript index 2023-11-17 15:49:31 -08:00
Pavel Yaskevich
72bb74aa4f [ConstraintSystem] Key path literals with completion tokens should be marked as invalid
Capability couldn't be determined for expressions like that which
means that inference should be delayed until root becomes available.

Resolves: https://github.com/apple/swift/issues/69936
2023-11-16 15:48:46 -08:00
Pavel Yaskevich
153bd9d72f [CSBindings] Add inferred key path type bindings only if there is no other choice
If key path literal type is converted to a function type, it's
always the best course of action to use such a binding and forego
adding a KeyPath type binding based on capability because it
would never match.

This applies to invalid key pathes as well because we'd want to
propagate as much contextual information from the key path literal
into the context as possible.
2023-11-10 23:45:20 -08:00
Pavel Yaskevich
0a2f1b14a7 [CSBindings] Delay key path type inference until literal capability is known
Since key path root is now transitively inferred. Key path type
inference can be delayed until key path is resolved enough to
infer its capability.

This solves multiple problems:

- Inference fully controls what key path type is bound to;
- KeyPath constraint simplification doesn't have to double-check
  the capability and attempt to re-bind key path type;
- Custom logic to resolve key path type is no longer necessary;
- Diagnostics are improved because capability and root/value type
  mismatch are diagnosed when key path is matched against the
  contextual type.
2023-11-08 19:54:31 -08:00
Pavel Yaskevich
54b5676188 [CSBindings] Infer key path root bindings transitively
As a first step in delaying key path type until all of the members are
resolved, attempt to infer a type of root based on bindings associated
with key path type.
2023-11-08 19:54:31 -08:00
Pavel Yaskevich
9bf9d549c4 [ConstraintSystem] Key path capability inference should indicate whether key path is invalid
This flag makes it easier to determine what binding to produce
from the default. In cases where some of the member references
are invalid it's better to produce a placeholder for a key
path type instead of letting the solver to attempt to fix more
contextual problems for a broken key path.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
0e1abc9f19 [CSBindings] Add special handling for key path default bindings
If there are no contextual types available but capability of
key path could be inferred, let's turn default into a real
binding.
2023-11-01 09:15:13 -07:00
Pavel Yaskevich
4ace882c12 [ConstraintSystem] Introduce a fallback type for key path literals
This is a very first step in attempt to move some of the logic
from `simplifyKeyPathConstraint` to the inference. This type is
going to be used as an anchor to trigger capability inference.
2023-11-01 09:14:15 -07:00
Pavel Yaskevich
978a22712e Merge pull request #69292 from xedin/delay-inference-from-OptionalObject
[CSBindings] Delay inference through OptionalObject if "object" is l-…
2023-11-01 09:13:37 -07:00
Pavel Yaskevich
8381f01e9e Merge pull request #69331 from xedin/dont-favor-vars-with-no-direct-bindings
[CSBindings] Don't priorize variables without direct bindings over di…
2023-10-24 14:51:54 -07:00
Pavel Yaskevich
0f194f4fcd [CSBindings] Don't priorize variables without direct bindings over disjunctions
It's possible that a disjunction could introduce new bindings
to the set.
2023-10-23 09:43:43 -07:00
Pavel Yaskevich
700a57ac4f [CSBindings] Delay inference through OptionalObject if "object" is l-value capable
Inference cannot be allowed in cases where both sides are type
variables and optional type is l-value capable because it results
in binding "optional" to an optional type and later discovering
a contextual type that is l-value optional i.e. if "optional type"
is resolved by selecting subscript overload.
2023-10-19 21:33:57 -07:00
swift-ci
c38077d919 Merge remote-tracking branch 'origin/main' into rebranch 2023-10-09 23:35:22 -07:00
Pavel Yaskevich
b095158684 [CSBinding] Don't produce type-erased key path bindings for a key path type
Such types are not going to be applied by `ConstraintSystem::resolveClosure`
anyway and they impede diagnostics by forcing the solver to produce mutliple
equal solutions.
2023-10-06 14:21:43 -07:00
swift-ci
67a872072d Merge remote-tracking branch 'origin/main' into rebranch 2023-10-04 10:17:15 -07:00
Pavel Yaskevich
5c83b18f64 [CSBindings] Infer key path types from superclass constrained existential values
If type of the key path expression is convertible to an existential
value with a superclass constraint represented as a known key path
type, we can use it to inform key path inference.

Resolves: rdar://93103421
2023-10-03 10:15:35 -07:00
Ben Barham
119d08c6d1 Merge remote-tracking branch 'origin/main' into 20230929-merge-rebranch
Conflicts:
  - `CMakeLists.txt` caused by the extra `-D` added in rebranch to
    reduce the number of deprecation warnings.
  - `lib/Frontend/PrintingDiagnosticConsumer.cpp` caused by the removal
    of one of the `#if SWIFT_SWIFT_PARSER` on rebranch (probably should
    have been done on main).
2023-09-29 09:50:50 -07:00
Pavel Yaskevich
32d265154f [ConstraintSystem] Add cache for conformance lookups
`lookupConformance` request is not cached and constraint solver
performs a lot of them for the same type (i.e. during disjunction
solving), let's try to cache previously performed requests to
see whether additional memory use is worth the performance benefit.
2023-09-20 00:59:22 +01:00
swift-ci
497f5224f5 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-07 09:54:22 -07:00
Pavel Yaskevich
56f390dde6 [CSBindings] Delay default binding production until inference is exhausted
Previously, default bindings (from Defaultable and FallbackType
constraints) where added to the set right after the first attempt,
but that is incorrect because binding producer should exhaust the
chain of superclasses and other "inferred" bindings first or risk
producing subpar solutions.
2023-09-04 10:39:15 -07:00
swift-ci
e0fc4308bd Merge remote-tracking branch 'origin/main' into rebranch 2023-08-16 23:13:54 -07:00
Pavel Yaskevich
d725e38cd2 Merge pull request #67971 from amritpan/kp-function-application
[ConstraintSystem] Resolve key path function applications via resolveKeyPath
2023-08-16 23:12:44 -07:00
swift-ci
87ce6d737c Merge remote-tracking branch 'origin/main' into rebranch 2023-08-16 16:14:00 -07:00
Amritpan Kaur
6d08e74915 [CSBindings] Add check to include key path function applications. 2023-08-16 12:53:54 -07:00
Pavel Yaskevich
b34f9720e4 [CSSimplify] Adjust {Any, Partial}KeyPath bindings right before resolving
A type variable that represents a key path literal cannot be bound
directly to `AnyKeyPath` or `PartialKeyPath`, such types could only
be used for conversions.
It used to be the task of the binding inference to infer `AnyKeyPath`
and `PartiaKeyPath` as a `KeyPath` using previously generated type
variables for a root and value associated with key path literal
(previously stored in the locator).

Recently we switched over to storing key path information in the
constraint system and introduced `resolveKeyPath` method to gain
more control over how key path type variable gets assigned.

Getting information from the constraint system creates a problem
for the inference because "undo" for some bindings would be run
after solver scope has been erased, so instead of modifying bindings
in `inferFromRelational`, let's do that in `resolveKeyPath` right
before the key path type variable gets bound which seems to be a
better place for that logic anyway.

Resolves: rdar://113760727
2023-08-15 13:50:54 -07:00
swift-ci
06112186ad Merge remote-tracking branch 'origin/main' into rebranch 2023-08-14 17:13:57 -07:00
Pavel Yaskevich
64a654dc0d Revert "[CSBindings] Prefer conjunctions over closure variables without bindings"
This reverts commit 11ee82de7b.
2023-08-14 10:02:05 -07:00
Becca Royal-Gordon
3eebc6dddb Merge branch 'main' into rebranch
# Conflicts:
#	lib/SILOptimizer/Differentiation/Common.cpp
2023-08-04 15:09:50 -07:00
Pavel Yaskevich
44cb14f849 Merge pull request #67441 from xedin/fix-overly-eager-closure-resolving
[CSBindings] Prefer conjunctions over closure variables without bindings
2023-08-04 09:42:50 -07:00
Amritpan Kaur
1384ff0038 [CSBinding] Allow inference to bind AnyKeyPath as a KeyPath
that can be converted to AnyKeyPath later.
2023-08-01 09:15:04 -07:00
Evan Wilde
309aed4925 Add SmallSetVector replacement
llvm::SmallSetVector changed semantics
(https://reviews.llvm.org/D152497) resulting in build failures in Swift.
The old semantics allowed usage of types that did not have an
`operator==` because `SmallDenseSet` uses `DenseSetInfo<T>::isEqual` to
determine equality. The new implementation switched to using
`std::find`, which internally uses `operator==`. This type is used
pretty frequently with `swift::Type`, which intentionally deletes
`operator==` as it is not the canonical type and therefore cannot be
compared in normal circumstances.

This patch adds a new type-alias to the Swift namespace that provides
the old semantic behavior for `SmallSetVector`. I've also gone through
and replaced usages of `llvm::SmallSetVector` with the
`Swift::SmallSetVector` in places where we're storing a type that
doesn't implement or explicitly deletes `operator==`. The changes to
`llvm::SmallSetVector` should improve compile-time performance, so I
left the `llvm::SmallSetVector` where possible.
2023-07-25 12:28:27 -07:00
Pavel Yaskevich
11ee82de7b [CSBindings] Prefer conjunctions over closure variables without bindings
If a closure doesn't have a contextual type inferred yet it should
be delayed in favor of already resolved closure conjunction because
"resolving" such a closure early could miss result builder attribute
attached to a parameter the closure is passed to.

Partially resolves https://github.com/apple/swift/issues/67363
2023-07-21 09:46:09 -07:00
Sophia Poirier
fa41015ae4 [ConstraintSystem] implement implicit pack materialization for abstract tuples instead of explicit '.element' 2023-07-14 10:32:38 -07:00
Alex Hoppen
1e723ddbc2 [CodeCompletion] Don't increase the score for holes at the code completion token 2023-07-07 19:50:46 +02:00
Alex Hoppen
6cec68e302 [IDE] Ignore score kinds that represent implicit conversions when solving for code completion
Ignore conversion score increases during code completion to make sure we don't filter solutions that might start receiving the best score based on a choice of the code completion token.
2023-07-07 19:50:46 +02:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Pavel Yaskevich
2ee646f47b [ConstraintSystem] NFC: Generalize DefaultClosureType constraint
This constraint is useful in more places than closures because
it gives a way to provide a "fallback type" without it having
effect on inference.
2023-06-16 11:15:15 -07:00
Pavel Yaskevich
1728cca5f0 [CSBindings] Mark type variables that represent type parameter packs as "involving type variables"
Such type variables are always dependent on pack expansion type variables
which should be bound first.
2023-05-16 16:32:24 -07:00
Pavel Yaskevich
ce826c2137 Revert "[CSBindings] Mark type variables that represent packs as involving type variables"
This reverts commit 8a39f3f38d.
2023-05-16 14:48:32 -07:00
Hamish Knight
b07f7b336e [CS] Improve handling of holes for Named/AnyPatterns
Rather than eagerly binding them to holes if the
sequence element type ends up being Any, let's
record the CollectionElementContextualMismatch fix,
and then if the patterns end up becoming holes,
skip penalizing them if we know the fix was
recorded. This avoids prematurely turning type
variables for ExprPatterns into holes, which
should be able to get better bindings from the
expression provided. Also this means we'll apply
the logic to non-Any sequence types, which
previously we would give a confusing diagnostic
to.
2023-05-04 14:53:58 +01:00
Pavel Yaskevich
bbe305cd43 [ConstraintSystem] Add same-shape constraint
The constraint takes two pack types and makes sure that their
reduced shapes are equal. This helps with diagnostics because
constraint has access to the original pack expansion pattern
types.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
9a395f091e [CSSimplify] Merge binding inference logic with PackElementOf constraint simplification
It's only safe to infer element type from `PackElementOf` constraint
when pattern type is fully resolved (because it can have pack element
archetypes which should be mapped out of context), the same applies
to the pattern type inference as well. Since constraints are re-activated
every time a referenced type variable is bound, simplication logic
can act as inference source by decaying into `Equal` constraints
where pattern/element type are resolved via surrounding information
first.
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
8a39f3f38d [CSBindings] Mark type variables that represent packs as involving type variables
Such type variables always depend on pack expansion variables.
2023-05-02 09:31:57 -07:00