Commit Graph

507 Commits

Author SHA1 Message Date
Slava Pestov
73aa029a8b Sema: Generalize heuristic in favoredOverDisjunction() 2024-09-09 11:18:58 -04:00
Pavel Yaskevich
23577af715 Merge pull request #76299 from xedin/rdar-135203192
Revert "[CSBindings] Mark generic parameter type vars as complete if they don't have any adjacent vars"
2024-09-06 13:08:32 -07:00
Pavel Yaskevich
401f3f3338 Revert "[CSBindings] Mark generic parameter type vars as complete if they don't have any adjacent vars"
This reverts commit 22e0f4a3d6.

Resolves: rdar://135203192
2024-09-05 15:07:42 -07:00
Pavel Yaskevich
10082c6011 [CSBindings] Re-implement closure de-prioritization in assignment context
Re-implements https://github.com/swiftlang/swift/pull/76115 in
a simpler fashion now that we don't have to eagerly binding destination
type on l-value.
2024-08-30 15:39:24 -07:00
Pavel Yaskevich
d8750a83f8 [CSSimplify] Allow l-value object simplication if l-value type is a placeholder
Placeholders propagate but we still can allow contextual inference,
to facilitate that the solver should consider `l-value object` constraint
to be solved and allow placeholders on "object" type.
2024-08-30 15:39:18 -07:00
Pavel Yaskevich
0a4ff7df81 [CSBindings] Allow inference of l-value type from its object type
This aims to help with cases like `_ = { x in x = 0 }` or `.test = 42`
which are currently supported because the member is eagerly bound to
an `@lvalue $T` during constraint generation.
2024-08-29 16:49:44 -07:00
Pavel Yaskevich
5a93255133 [ConstraintSystem] Introduce new LValueObject constraint 2024-08-29 10:10:14 -07:00
Pavel Yaskevich
c8690b34e7 [CSBindings] Don't prioritize closures that are assigned to overloaded members
Constraint generation uses a special pattern while generating
constraints for assignments to make sure that the source type
is convertible to r-value type of the destination.

`BindingSet::favoredOverDisjunction` needs to recognize this
pattern, where disjunction type variable is bound early, and
avoid prioritizing closure if it's connected to the "fixed type"
of the disjunction or risk losing annotations associated with
the member such as `@Sendable` or a global actor.

Resolves: rdar://131524246
2024-08-27 16:19:29 -07:00
Hamish Knight
4beaaf32ad [CS] Improve placeholder diagnostics slightly
Make sure `CouldNotInferPlaceholderType` can
produce a diagnostic for a `PlaceholderType`
locator element, and avoid emitting an extra
diagnostic for a placeholder type in an invalid
position.
2024-07-07 23:42:33 +01:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Pavel Yaskevich
d7a06284d3 [CSBindings] Optional object type variable should be connected to the optional
If both sides of an `OptionalObject` constraint are un-inferred, their
binding sets need to correctly reflect adjacency - a type variable
that represents optional would get "object" as an adjacency through
its potential binding (the binding is - "object" wrapped in a single
level of optional) and "object" type variable needs to get its parent
optional type variable added to its adjacency list explicitly.
Without this it would be possible to prematurely pick "object" before
its parent optional type variable.

Resolves: https://github.com/apple/swift/issues/73207
Resolves: rdar://126960579
2024-05-28 15:45:03 -07:00
Pavel Yaskevich
7594e43b2c Revert "[CSBindings] Delay inference through OptionalObject if "object" is l-value capable" 2024-04-30 13:43:37 -07:00
Pavel Yaskevich
7654afd3e6 [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.
2024-04-17 10:31:18 -07:00
Pavel Yaskevich
559927da59 Merge pull request #71931 from xedin/relax-potential-incompleteness-of-generic-param-vars
[CSBindings] Mark generic parameter type vars as complete if they don…
2024-03-18 09:39:05 -07:00
Erik Eckstein
3c76464c1c rename withConcurrent -> withSendable
That was missed when "concurrent" was renamed to "sendable"
2024-03-13 09:58:31 +01:00
li3zhen1
00b0491676 [CSDiagnostics] Update UnableToInferGenericPackElementType to accept overloads and update test cases 2024-02-27 20:35:06 -05:00
Pavel Yaskevich
22e0f4a3d6 [CSBindings] Mark generic parameter type vars as complete if they don't have any adjacent vars
Always marking generic parameter type variables as incomplete
is too aggressive. That was the way to make sure that they
are never attempted to eagerly, but really there are only a
couple of situations where that can cause issues:

```
1. Int <: $T_param
   $T1 <: $T_param

2. $T2 conv Generic<$T_param>
   $T2 conv Generic<Int?>
   Int <: $T_param
```

Attempting $T_param before $T1 in 1. could result in a missed
optional type binding for example.

Attempting $T_param too early in this case (before $T2) could
miss some transitive bindings inferred through conversion
of `Generic` type.

If a type variable that represents a generic parameter is no longer
associated with any type variables (directly or indirectly) it's safe
to assume that its binding set is complete.
2024-02-27 14:48:53 -08:00
Li, Zhen
b44fbf60c6 [CSDiagnostics] Add diags for holes in generic parameter pack 2024-02-21 23:25:25 -05:00
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