Commit Graph

28619 Commits

Author SHA1 Message Date
Slava Pestov
a88c5ca74c Merge pull request #82581 from slavapestov/fix-issue-82160
AST: Better cope with UnboundGenericType in TypeBase::getSuperclass()
2025-06-28 14:40:42 -04:00
Doug Gregor
1d5fecb0e2 Merge pull request #82584 from DougGregor/nonisolated-deinit-availability
"nonisolated deinit" does not have back-deployment constraints
2025-06-28 06:47:31 -07:00
Hamish Knight
e912eeb2f4 [CS] Use the resolved type in NonOptionalUnwrapFailure::diagnoseAsError
Diagnostics can outlive the ConstraintSystem itself if we have a
diagnostic transaction for e.g `typeCheckParameterDefault`, make sure
we don't try to use a solver-allocated type as an argument.
2025-06-28 12:13:19 +01:00
Hamish Knight
2b05212ff5 [CS] Avoid escaping solver-allocated types in computeSubstitutions
Make sure we call `simplifyType` for the opened type bindings to
ensure holes get converted to UnresolvedType.

rdar://154553285
2025-06-28 11:19:13 +01:00
Doug Gregor
b75d8f054e Merge pull request #81468 from DougGregor/no-main-actor-when-conforming-to-sendable
[SE-0466] Don't infer @MainActor on types conforming to Sendable
2025-06-28 01:23:41 -07:00
Pavel Yaskevich
358067917e [Concurrency] Fix a crash caused by misuse of isolated modifier
Adjust isolation checking to handle misused `isolated` attribute
and let attribute checker property diagnose it.

Resolves: rdar://148076903
Resolves: https://github.com/swiftlang/swift/issues/80363
2025-06-27 23:45:53 -07:00
Pavel Yaskevich
c1e0e6622d [TypeChecker] Implement a per-module block list for disjunction optimizer
Allow enabling performance hacks via a block list action - `ShouldUseTypeCheckerPerfHacks`
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
3efb948ad8 [ConstraintSystem] Add a option to re-enable performance hacks
Instead of checking `EnableConstraintSolverPerformanceHacks`
directly, let's use an option which is easier to set i.e.
when a block list is implemented.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
eee40b4ecc [CSGen] Make collection subscript result type inference more principled
Infer result type of a subscript with Array or Dictionary base type
if argument type matches the key type exactly or it's a supported
literal type.

This helps to maintain the existing behavior without having to resort
to "favored type" computation.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
979e0469e0 [CSOptimizer] Rework inferTypeOfArithmeticOperatorChain
- Expand the inference to include prefix and postfix unary operators
- Recognize previously resolved declaration and member references
  in argument positions and record their types.
- Expand reconciliation logic from Double<->Int to include other
  floating-point types and `CGFloat`.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
ef2be0d3f5 [CSOptimizer] Rank operators with the same score based on speculative status
If the scores are the same and both disjunctions are operators
they could be ranked purely based on whether the candidates
were speculative or not. The one with more context always wins.

Consider the following situation:

```swift
func test(_: Int) { ... }
func test(_: String) { ... }

test("a" + "b" + "c")
```

In this case we should always prefer ... + "c" over "a" + "b"
because it would fail and prune the other overload if parameter
type (aka contextual type) is `Int`.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
2520d40575 [CSOptimizer] Allow literal inference inside of operator chains
This is helpful in situations when all of the chained operators
have literal arguments because it would make sure that every
operator has the same score if there is no contextual type.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
479e61b020 [CSOptimizer] Adopt to removal of TypeBase::isArrayType() by switching to isArray() 2025-06-27 23:43:12 -07:00
Pavel Yaskevich
8037bbced2 [CSOptimizer] NFC: Adopt to changes in getParameterList() API 2025-06-27 23:43:12 -07:00
Pavel Yaskevich
eb78e27a08 [CSOptimizer] Consider choices marked as @_disfavoredOverload
These choices could be better than some other non-disfavored ones
in certain situations i.e. when `async` overload is disfavored
but appears in async context it's preferrable to a non-async
overload choice.

