Commit Graph

2431 Commits

Author SHA1 Message Date
Pavel Yaskevich
bfeade4e39 [CSSimplify] Cleanup some obsolete code from key path simplification
Since key path type is always bound by inference before the
constraint could be simplified, there is no need to search
for a contextual type or try to bind key path type if there
are errors in the path.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
161e126a91 [CSDiagnostics] Diagnose a key path literal without components early
Detect that diagnose key path literals that do not have any components.
2023-11-01 09:15:14 -07:00
Pavel Yaskevich
19e9cb0a91 [CSSimplify] Delay simplification of key path constraint until key path type is bound
Let the inference drive key path literal resolution, this fixes
a problem where key path is bound too eagerly.
2023-11-01 09:15:14 -07:00
Kavon Farvardin
29acda5136 Merge pull request #69406 from kavon/noncopyable-generics-pt2
[NoncopyableGenerics] handle `~Copyable` in `where`, `some`, and compositions.
2023-10-28 22:36:22 -07:00
Doug Gregor
ff45fec1d8 [Typed throws] Handle key-path literal used with a function that has typed throws
When providing a key-path literal for a parameter of function type
where that function type has a generic parameter for its thrown error
type, infer `Never` for the generic argument because key paths don't
throw.

Thanks to @xedin for realizing that this would be an issue.
2023-10-27 12:52:05 -07:00
Kavon Farvardin
66712ce6e0 [Sema] introduce InverseType
This type will become the corresponding type that is resolved for an
`InverseTypeRepr`. This kind of type is not expected to appear past type
 checking (currently, not even past requirement lowering!).
2023-10-23 10:37:22 -07:00
Kavon Farvardin
f76360c5b1 [Sema] "Noncopyable" means no Copyable conformance 2023-10-18 13:52:14 -07:00
Doug Gregor
a14f280104 Merge pull request #69087 from DougGregor/typed-throws-conformances 2023-10-10 21:16:44 -07:00
Pavel Yaskevich
12630f4c5c Merge pull request #69031 from xedin/rdar-116376651
[ConstraintSystem] A few improvements to key path handling and diagnostics
2023-10-09 23:27:45 -07:00
Doug Gregor
7d7c726efe [Typed throws] Teach associated type inference to infer from thrown errors
When comparing a requirement that uses typed throws and uses an
associated type for the thrown error type against a potential witness,
infer the associated type from the thrown error of the
witness---whether explicitly specified, untyped throws (`any Error`),
or non-throwing (`Never`).
2023-10-09 21:21:21 -07:00
Doug Gregor
ccf8619453 [Typed throws] Refactor thrown error subtyping check for reuse.
Lift the subtyping check for thrown error types out of the constraint
solver, so we can re-use it elsewhere.

