Commit Graph

558 Commits

Author SHA1 Message Date
Pavel Yaskevich
9e562881fe [TypeChecker] Improve diagnostics for access to actor-isolated values outside of the actor
Replaces generic `expression is 'async' but is not marked with 'await`
diagnostic with a tailed one for cases where there is an access to an
actor-isolated value outside of its actor without `await` keyword.

This makes the diagnostics for async and sync contexts consistent
and actually identifies a problem instead of simply pointing out
the solution.

Resolves: rdar://151720646
2025-05-23 00:20:18 -07:00
Michael Gottesman
3ed4059a60 [sema] Change non-sendable -> non-Sendable in diagnostics.
This matches send non sendable but importantly also makes it clear that we are
talking about something that doesn't conform to the Sendable protocol which is
capitalized.

rdar://151802975
2025-05-22 11:37:58 -07:00
Nate Chandler
041851e799 [CoroutineAccessors] Infer same lifetime deps.
When the feature is enabled, a read2 accessor is generated when a
mutating getter is declared.

rdar://149385088
2025-04-16 16:03:54 -07:00
Pavel Yaskevich
bf2d01e562 Merge pull request #80419 from xedin/revert-pr-79397
Revert "Sema: Allow optional-to-optional CGFloat <-> Double conversion"
2025-04-02 00:10:23 -07:00
Pavel Yaskevich
35077a01d1 Revert "Sema: Allow optional-to-optional CGFloat <-> Double conversion"
This reverts commit f0f5ffcebf.
2025-03-31 13:58:38 -07:00
Pavel Yaskevich
62d19c5d45 [ConstraintSystem] Use findSelectedOverloadFor in isArgumentGenericFunction
Fixes a crash when existing logic cannot properly determine whether
argument is resolved or not.

Instead of ad-hoc code that checks `ResolvedOverloads` directly, let's
use a method that would reach of a callee locator to determine whether
argument is a reference to a generic function or not.

Resolves: rdar://137825558
2025-03-28 00:41:13 -07:00
Pavel Yaskevich
d656f74652 [TypeChecker] Avoid dropping pre-check diagnostics in typeCheckParameterDefault
`typeCheck{Expression, Target}` has a pre-check phase which would
replace some invalid AST nodes (i.e. name references that are not
available in the given declaration context) with `ErrorExpr`s and
emit a diagnostic. Such diagnostics were then dropped by `abort()`
call to a diagnostic transaction. This results in invalid code being
accepted by Sema and forwarded to SILGen.