Note that the code that mimic old hacks still needs to filter on
`@_disfavoredOverload` in few places to maintain source compatibility.
2025-06-27 23:43:12 -07:00
Pavel Yaskevich
ea47e3cc24 [CSOptimizer] NFC: Adopt new Constraint and ConstraintGraph APIs 2025-06-27 23:43:11 -07:00
Pavel Yaskevich
b90fc2b8c6 [CSOptimizer] Don't attempt to walk into literals while analyzing operator chains
Most of them don't have any children but string interpolation does
and that marks whole chain as unsupported.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
db0a9de996 [CSOptimizer] Account for the fact that sometimes all initializer choices are failable
If all of the viable initializer overloads are failable,
the only valid inference choice is an optional candidate type.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
9e97b8e3a1 [CSOptimizer] Skip disfavored choices when they would result in a worse solution 2025-06-27 23:43:11 -07:00
Pavel Yaskevich
e7b351d41d [CSOptimizer] NFC: Use builder pattern to construct DisjunctionInfo 2025-06-27 23:43:11 -07:00
Pavel Yaskevich
125abede66 [CSOptimizer] Detect when candidate comes from string interpolation
Instead of checking both protocols, check one that matches best
depending on where `String` literal candidate came from.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
5aa3859f17 [CSOptimizer] Disable unary argument hack if overload set has requirements or variadic overloads
This matches the behavior of the old hack where favoring choices
were rolled back if `mustConsider` produced `true` which happened
only for protocol requirements and variadic overload choice regardless
of their viability.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
e280569988 [CSOptimizer] Fix scoring while matching against partially resolved parameter types
When matching candidate like `[Int]` against `Array<Element>`
we need to conservatively assume that if the nominals match
the argument is a viable exact match because otherwise it's
possible to skip some of the valid matches when other overload
choice have generic parameters at the same parameter position.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
1dab584f29 [CSOptimizer] Account for speculative scores only when matching operators vs. non-operators
The problem this is trying to solve is eager selection of operators
over unsupported disjunctions, when matching operators let's take
speculative information into account because it helps to make better
choices in this case.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
073b48c43b [CSOptimizer] Restrict unary argument legacy favoring behavior to ApplyExprs
The original hack never applied to subscripts.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
617338f250 [CSOptimizer] Prevent candidate inference from unresolved generic parameters and ternary expressions
We need to have a notion of "complete" binding set before
we can allow inference from generic parameters and ternary,
otherwise we'd make a favoring decision that might not be
correct i.e. `v ?? (<<cond>> ? nil : o)` where `o` is `Int`.
`getBindingsFor` doesn't currently infer transitive bindings
which means that for a ternary we'd only have a single
binding - `Int` which could lead to favoring overload of
`??` and has non-optional parameter on the right-hand side.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
e2fe558956 [CSOptimizer] Introduce a way to preference disjunctions before scores are considered
Some of the disjunctions are not supported by the optimizers but
could still be a better choice than an operator. Using a non-score
based preference mechanism first allows us to make sure that
operator disjunctions are not selected too eagerly in some situations
when i.e. a member (supported or not) could be a better choice.

`isPreferable` currently targets only operators in result builder
contexts but it could be expanded to more uses in the future.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
bcc749fa1a [CSOptimizer] Reset the overall score of operator disjunctions that is based speculation
New ranking + selection algorithm suffered from over-eagerly selecting
operator disjunctions vs. unsupported non-operator ones even if the
ranking was based purely on literal candidates.

This change introduces a notion of a speculative candidate - one which
has a type inferred from a literal or an initializer call that has
failable overloads and/or implicit conversions (i.e. Double/CGFloat).

