Commit Graph

517 Commits

Author SHA1 Message Date
Slava Pestov
434b02166e Sema: LLVM_DEBUG for CSBindings.cpp 2024-10-07 16:50:46 -04:00
Slava Pestov
3dc16a9f5b Sema: Record defaulted constraints in the trail 2024-10-01 18:10:20 -04:00
Slava Pestov
6652e34f55 Sema: Add SolverTrail::Change::InferredBindings and ::RetractedBindings 2024-09-30 21:29:59 -04:00
Slava Pestov
94494b660b Sema: Make PotentialBindings::infer() and ::retract() idempotent 2024-09-30 21:29:59 -04:00
Slava Pestov
148033930c Sema: Split off introduceToInference() into its own Change
Previously, retractFromInference() was the last step in
unbindTypeVariable(). This doesn't really make sense,
because bindTypeVariable() doesn't call introduceToInference();
its two callers do it later.

Start untangling this by splitting off introduceToInference()
into its own Change, but for now, record this change at the
incorrect place to maintain the same behavior as before.
2024-09-29 21:46:41 -04:00
Pavel Yaskevich
ebc4e8d2d7 Merge pull request #76584 from xedin/remove-outdated-comment
[CSBindings] NFC: Remove outdated comment
2024-09-19 20:33:52 -07:00
Pavel Yaskevich
b712b501c0 [CSBindings] NFC: Remove outdated comment
The closure literal check has been removed but the comment
was left behind.
2024-09-19 13:21:35 -07:00
Pavel Yaskevich
2c9d05991f [CSBindings] Prevent BindingSet::isViable from dropping viable bindings
I think the original idea was to elide `Array<$T>` if there is
a binding a resolved generic arguments i.e. `Array<Float>`, but
the check doesn't account for the fact that bindings could be
of different kinds and there are some implicit conversions that
could be missed if we remove the bindings.

For example, given the following constraints:

`Array<$T0> conv $T1`
`$T1 conv Array<(String, Int)>`

`$T0` can be a supertype of `Array<$T0>` and subtype of `Array<(String, Int)>`.

The solver should accept both types as viable bindings because the
`$T0` could be bound to `(key: String, value: Int)` and that would
match `Array<(String, Int)>` conversion.
2024-09-16 09:54:12 -07:00
Slava Pestov
6fb66db6da Merge pull request #76318 from slavapestov/favored-over-disjunction-heuristic
Sema: Generalize heuristic in favoredOverDisjunction()
2024-09-14 08:45:09 -04:00
Pavel Yaskevich
279ef7de2d [CSBindings] Infer bindings through inout in some cases
Allow inferring type of `inout` from a pointer type
(or optional thereof) but delay the binding set because
it might not be complete and object type of `inout` could
also be an Array or C-style pointer type.
2024-09-13 05:08:26 -07:00
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