Commit Graph

489 Commits

Author SHA1 Message Date
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Kavon Farvardin
3ab4c7548c Sema: ban extensions of known marker protocols
There's no good reason to permit them. Conformances like Copyable and
Sendable are pervasive, so it's as though we are permitting extensions
of `Any`. Until there's a good argument in favor of such extensions,
remove the capability now.
2024-02-07 14:54:36 -08:00
Pavel Yaskevich
4cd167c918 Merge pull request #71393 from xedin/filter-out-escapable-from-leading-dot-in-generic-context
[CSBindings] Don't allow leading-dot base inference from Escapable pr…
2024-02-06 09:27:52 -08:00
Pavel Yaskevich
a613015b33 [CSBindings] Don't allow leading-dot base inference from Escapable protocol
Similarly to `Copyable`, `Escapable` shouldn't have any members
declared and as a result shouldn't be considered for leading-dot
base type inference.
2024-02-05 10:15:19 -08:00
Hamish Knight
05615fa0e4 NFC: Rename TypeWalker's SkipChildren to SkipNode
For consistency with ASTWalker.
2024-02-05 15:27:25 +00:00
Slava Pestov
acab50b21b AST: Update callers of ProtocolCompositionType::get() to handle inverses
And remove the old overload.
2024-02-01 23:35:33 -05:00
Alex Hoppen
2016f095a2 [CodeCompletion] Fix an issue that caused us to not produce any results for a guard condition inside a closure
rdar://121406956
2024-01-29 16:34:41 -08:00
Pavel Yaskevich
4407c0db07 [CSBindings] NFC: Fix a missing return warning in BindingSet::finalize 2024-01-04 10:03:25 -08:00
Pavel Yaskevich
6afc30761e [CSBindings] Prevent determineBestBindings from selecting unresolved key path type 2024-01-01 12:47:13 -08:00
Pavel Yaskevich
a288b1eb76 [CSBindings] Key path cannot be bound to typ erased type even if its existential
Extend existing check to handle type erased versions when they
are wrapped in an existential.
2023-12-19 13:36:46 -08:00
Pavel Yaskevich
fe2f1cab52 [CSBindings] Make it possible to enumerate supertypes of existentials
- Drop `mayHaveSuperclass` because it's too restrictive.
- Add logic to get superclass of existential and re-create
  existential type with all of the protocol requirements.
2023-12-19 13:36:35 -08:00
Pavel Yaskevich
bb20c7a66a [CSBindings] keypath-to-function conversion should respect sendability of the key path
If the underlying key path is not Sendable, the compiler generated
closure that captures the key path expression (as `{ [$kp$ = ...] in $0[keyPath: $kp$] }`)
cannot be marked as Sendable either.
2023-12-11 11:33:46 -08:00
Pavel Yaskevich
d270dade42 Merge pull request #70337 from xedin/rdar-72864716
[CSBindings] Unwrap optionals from contextual function type used as a…
2023-12-11 10:17:09 -08:00
Pavel Yaskevich
9cd9619a57 [CSBindings] Unwrap optionals from contextual function type used as a key path type
The key path is going to be implicitly wrapped into a contextual
optional type.

Resolves: rdar://72864716
Resolves: https://github.com/apple/swift/issues/56393
2023-12-08 15:08:17 -08:00
Pavel Yaskevich
9875fcf7e8 [CSBindings] Apply more checking to transitively inferred bindings
This makes sure that we never run into infinite recursion situations
with transitive bindings that could have been aseembled and not
properly checked.
2023-12-07 15:32:03 -08:00
Pavel Yaskevich
78c1e1be69 [CSBindings] Transitive key path root inference should make note of adjacent vars
Binding inference through a contextual root variable should include
marking contextual root and all of its adjacent variables as adjacent
to a key path root variable as well otherwise transitively inferred
bindings are ranked incorrectly.
2023-12-07 14:09:22 -08:00
Pavel Yaskevich
835fa02016 [CSBindings] Infer key path root bindings transitively through contextual root variable
Since generic arguments have to match exactly the inference could
be extended to transfer bindings through contextual root types if
they are sufficiently resolved (not delayed).

For example if key path expression is passed as an argument to
a parameter like `WritableKeyPath<$Root, $Value>` and `$Root`
is not yet bound but has a binding set of `{String}` its bindings
are transferable over to resolve the key path which would be
ultimately bound to the contextual type.
2023-12-07 14:09:22 -08:00
Pavel Yaskevich
0ec84e7bce Merge pull request #70214 from xedin/rdar-119040159
[CSBindings] Extend early array literal favoring to cover dictionaries
2023-12-05 14:38:55 -08:00
Pavel Yaskevich
911ba101e7 [CSBindings] Extend early array literal favoring to cover dictionaries
Follow-up to https://github.com/apple/swift/pull/70148

Just like with arrays it's advantageous to favor dictionary
literals over disjunctions to bind the elements together and
enable inference across elements and, as a consequence,
type pruning.

Resolves: rdar://119040159
2023-12-04 15:01:27 -08:00
Pavel Yaskevich
cbfe349e33 Revert "[CSBindings] Delay inference through OptionalObject if "object" is l-value capable"
This reverts commit 700a57ac4f.
2023-12-04 11:36:20 -08:00
Pavel Yaskevich
d54f03e455 Merge pull request #70175 from xedin/rdar-119055010
[CSBindings] Don't favor unresolved key path type over a conjunction
2023-12-04 09:33:35 -08:00
Pavel Yaskevich
cdd81667f4 [CSBindings] Don't favor unresolved key path type over a conjunction
If key path capability is not yet determined it cannot be favored
over a conjunction because:

1. There could be no other bindings and that would mean that
   key path would be selected even though it's not yet ready.
2. A conjunction could be the source of type context for the key path.

Resolves: rdar://119055010
2023-12-01 17:19:25 -08:00
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