There is a minor diagnostic change, from one that is actively
misleading (it shows a legitimate conversion that's wrong) to one that
is correct, which comes from us not treating "dropping throws" as a
legitimate way to handle equality of function types.
2023-10-09 21:21:21 -07:00
Doug Gregor
9340705294 Address code review comments 2023-10-07 07:29:02 -07:00
Pavel Yaskevich
b17ec0e385 [CSSimplify] Broaden delay of the key path assignment
If key path is connected to a disjunction directly or indirectly
(i.e. via ApplicableFunction constraint) do not attempt to bind
it until disjunction is taken, otherwise there is a risk to miss
a valid keypath-to-function conversion.
2023-10-06 17:47:10 -07:00
Pavel Yaskevich
cc0e621497 [CSFix] NFC: Remove obsolete IgnoreKeyPathContextualMismatch fix 2023-10-06 17:07:41 -07:00
Pavel Yaskevich
6fd6c16bf7 [CSSimplify] Key path should use a default type is contextual type is a placeholder
If there is something wrong with the context, let's assign a default
KeyPath type to the key path literal to avoid propagating placeholder
into the key path inference.
2023-10-06 16:59:53 -07:00
Pavel Yaskevich
4830c5be9a [CSSimplify] Unwrap of key path root can only yield one type
For methods there are two possibilities - force unwrap and
conditional, that's why we need a disjunction with two choices.

This is not the case for key path root type it could only
be force unwrapped.
2023-10-06 16:14:01 -07:00
Pavel Yaskevich
5a6c562b8c [CSSimplify] Adjust locators associated with keypath root/value matching constraints
`tryMatchRootAndValueFromType` anchored both root and value match
constraints directly on the key path expression. That is incorrect
because we have special locators for that.
2023-10-06 10:35:42 -07:00
Pavel Yaskevich
6a1783d48e [CSSimplify] Prevent overly eager solving of key path constraint
If key path literal is passed as an argument to a function/subscript
application its type cannot be resolved until the overload for the
call is selected, otherwise it could prevent a valid keypath-to-function
conversion in cases were argument ends up being a function type.
2023-10-05 17:10:47 -07:00
Doug Gregor
38b004d96e Tolerate missing Never types
This won't happen in "normal" Swift code, but can occur with tests that
use `-parse-stdlib`.
2023-10-05 12:46:11 -07:00
Doug Gregor
4b3d2f47d9 [Typed throws] Handle function conversions involving different thrown errors
Teach the constraint solver about the subtyping rule that permits
converting one function type to another when the effective thrown error
type of one is a subtype of the effective thrown error type of the
other, using `any Error` for untyped throws and `Never` for
non-throwing.

With minor other fixes, this allows us to use typed throws for generic
functions that carry a typed error from their arguments through to
themselves, which is in effect a typed `rethrows`:

```swift
func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U)
throws(E) -> [U] {
  var resultArray: [U] = .init()
  for value in array {
    resultArray.append(try body(value))
  }
  return resultArray
}
```
2023-10-05 11:55:05 -07:00
Pavel Yaskevich
06984b4307 Merge pull request #68964 from xedin/rdar-113675093
[CSSimplify] Determine whether type is know Foundation entity in a sa…
2023-10-05 08:41:15 -07:00
Pavel Yaskevich
b83a73d177 [CSSimplify] Determine whether type is know Foundation entity in a safer way
Instead of trying to get string representation of the type itself,
let's just get it based on the type name, which works well with the
list of types we have.

Resolves: rdar://113675093
2023-10-04 11:56:37 -07:00
Pavel Yaskevich
3ca54ae9da [CSGen] Use OptionalObject constraint to connect element type and result of next()
Previously this wasn't possible because of `one-way` bind constraints
involved in pattern inference but this no longer the case.
2023-09-29 17:33:07 -07:00
Pavel Yaskevich
204d496c36 Merge pull request #63889 from xedin/cache-conformance-checks-in-cs
[ConstraintSystem] Add cache for conformance lookups
2023-09-28 14:00:17 -07:00
Allan Shortlidge
b22b8f0a55 NFC: Removed unused function in CSSimplify.cpp. 2023-09-22 18:04:39 -07:00
Holly Borla
940ab81244 Merge pull request #68685 from hborla/minimize-constraint-system-isolation-checking 2023-09-22 07:31:45 -07:00
Holly Borla
2e9c64d36c [Concurrency] Do not allow actor isolation violations in function conversions
to impact constraint solving.
2023-09-21 19:44:03 -07:00
Kavon Farvardin
f1142d5da4 [nfc] rename or eliminate isPureMoveOnly APIs
I think from SIL's perspective, it should only worry about whether the
type is move-only. That includes MoveOnlyWrapped SILTypes and regular
types that cannot be copied.

Most of the code querying `SILType::isPureMoveOnly` is in SILGen, where
it's very likely that the original AST type is sitting around already.
In such cases, I think it's fine to ask the AST type if it is
noncopyable. The clarity of only asking the ASTType if it's noncopyable
is beneficial, I think.
2023-09-20 15:23:17 -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
Holly Borla
97f1e617fd [Concurrency] Replace ClosureActorIsolation with ActorIsolation throughout
the isolation query APIs.
2023-09-16 12:21:36 -07:00
Holly Borla
4b23564711 [Concurrency] Rename AbstractClosureExpr::getActorIsolation to
getClosureActorIsolation.

This is preparation for changing AbstractClosureExpr to store
ActorIsolation instead of ClosureActorIsolation, and convert to
ClosureActorIsolation when needed to allow incrementally updating
callers. This change is NFC.
2023-09-16 12:20:53 -07:00
Luciano Almeida
9c3f2aa2c1 Merge pull request #68301 from LucianoPAlmeida/coerce-function 2023-09-06 06:57:52 -03:00
Luciano Almeida
50130b052f [NFC] Adjusting a couple of comments for clarity 2023-09-05 18:18:40 -03:00
Luciano Almeida
0300139257 [Sema][Diagnostics] Improving diagnostics for function type coerce failure 2023-09-03 15:18:04 -03:00
Hamish Knight
c0ae9b950b Move out-of-place ThenStmt checking to constraint generation 2023-09-01 14:32:15 +01:00
Hamish Knight
6ee44f09b4 Introduce then statements
These allow multi-statement `if`/`switch` expression
branches that can produce a value at the end by
saying `then <expr>`. This is gated behind
`-enable-experimental-feature ThenStatements`
pending evolution discussion.
2023-09-01 14:32:14 +01:00
Pavel Yaskevich
ed046e5efe Merge pull request #68215 from xedin/rdar-112426330
[CSSimplify] Rework detection of missing/extraneous arguments in closure
2023-08-31 13:15:52 -07:00
Slava Pestov
361d49a843 AST: Remove DeclContext::getSelfProtocolType() 2023-08-30 15:15:08 -04:00
Pavel Yaskevich
1c8b8d981a [CSSimplify] Rework detection of missing/extraneous arguments in closure
Checking type variables is no longer necessary because constraint
system now stores types of all closures it encountered, this makes
detection logic more reliable as well.

Resolves: rdar://112426330
2023-08-30 10:47:41 -07:00
Pavel Yaskevich
b5b335c499 [CSSimplify] Modernize binary function argument reordering fix
The fix used incorrect locators for attempted argument conversions,
didn't check whether arguments are already re-ordered and allowed
fixes as visible side-effects from checking.

Resolves: rdar://114341979
2023-08-24 17:10:14 -07:00
Amritpan Kaur
71a94a859b [CSSimplify] Add checks for invalid keypath member refs 2023-08-21 13:01:22 -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
Amritpan Kaur
f4764b32bf [CSSimplify] Attempt tryMatchRootAndValueFromType even if
missing member.
2023-08-16 14:28:05 -07:00
Amritpan Kaur
feb0f34993 [CSSimplify] For function type binding for key path types, add flag
to allow it to go through resolveKeyPath in matchTypesBindTypeVar.
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
Pavel Yaskevich
cedbdf920c Merge pull request #67759 from xedin/rdar-112617922
[CSSimplify] Relax `isBindable` requirements for pack expansion variables
2023-08-15 09:00:12 -07:00
Slava Pestov
c26cafc47a AST: Remove bogus PackExpansionType logic in lookupConformance() 2023-08-09 18:28:22 -04:00
Pavel Yaskevich
27413f5624 [CSSimplify] Relax isBindable requirements for pack expansion variables
If type variable we are about to bind represents a pack
expansion type, allow the binding to happen regardless of
what the \c type is, because contextual type is just a hint
in this situation and type variable would be bound to its
opened type instead.

Resolves: rdar://112617922
2023-08-04 16:49:20 -07:00
Pavel Yaskevich
3dff3e5a49 [CSSimplify] Avoid eager matching between pack expansion and its contextual type
This is effectively a premature optimization. Contextual type might
not be fully resolved yet, so let's give solver a chance to do it for us.
2023-08-04 11:21:16 -07:00
Pavel Yaskevich
043d2aa3dd [CSSimplify] NFC: Straighten checking of type variable passed to resolvePackExpansion
The type variable should always be one for pack expansion type which
implies that locator always ends at `PackExpansionType`.
2023-08-03 14:11:09 -07:00