Resolves: https://github.com/swiftlang/swift/issues/73986
Resolves: rdar://131732245
2025-03-26 14:22:32 -07:00
Pavel Yaskevich
9daf23ca8a Merge pull request #79956 from xedin/reenable-SwiftUI-tests
[Tests] NFC: Re-enable rdar://57201781 and rdar://70880670
2025-03-13 00:20:31 -07:00
Pavel Yaskevich
c83eefc41a [Tests] NFC: Re-enable rdar://57201781 and rdar://70880670
A crash that was causing these tests to fail has been resolved.
2025-03-12 11:50:10 -07:00
Slava Pestov
1a38309661 Add a couple of regression tests 2025-03-11 10:20:58 -04:00
Pavel Yaskevich
d70f14e936 [BuilderTransform] If: Save result of buildBlock into a variable before using in buildEither
Similar to `case` statements, if chain is non-optional, let's
isolate `buildBlock` of every branch into a separate type variable.
2025-02-25 13:09:35 -08:00
Slava Pestov
f0f5ffcebf Sema: Allow optional-to-optional CGFloat <-> Double conversion
After https://github.com/swiftlang/swift/pull/78957, there is no
technical reason to not allow this conversion. This is needed for
an upcoming optimization.
2025-02-14 13:08:57 -05:00
Pavel Yaskevich
725bd910be [ConstraintSystem] Revert new disjunction favoring algorithm (#79128)
* Revert "[CSOptimizer] Look through `OptionalEvaluationExpr`s when dealing with unapplied disjunctions"

This reverts commit 72340f39b8.

* Revert "[CSOptimizer] Don't consider disabled overloads when checking whether disjunction is supported"

This reverts commit 6bc23b5057.

* Revert "[CSOptimizer] Disjunctions with IUO overload choices are unsupported"

This reverts commit 471ee21535.

* Revert "[CSOptimizer] MemberImportVisibility: Don't consider overloads that come from implicit imports"

This reverts commit aa4a2b9071.

* Revert "[CSOptimizer] Don't consider CGFloat widening when explicit initializer is used"

This reverts commit 3cc76eacdd.

* Revert "[CSOptimizer] Literal arguments should cause score reset only for operators"

This reverts commit e3987beffb.

* Revert "[CSOptimizer] NFC: check whether a choice is of operator instead of whole disjunction"

This reverts commit 6c82892c3c.

* Revert "[CSOptimizer/Tests] NFC: Add a perf test-case fixed by improved literal array handling"

This reverts commit cfd34e54c4.

* Revert "[CSOptimizer] Extend candidate/parameter matching to support array literals"

This reverts commit 8a304f88c6.

* Revert "[CSOptimizer] Favor choices that don't require application"

This reverts commit 0737542da8.

* Revert "[CSOptimizer] Disable CGFloat -> Double conversion for unary operators"

This reverts commit bc3a15fbe6.

* Revert "[CSOptimizer] Mark bitwise operators as supported"

This reverts commit 860ae08d1b.

* Revert "[CSOptimizer] Simplify handling of non-applied disjunctions"

This reverts commit 43ca7dfff9.

* Revert "[ConstraintSystem] Fix `getEffectiveOverloadType` handling of `mutating` methods"

This reverts commit c767f7aff7.

* Revert "[CSOptimizer] Reduce overload types before ranking"

This reverts commit 95b47aead6.

* Revert "[CSOptimizer] Implement special prioritization rules for result builder contexts"

This reverts commit 56d6635e46.

* Revert "[CSOptimizer] Allow only widening CGFloat->Double conversions while matching candidate arguments"

This reverts commit bf8ae3bc1b.

* Revert "[CSSimplify] CGFloat-Double: Rank narrowing correctly when result is injected into an optional"

This reverts commit cb876cbd9e.

* Revert "[CSBindings] Prevent `BindingSet::isViable` from dropping viable bindings (v2)"

This reverts commit b7e7493076.

* Revert "[CSOptimizer] Add support for chained members without arguments"

This reverts commit 87cd5f8733.

* Revert "[CSOptimizer] Mark compiler synthesized disjunctions as optimized"

This reverts commit 867e64182f.

* Revert "[CSOptimizer] Make a light-weight generic overload check if some requirements are unsatisfiable"

This reverts commit 15c773b9d7.

* Revert "[CSOptimizer] Fix `selectDisjunction` to use favored choices even if disjunction was not optimized"

This reverts commit c2a55886f0.

* Revert "[CSOptimizer] Limit "old" behavior compatibility to unlabeled unary arguments"

This reverts commit 9fb73143f6.

* Revert "[Tests] NFC: Update a couple of type-checker tests"

This reverts commit ff8663ff16.

* Revert "[Tests] NFC: Move simd related test-case from `slow` to `fast`"

This reverts commit 28396a6dce.

* Revert "[CSGen] NFC: Remove obsolete `ConstraintSystem::{get, set}FavoredType`"

This reverts commit 8bd288447f.

* Revert "[CSOptimizer] Allow literal arguments to match parameters that conform to `ExpressibleBy{Integer, Float}Literal`"

This reverts commit 2fdd4b6c35.

* Revert "[CSOptimizer] Adjust `scoreCandidateMatch` to indicate when match cannot be decided"

This reverts commit 9b62c84a4f.

* Revert "[CSOptimizer] Fix Double<->CGFloat implicit conversion support when arguments are literals"

This reverts commit 6caf1ccbb2.

* Revert "[CSOptimizer] A more comprehensive generic overload checking when candidates are fully resolved"

This reverts commit e30587bda4.

* Revert "[CSOptimizer] Restore old hack behavior which used to favor overloads based on arity matches"

This reverts commit a3a3ec4fe0.

* Revert "[CSOptimizer] Desugar types before checking for equality"

This reverts commit 802f5cd105.

* Revert "[ConstraintSystem] Narrowly disable `tryOptimizeGenericDisjunction` when some of the arguments are number literals"

This reverts commit 8d5cb112ef.

* Revert "[CSOptimizer] Infer argument candidates from calls to `Double` and CGFloat constructors"

This reverts commit f2a6677a6d.

* Revert "[CSOptimizer] Score all of the overload choices matching on literals uniformly"

This reverts commit 59109c2d60.

* Revert "[CSOptimizer] Enable ranking of `Int*`, `Float{80}` and `Double` initializers"

This reverts commit 6fb6d1cf90.

* Revert "[CSOptimizer] Rank disjunctions based on score only if both sides are supported"

This reverts commit 8818d399f9.

* Revert "[CSOptimizer] Rank results of operators regardless of whether anything is known about parameters"

This reverts commit 3996b25fbd.

* Revert "[Tests] NFC: Add more test-cases that were previously solved due to old hacks behavior"

This reverts commit d0ff6c81b8.

* Revert "[CSOptimizer] Average score should reflect number of defaulted parameters"

This reverts commit 23589add74.

* Revert "[Tests] NFC: Adjust a couple of improved tests"

This reverts commit 66981364fe.

* Revert "[CSOptimizer] Don't optimize (implicit) calls with code completion arguments"

This reverts commit 8a918e2369.

* Revert "[CSOptimizer] attempt to rank only standard/simd operators and fully concrete overload sets"

This reverts commit deca9b61c5.

* Revert "[CSOptimizer] Record best scores for each disjunction and use them in `selectDisjunction`"

This reverts commit 3819ddfb40.

* Revert "[CSOptimizer] Let `determineBestChoicesInContext` return the best disjunction if one is available"

This reverts commit cf05405eae.

* Revert "[CSOptimizer] Emulate old behavior related to favoring of unary calls to members"

This reverts commit 527de22bec.

* Revert "[Tests] NFC: Add a test-case for rdar://133340307 which is now fast"

This reverts commit 670127abd6.

* Revert "[CSOptimizer] Prefer homogeneous arithmetic operator overloads when argument(s) or result match"

This reverts commit d69b6a0594.

* Revert "[CSOptimizer] Remove an outdated optimization to compare resolved argument types with all else equal"

This reverts commit 1760bd1f1e.

* Revert "[CSOptimizer] NFC: Switch from llvm::Optional to std::optional post-rebase"

This reverts commit c429f5b9ec.

* Revert "[CSOptimizer] Increase score when type matches opaque type"

This reverts commit 2869dff995.

* Revert "[CSOptimizer] NFC: Switch to llvm::Optional"

This reverts commit 0fc6806922.

* Revert "[CSOptimizer] NFC: Adjust conformance check to use `ConstraintSystem::lookupConformance`"

This reverts commit da65333d41.

* Revert "[CSOptimizer] Treat all type parameters equally"

This reverts commit 957a5f4270.

* Revert "[CSStep] Remove disjunction pruning logic from DisjunctionStep"

This reverts commit 2c44e37948.

* Revert "[CSOptimizer] Relax candidate type requirements from equality to set of no-impact conversions"

This reverts commit 11b897b32f.

* Revert "[CSOptimizer] Use `matchCallArguments` to establish argument-to-parameter relationships"

This reverts commit cb1cb2018d.

* Revert "[CSOptimizer] Don't attempt to optimize calls with code completion token(s) in argument position"

This reverts commit 14e2a16fce.

* Revert "[CSOptimizer] Allow generic operator overloads without associated type parameters"

This reverts commit bc5f70a9a3.

* Revert "[CSOptimizer] Make sure that all parameters without arguments are defaulted"

This reverts commit 7c1c46d4e4.

* Revert "[CSStep] Don't favor choices until the disjunction is picked"

This reverts commit e404ed722a.

* Revert "[CSOptimizer] Keep track of mismatches while evaluating candidates"

This reverts commit a094c3ebb0.

* Revert "[CSOptimizer] Favor SIMD related arithmetic operator choices if argument is SIMD<N> type"

This reverts commit c2f7451c7b.

* Revert "[CSOptimizer] Initial implementation of disjunction choice favoring algorithm"

This reverts commit 672ae3d252.

* Revert "[ConstraintSystem] Add skeleton of constraint optimizer"

This reverts commit b5f08a4009.

* Revert "[CSGen] Remove ConstraintOptimizer and all favoring logic"

This reverts commit 4432c51f57.

* Revert "[ConstraintSystem] Remove `shrink`"

This reverts commit 757ca24e8a.

* [TypeChecker] NFC: Remove resurrected use of `SolverShrinkUnsolvedThreshold`

* [TypeChecker] Bring back `SolverDisableShrink`

* [Tests] NFC: Mark tests affected by solver-perf revert as slow

* [Tests] NFC: Adjust async tests that are affected by performance hacks
2025-02-13 08:06:24 -08:00
Mishal Shah
9f2ce02cfc [Disabled] 6 tests to support new version of Xcode 16.2 and macOS 15.2 https://github.com/swiftlang/swift/issues/79255 2025-02-09 20:01:27 -08:00
Pavel Yaskevich
d245dac784 Merge pull request #79086 from xedin/small-solver-perf-optimizations
[CSOptimizer] A few small performance optimizations
2025-01-31 21:20:50 -08:00
Pavel Yaskevich
72340f39b8 [CSOptimizer] Look through OptionalEvaluationExprs when dealing with unapplied disjunctions
This improves performance for situations like `a?.b + 2` by
giving a high score to `b` which prioritizes it over an operator.
2025-01-31 13:07:29 -08:00
Pavel Yaskevich
aff0f455cf Merge pull request #79026 from xedin/rdar-143474313
[CSBindings] Allow optional subtype inference when closure result is …
2025-01-30 00:37:46 -08:00
Pavel Yaskevich
c922ab5634 [CSBindings] Allow optional subtype inference when closure result is not yet resolved
Similar to `Void?` we need to unwrap `$T?` because it could be later
bound to `Void` from context.

Resolves: rdar://143474313
2025-01-29 13:12:56 -08:00
Pavel Yaskevich
471ee21535 [CSOptimizer] Disjunctions with IUO overload choices are unsupported
They form a follow-up disjunction to determine whether force unwrap
is necessary and `getEffectiveType` cannot handle that.

Resolves: rdar://143799118
2025-01-29 10:36:16 -08:00
Anthony Latsis
35f1370f6e TypeCheckType: Rework IUO diagnostics using behavior limitation
Also stop suggesting a '?' fix-it for casts, where it is not likely to
be helpful because the common intention is either to force the optional
or declare an IUO.
2025-01-21 21:41:00 +00:00
Slava Pestov
cfcc73f164 Sema: Add -solver-disable-splitter flag for debugging
We're not planning on removing the splitter because it is a big win
in some cases, but we want to run it less often since it can also
be a source of overhead. This flag allows us to compare performance
to understand the tradeoffs better.
2025-01-15 22:30:33 -05:00
Pavel Yaskevich
84c7188219 Merge pull request #78526 from xedin/issue-78371
[CSOptimizer] Literal arguments should cause score reset only for operators
2025-01-13 08:59:36 -08:00
Pavel Yaskevich
e3987beffb [CSOptimizer] Literal arguments should cause score reset only for operators
Resetting score to `0.1` is intended to make sure that the
solver picks the outermost disjunction in literal chains like
`1 + 2 + 3 ...` because that would provide context to the
inner choices.

Resolves: https://github.com/swiftlang/swift/issues/78371
Resolves: rdar://142105691
2025-01-09 11:28:24 -08:00
Pavel Yaskevich
8e3943c4c1 Merge pull request #78506 from xedin/disable-swiftui-test-due-to-141262107
[Tests] NFC: Disable a test-case to unblock update to Xcode 16.2
2025-01-09 00:13:25 -08:00
Pavel Yaskevich
8b4a809482 [Tests] NFC: Disable a test-case to unblock update to Xcode 16.2 2025-01-08 16:07:02 -08:00
Pavel Yaskevich
cfd34e54c4 [CSOptimizer/Tests] NFC: Add a perf test-case fixed by improved literal array handling 2024-12-30 10:20:57 -08:00
Pavel Yaskevich
8a304f88c6 [CSOptimizer] Extend candidate/parameter matching to support array literals
Match `[...]` to Array<...> and/or `ExpressibleByArrayLiteral`
conforming types. This is very helpful for expressions like:
`[...] + [...]`.
2024-12-25 12:51:01 -08:00
Pavel Yaskevich
0737542da8 [CSOptimizer] Favor choices that don't require application
When disjunction is not applied, don't only bump its score
but also favor all of the choices that don't require application
because selection algorithm uses that for comparison.

This is important for situation when property is overload with
a method i.e. `Array.count`.
2024-12-24 17:12:36 -08:00
Pavel Yaskevich
443d011feb [Tests] NFC: Restrict SIMD related perf test to macOS to make it more stable 2024-12-20 10:33:17 -08:00
Slava Pestov
8d0d9577f6 Add formerly-slow expressions found by building projects from the Swift package index
These were slow in 6.0 and are now fast(er).
2024-12-19 17:26:16 -05:00
Pavel Yaskevich
19350d9737 Merge pull request #63585 from xedin/solver-perf
[CSSolver] Implementation of disjunction choice favoring algorithm
2024-12-18 09:47:59 -08:00
Pavel Yaskevich
bc3a15fbe6 [CSOptimizer] Disable CGFloat -> Double conversion for unary operators
Some of the unary operators, i.e. prefix `-`, don't have
CGFloat variants and expect generic `FloatingPoint` overload
to match CGFloat type. Let's not attempt `CGFloat` -> `Double`
conversion for unary operators because it always leads
to a worse solutions vs. generic overloads.
2024-12-18 00:22:49 -08:00
Pavel Yaskevich
636525ebd9 Merge pull request #78171 from xedin/rdar-140300022
[TypeChecker/SILGen] Allow `any Sendable` to match `Any` while matching generic arguments
2024-12-17 20:24:34 -08:00
Pavel Yaskevich
40a41c82d9 [CSBindings] Don't attempt to strip optional for closure result types
Let's not perform $T? -> $T for closure result types to avoid having
to re-discover solutions that differ only in location of optional
injection.

The pattern with such type variables is:

```
$T_body <conv/subtype> $T_result <conv/subtype> $T_contextual_result
```

When `$T_contextual_result` is `Optional<$U>`, the optional injection
can either happen from `$T_body` or from `$T_result` (if `return`
expression is non-optional), if we allow  both the solver would
find two solutions that differ only in location of optional
injection.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
56d6635e46 [CSOptimizer] Implement special prioritization rules for result builder contexts
Prioritize `build{Block, Expression, ...}` and any chained
members that are connected to individual builder elements
i.e. `ForEach(...) { ... }.padding(...)`, once `ForEach`
is resolved, `padding` should be prioritized because its
requirements can help prune the solution space before the
body is checked.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
bf8ae3bc1b [CSOptimizer] Allow only widening CGFloat->Double conversions while matching candidate arguments
Allow CGFloat -> Double widening conversions between
candidate argument types and parameter types. This would
make sure that Double is always preferred over CGFloat
when using literals and ranking supported disjunction
choices. Narrowing conversion (Double -> CGFloat) should
be delayed as much as possible.
2024-12-17 11:36:42 -08:00
Pavel Yaskevich
87cd5f8733 [CSOptimizer] Add support for chained members without arguments
If disjunction represents a member reference that has no arguments
applied, let's score that as `1` to indicate that it should be priorized.

This helps in situations like `a.b + 1` where resolving `a.b` member
chain helps to establish context for `+`.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
15c773b9d7 [CSOptimizer] Make a light-weight generic overload check if some requirements are unsatisfiable
If some of the requirements of a generic overload reference other
generic parameters, the optimizer won't be able to satisfy them
because it only has candidates for one (current) parameter. In
cases like that, let's fallback to a light-weight protocol conformance
check instead of skipping an overload choice altogether.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
c2a55886f0 [CSOptimizer] Fix selectDisjunction to use favored choices even if disjunction was not optimized
Some disjunctions e.g. explicit coercions, compositions of restrictions,
and implicit CGFloat initializers have favored choices set independently
from optimization algorithm, `selectDisjunction` should account for
such situations.
2024-12-17 11:36:41 -08:00
Pavel Yaskevich
ff8663ff16 [Tests] NFC: Update a couple of type-checker tests 2024-12-17 11:36:41 -08:00
Pavel Yaskevich
28396a6dce [Tests] NFC: Move simd related test-case from slow to fast 2024-12-17 11:36:41 -08:00
Pavel Yaskevich
3996b25fbd [CSOptimizer] Rank results of operators regardless of whether anything is known about parameters
When operators are chained it's possible that we don't know anything
about parameter(s) but result is known from the context, we should
use that information.
2024-12-17 11:36:40 -08:00
Pavel Yaskevich
670127abd6 [Tests] NFC: Add a test-case for rdar://133340307 which is now fast 2024-12-17 11:36:39 -08:00
Pavel Yaskevich
bc5f70a9a3 [CSOptimizer] Allow generic operator overloads without associated type parameters 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
672ae3d252 [CSOptimizer] Initial implementation of disjunction choice favoring algorithm
This algorithm attempts to ensure that the solver always picks a disjunction
it knows the most about given the previously deduced type information.

For example in chains of operators like: `let _: (Double) -> Void = { 1 * 2 + $0 - 5 }`

The solver is going to start from `2 + $0` because `$0` is known to be `Double` and
then proceed to `1 * ...` and only after that to `... - 5`.

The algorithm is pretty simple:

- Collect "candidate" types for each argument
  - If argument is bound then the set going to be represented by just one type
  - Otherwise:
    - Collect all the possible bindings
    - Add default literal type (if any)

- Collect "candidate" types for result

- For each disjunction in the current scope:
  - Compute a favoring score for each viable* overload choice:
    - Compute score for each parameter:
      - Match parameter flags to argument flags
      - Match parameter types to a set of candidate argument types
        - If it's an exact match
          - Concrete type: score = 1.0
          - Literal default: score = 0.3
        - Highest scored candidate type wins.
      - If none of the candidates match and they are all non-literal
        remove overload choice from consideration.

    - Average the score by dividing it by the number of parameters
      to avoid disfavoring disjunctions with fewer arguments.

    - Match result type to a set of candidates; add 1 to the score
      if one of the candidate types matches exactly.

  - The best choice score becomes a disjunction score

- Compute disjunction scores for all of the disjunctions in scope.

- Pick disjunction with the best overall score and favor choices with
  the best local candidate scores (if some candidates have equal scores).

- Viable overloads include:
  - non-disfavored
  - non-disabled
  - available
  - non-generic (with current exception to SIMD)
2024-12-17 11:36:38 -08:00
Slava Pestov
8eb9e745a6 Merge pull request #78225 from slavapestov/optimized-shifts
Sema: Filter out protocol extension default implementations of operators
2024-12-17 07:44:10 -05:00
Slava Pestov
27812856dc Sema: Extend DisjunctionStep::shouldSkip() hack to cover ~ & | ^ << >> 2024-12-16 18:08:01 -05:00
omochimetaru
eb2d4b62b2 add regression test 2024-12-15 21:11:46 +09:00
Pavel Yaskevich
fb54682753 [CSSimplify] Don't match pack expansion patterns if shapes are not the same
This helps to avoid spurious failures pointing to involved pattern types
because they won't match exactly if shape types are not the same.
2024-12-12 20:55:02 -08:00
Pavel Yaskevich
7701f4e0dd [CSSimplify] Avoid resolving extraneous (trailing) closures
If a (trailing) closure is determined to be an extraneous argument
for one of the overload choices it needs to be marked as hole as
eagerly as possible and prevented from being resolved because
otherwise it's going to be disconnected from the rest of the
constraint system and resolution might not be able to find all of
the referenced variables. This could result either in crashes
or superfluous diagnostics.

Resolves: rdar://141012049
2024-12-06 16:41:49 -08:00