`determineBestChoicesInContext` would reset the score of an operator
disjunction which was computed based on speculative candidates alone
but would preserve favoring information. This way selection algorithm
would not be skewed towards operators and at the same time if there
is no no choice by to select one we'd still have favoring information
available which is important for operator chains that consist purely
of literals.
2025-06-27 23:43:11 -07:00
Pavel Yaskevich
df4ae0a546 [CSOptimizer] Update candidate selection to use arithmetic operator chain inference 2025-06-27 23:43:11 -07:00
Pavel Yaskevich
4eec3f6788 [CSOptimizer] Introduce a drop of inference to preserved unary argument behavior
Thanks to `LinkedExprAnalyzer` unary argument hack was able to
infer matching based on literals and arithmetic operator chains,
let's preserve that behavior in a more principled manner.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
0525818a33 [CSOptimizer] Allow matching candidates with optional types against generic paameter types
For example passing `Int?` to `T` should be considered a match
if `T` doesn't have any requirements that block it.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
ac24a8e251 [CSOptimizer] Introduce _OptionalNilComparisonType as candidate for nil while ranking == and != operators
`==` and `!=` operators have special overloads that allow matching
`nil` literal on either side even if wrapped type on the other side
doesn't conform to `Equatable`.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
aedc4fe25e [CSOptimizer] Remove selectBestBindingDisjunction hack 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
b936900eb8 [CSOptimizer] Support non-operator generic choices with simple signatures
If there are no-same type requirements and parameters use
either concrete types or generic parameter types directly,
the optimizer should be able to handle ranking. Currently
candidate arguments are considered in isolation which makes
it impossible to deal with same-type requirements and
complex generic signatures.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
2646efac98 [CSOptimizer] Expand literal support to bools, strings and dictionaries
Optimizer now covers all of the most common ExpressibleBy*Literal
protocols.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
0e72686a2c [CSSolver] Attempt choices marked as disfavored at the end of partition
Since such choices are all but guaranteed to be worst than any
other non-disfavored choice, let's attempt them last to avoid
having to form a complete solution just to filter it out during
ranking.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
0e0b5f9a80 [CSOptimizer] Always prefer a disjunction with a single active choice
Disjunctions with a single element are sometimes introduced after
disfavoring, so we need to make sure that they are always preferred
during disjunction selection.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
b96139eb5b [CSOptimizer] Emulate old hack for unary argument matching more precisely
Having it be part of the other matching wasn't a good idea because
previous "favoring" happened only in a few situations - if argument
was a declaration reference, application or (dynamic) subscript that
had overload choice selected during constraint generation.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
666aa24224 [CSOptimizer] Add support for ?? operator 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
84d034c388 [CSOptimizer] Don't reduce the ranking for non-default literal types
Since each candidate and overload choice are considered independenty
there is no way to judge whether non-default literal type is going
to result in a worse solution than non-default one.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
0dfb2044ef [CSOptimizer] Infer types from init calls used as arguments
This used to be limited to Double/CGFloat and operator arguments
but it's safe to do in general.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
899b2bc1e9 [CSOptimizer] Allow matching against CGFloat as a contextual result type 2025-06-27 23:43:10 -07:00
Pavel Yaskevich
6c28cdfb78 [CSOptimizer] Infer argument candidates through optionals
For example, `??` operator could produce an optional type
so `test(<<something>> ?? 0) could result in an optional
argument that wraps a type variable. It should be possible
to infer bindings from underlying type variable and restore
optionality.
2025-06-27 23:43:10 -07:00
Pavel Yaskevich
2f8a343aef [CSOptimizer] Infer result types through ternary expressions 2025-06-27 23:43:09 -07:00
Pavel Yaskevich
80e12b9dde [CSOptimizer] Improve contextual result type handling during overload matching
Result type should only be matched if there are matches on arguments
or there are no viable candidates.
2025-06-27 23:43:09 -07:00
Pavel Yaskevich
0a5bd787d0 [ConstraintSystem] Narrowly disable tryOptimizeGenericDisjunction when some of the arguments are number literals
Don't attempt this optimization if call has number literals.
This is intended to narrowly fix situations like:

```swift
func test<T: FloatingPoint>(_: T) { ... }
func test<T: Numeric>(_: T) { ... }

test(42)
```

The call should use `<T: Numeric>` overload even though the
`<T: FloatingPoint>` is a more specialized version because
selecting `<T: Numeric>` doesn't introduce non-default literal
types.

(cherry picked from commit 8d5cb112ef)
2025-06-27 23:43:09 -07:00
Pavel Yaskevich
c04068db1c [ConstraintSystem] Initial implementation of disjunction optimizer
This brings back CSOptimizer from https://github.com/swiftlang/swift/pull/63585
2025-06-27 23:43:09 -07:00
Pavel Yaskevich
0871358ec4 [TypeChecker] Update async initializer warning downgrade to check the call instead of callee
`calleeFn` now returns the underlying declaration reference looking through
`ConstructorRefCallExpr`, which means the downgrade logic needs to check
whether the call is using initializer reference before making a decision.
2025-06-27 23:43:09 -07:00
Pavel Yaskevich
1b2ba7e059 [CSStep] Modify selectDisjunction to return favored choices directly
This is currently unused because current mechanism set favored choices
directly but it would be utilized by the disjunction optimizer.

(cherry picked from commit e404ed722a)
2025-06-27 23:43:09 -07:00
Pavel Yaskevich
c18c626a0f [ConstraintSystem] Fix getEffectiveOverloadType to recognize when base is opaque result type
Previously only nominal type and existential where allowed but
that's outdated, it's possible to reference a member of an opaque
result type as well, the concrete type in this case is going to
be deduced by the solver.
2025-06-27 23:43:09 -07:00