Commit Graph

2497 Commits

Author SHA1 Message Date
Pavel Yaskevich
e67dfc182b [CSSimplify] Remove obsolete key path capability inference and matching
The capability inference is now handled by the binding inference
and that makes resolved key path matching to bound type is no longer
necessary.
2023-11-08 19:54:31 -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
Angela Laar
eb277d9ee7 Merge pull request #67498 from angela-laar/implicit-sendable-for-methods
[Sema] Implicitly add @Sendable attribute to partial methods
2023-11-07 16:46:16 -08:00
Angela Laar
c09ec72e85 [Constraint System] Always add Sendable to unapplied function applications 2023-11-07 13:37:24 -08:00
Pavel Yaskevich
8b5e3848a1 [CSSimplify] Fix handling of holes by OptionalObject constraint
When "optional type" is a hole the simplification logic has to
simplify "object type" and mark all of its unresolved components
are potential holes, otherwise hole propagation won't work since
sometimes there is no other contextual information for such types
but "optional type".

Resolves: rdar://117871338
2023-11-03 10:13:09 -07:00
Pavel Yaskevich
b129d92937 Merge pull request #69549 from xedin/rdar-117227549
[ConstraintSystem] Rework overload ranking based on Sendable conformances
2023-11-02 09:42:49 -07:00
Pavel Yaskevich
736381286a [CSSimplify] NFC: Extract invalid function type detection from tryMatchRootAndValueFromType 2023-11-01 09:15:14 -07:00
Pavel Yaskevich
d74c4723dc [ConstraintSystem] De-duplicate key path constraint simplification
Move some of the checks from the constraint simplification into
`inferKeyPathLiteralCapability` and start using it for both
inference and constraint simplification.
2023-11-01 09:15:14 -07: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
8b4985054a [CSDiagnostics] Differentiate between key path type and value issues
Make sure that contextual mismatch uses a correct locator when
the issue is with key path value type instead of the key path
type.
2023-11-01 09:15:14 -07:00
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
Pavel Yaskevich
d0fae407f3 [CSSimplify] Increase score if conformance constraint finds missing conformances
Although the solver is allowed to use types with missing synthesizable
conformances we need to note their presence in the score in order
to rank overloads without such types higher.
2023-10-31 13:47:23 -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
Angela Laar
619a517a61 [CSSimplify] Check all conditional requirements for a type variable 2023-10-25 12:30:50 -07:00
Angela Laar
34ca702031 [Frontend] Feature flag for InferredSendableMethods 2023-10-25 12:30:24 -07:00
Angela Laar
9e408af419 [Sema] Global funcs should always be Sendable
Global funcs should be Sendable by default because they don't
capture anything.
2023-10-25 12:29:37 -07:00
Angela Laar
6879961c52 [Sema] Delay lookup for conditional conformances
Remove diagnostics and sendable checks from ActorIsolationChecker
2023-10-25 12:29:35 -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
Rose
d132708f1a Ensure the correct function is called by appending std:: to the std math functions 2023-10-01 22:19:58 -04: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