Commit Graph

507 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
dba1b3436a [CSBindings] Diagnose leading-dot inference failure when base type is attempted as placeholder
Diagnose base inference failure only if base gets inferred to be
a placeholder, any transitive placeholder inference points to the
problem being elsewhere.
2023-05-01 14:19:14 -07:00
Pavel Yaskevich
53fdeb10b6 [ConstraintSystem] NFC: Print attributes/capabilities only for unbound type variables
Print bound type variables as `$T<Num> as <Type> @ <locator>` and
unbound ones as `$T<Num> [allows bindings to: ...] [attributes: ...] ... @ <locator>`
2023-04-10 19:15:43 -07:00
Pavel Yaskevich
8f2a9a7a9c [CSBindings] NFC: Log binding attempt failures
Currently we end up with an empty scope with no indication about
what actually happened i.e.:

```
(attempting type variable $T0 := Int
)
```
2023-04-06 22:53:53 -07:00
Pavel Yaskevich
c726b00741 [CSBindings] NFC: Format default bindings debug output
Default bindings should be printed as a comma separate list.
2023-03-13 14:01:48 -07:00
John McCall
db4b16e3dd [WIP] Always use PackTypes as the substitutions for type parameter packs
This simplifies the representation and allows clients to handle fewer
cases.  It also removes an ambiguity in the representation which could
lead us to have two canonical types for the same type.

This is definitely not working yet, but I'm not making progress on
it quickly enough to unblock what we need to unblock; it'll have to
be fixed in parallel.
2023-03-06 17:08:35 -08:00
Holly Borla
e6f29f8f93 Merge pull request #64119 from hborla/pack-expansion-shape-crash
[Diagnostics] Diagnose pack element expressions containing a non-pack a subexpression.
2023-03-05 10:11:02 -08:00
Holly Borla
8012e45109 [Diagnostics] Diagnose pack element expressions containing a non-pack subexpression. 2023-03-05 00:11:54 -08:00
Kavon Farvardin
091d63a5c8 revise how the Copyable constraint is added
The _Copyable constraint was implemented as a marker protocol.
That protocol is part of the KnownProtocol's in the compiler.
When `ASTContext::getProtocol(KnownProtocolKind kind)` tries
to find the ProtocolDecl for Copyable, it will look in the
stdlib module (i.e., Swift module), which is where I initially
planned to put it.

That created problems initially when some regression tests
use `-parse-stdlib` failed to do that protocol lookup, which is
essential for adding the constraint (given the current implementation).

That led to believe we need to pull Copyable out of the stdlib, but that's
wrong. In fact, when building the Swift module itself, we do `-parse-stdlib`
but we also include `-module-name Swift`. This causes the _Copyable protocol
defined in the Stdlib to be correctly discovered while building the stdlib
itself (see the test case in this commit). So, the only downside of
having the Copyable protocol in the Stdlib is that `-parse-stdlib` tests
in the compiler can't use move-only types correctly, as they'll be
allowed in generic contexts. No real program would build like this.

Until I have time to do a further refactoring, this is an acceptable trade-off.

fixes rdar://104898230
2023-03-04 11:42:09 -08:00
Pavel Yaskevich
fda7530418 [CSBindings] Relax early conjunction solving rule related to outer declarations
Instead of checking that there are no references to external declarations,
let's check that all of such declarations have their types resolved.
2023-02-15 00:25:44 -08:00
Pavel Yaskevich
5bb9d75e11 [CSBindings] Allow early conjunction solving if all of the contextual parameters are bound
Instead of allowing only disjunctions with no parameters, let's
use a contextual type to determine whether all of the parameters
have been inferred.
2023-02-15 00:24:17 -08:00