Commit Graph

28352 Commits

Author SHA1 Message Date
Pavel Yaskevich
14e2a16fce [CSOptimizer] Don't attempt to optimize calls with code completion token(s) in argument position 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
bc5f70a9a3 [CSOptimizer] Allow generic operator overloads without associated type parameters 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
7c1c46d4e4 [CSOptimizer] Make sure that all parameters without arguments are defaulted 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
e404ed722a [CSStep] Don't favor choices until the disjunction is picked 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
a094c3ebb0 [CSOptimizer] Keep track of mismatches while evaluating candidates 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
c2f7451c7b [CSOptimizer] Favor SIMD related arithmetic operator choices if argument is SIMD<N> type 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
Pavel Yaskevich
b5f08a4009 [ConstraintSystem] Add skeleton of constraint optimizer 2024-12-17 11:36:38 -08:00
Pavel Yaskevich
4432c51f57 [CSGen] Remove ConstraintOptimizer and all favoring logic 2024-12-17 11:36:37 -08:00
Pavel Yaskevich
757ca24e8a [ConstraintSystem] Remove shrink 2024-12-17 11:36:37 -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
Anthony Latsis
f43166a5d0 Merge pull request #77851 from AnthonyLatsis/day-2 2024-12-17 12:03:09 +00:00
Slava Pestov
27812856dc Sema: Extend DisjunctionStep::shouldSkip() hack to cover ~ & | ^ << >> 2024-12-16 18:08:01 -05:00
Pavel Yaskevich
a2f711cf8e [ConstraintSystem] Make any Sendable -> Any behave the same in all language modes 2024-12-16 13:57:33 -08:00
Gabor Horvath
985c3a44f8 [cxx-interop] Make sure C++ span is imported as @unsafe
The C++ span should be a non-escapable type but is imported as escapable
for backward compatibility reason. This is inherently unsafe, so make
sure std::span is imported as such. In the future, we plan to generate
safe overloads using Swift's Span and that will be the preferred way of
using the API.
2024-12-16 17:11:22 +00:00
Doug Gregor
997dfc6507 Top-level macros and type aliases can get @available attributes and @unsafe 2024-12-14 21:49:56 -08:00
Doug Gregor
a98a653aee Ensure that @safe(unchecked) works within variable initializers 2024-12-14 21:47:43 -08:00
Doug Gregor
836192b585 Correctly handle @safe on accessors 2024-12-14 21:47:43 -08:00
Doug Gregor
bd4a477cbd Suggest @safe(unchecked) when only the body of a declaration uses unsafe constructs
Also clean up some diagnostic text for the @unsafe/@safe(unchecked) suggestions.
2024-12-14 08:40:28 -08:00
Doug Gregor
b2ae07d089 Merge pull request #78177 from DougGregor/safe-unchecked-attr 2024-12-13 20:32:41 -08:00
Konrad `ktoso` Malawski
3ea940e304 Merge pull request #77730 from nickolas-pohilets/mpokhylets/isolated-deinit-version 2024-12-14 11:00:31 +09:00
Pavel Yaskevich
0de9e9cd1d [CSSimplify] Extend any Sendable -> Any conversion support to function argument/result positions in generic argument context 2024-12-13 17:27:06 -08:00
Doug Gregor
618630fd3f Bail out early if we don't have a valid source range 2024-12-13 13:42:05 -08:00
Doug Gregor
f33077a363 Ensure that @safe(unchecked) only applies to bodies (not declarations) 2024-12-13 13:31:21 -08:00
Doug Gregor
e260d65f71 Add @safe(unchecked) to allow unsafe code within a declaration.
Introduce an attribute to allow unsafe code within the annotated
declaration without presenting an unsafe interface to users. This is,
by its nature, and unsafe construct, and is used to document where
unsafe behavior is encapsulated in safe constructs.

There is an optional message that can be used as part of an audit
trail.
2024-12-13 11:36:40 -08:00
Pavel Yaskevich
c8f3524961 [CSDiagnostics/Tests] Add tests for any Sendable vs. Any interaction in non-strict concurrency contexts 2024-12-13 09:35:34 -08:00
Doug Gregor
268d5ccbde Suppress strict safety diagnostics in @unsafe declarations
When a declaration is `@unsafe`, don't emit strict safety diagnostics
for uses of unsafe entities, constructs, or types within it. This
allows one to account for all unsafe behavior in a module using strict
memory safety by marking the appropriate declarations `@unsafe`.

Enhance the strict-safety diagnostics to suggest the addition of
`@unsafe` where it is needed to suppress them, with a Fix-It. Ensure
that all such diagnostics can be suppressed via `@unsafe` so it's
possible to get to the above state.

Also includes a drive-by bug fix where we weren't diagnosing unsafe
methods overriding safe ones in some cases.

Fixes rdar://139467327.
2024-12-12 21:22:41 -08: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
0772e4cbc7 [CSSimplify] Presence of conversion restrictions should delay fixing of generic arguments
Conversion restrictions like "deep equality" could produce a more
narrow/actionable fixes.
2024-12-12 18:12:01 -08:00
Pavel Yaskevich
6f967daf7a [CSSimplify] Increase generic argument mismatch score when matched against marker protocol
This indicates that the type doesn't have appropriate annotations
i.e. for concurrency or ownership.
2024-12-12 14:36:16 -08:00
Pavel Yaskevich
a8146d6086 [CSDiagnostics] Generic argument mismatches should always mention the container types 2024-12-12 14:36:10 -08:00
Mykola Pokhylets
5ac1cba8d1 Handle versioning of the IsolatedDeinit feature 2024-12-12 16:41:02 +09:00
Mykola Pokhylets
bc80529d02 Revert "Merge pull request #77438 from swiftlang/revert-77364-mpokhylets/non-experimental-isolated-deinit"
This reverts commit 11781a5fd1, reversing
changes made to 2ee2f1eb2c.
2024-12-12 16:41:02 +09:00
Pavel Yaskevich
5e08f7e257 [CSApply/SILGen] Implement any Sendable to Any erasure for generic arguments
In non-strict concurrency mode when `@preconcurrency` declarations
are involved `any Sendable` should be treated as `Any` in generic
argument positions to support passing types that (partially) adopted
concurrency annotations to types that haven't yet done so.
2024-12-11 17:19:14 -08:00
Pavel Yaskevich
c6a8cbfa7b [CSSimplify] Allow any Sendable to match Any while matching generic arguments
Allow `any Sendable` to match `Any` constraint while matching
generic arguments i.e. `[any Sendable]` -> `[Any]` when `any Sendable`
type comes from context that involves `@preconcurrency` declarations
in non-strict concurrency compiler mode.

Note that it's currently impossible to figure out precisely
where `any Sendable` type came from.
2024-12-11 11:40:22 -08:00
Pavel Yaskevich
93a8675e49 Merge pull request #76946 from pwongxy/diag-warn-on-for-var-loop
[Diagnostics] Improve warning suggestion for `var` in for loop
2024-12-10 18:01:25 -08:00
Pavel Yaskevich
17093b35d7 [CSSimplify] Delay binding generic argument type variables to any Sendable
Prevent generic arguments from being assigned `any Sendable`
directly, that should only happen through inference. This is
required because we allow `any Sendable` -> `Any` conversion
in modes without strict concurrency enabled to maintain source
compatibility and let the developers annotate existing APIs
with `any Sendable` and other concurrency attributes.
2024-12-10 17:39:24 -08:00
Allan Shortlidge
0dfca46225 Merge pull request #78077 from tshortli/allow-unavailable-on-platform-in-universally-unavailable-contexts
Sema: Relax availability checking in universally unavailable contexts
2024-12-10 10:02:54 -08:00
Peter Wong
c1473e84f9 [Diagnostics] Improve warning suggestion for var in for loop
When iterator consists of tuple of variable and iteration only mutates
the tuple partially, improve the warning message from "changing to 'let"
to "changing the pattern to '(..., case let, ...)"
2024-12-10 22:51:08 +08:00
Richard Howell
6a6cc48c19 Merge pull request #77832 from rmaz/namedpatterndiag
Add access level import diagnostic for named pattern.
2024-12-10 06:29:22 -08:00
Egor Zhdan
c1ff2c3392 Merge pull request #78057 from swiftlang/egorzhdan/relax-cxx-resilience
[cxx-interop] Allow more C++ decls in public Swift interfaces
2024-12-10 13:28:21 +00:00
Allan Shortlidge
eafea5b21e Sema: Relax availability checking in universally unavailable contexts.
Recent refactoring fixed a bug that previously caused `f()` to be checked as if
it were unavailable only on macOS in the following example:

```
@available(macOS, unavailable)
struct Outer {
  @available(*, unavailable)
  func f() {
    someFunctionUnavailableOnMacOS()
  }
}
```

Unfortunately, fixing that bug made a different existing availability checking
rule more problematic. References to declarations that are unavailable on the
current platform have been diagnosed as unavailable even in contexts that are
universally unavailable. This long standing behavior is overly strict but it
rarely had consequences. However, now that the example above is modeled
correctly, this overly strict behavior is causing some source compatibility
issues. The easiest solution is to relax the overly strict checking.

Resolves rdar://141124478.
2024-12-09 17:15:15 -08:00
Pavel Yaskevich
70b07af38b Merge pull request #78035 from xedin/rdar-141012049
[CSSimplify] Avoid resolving extraneous (trailing) closures
2024-12-09 14:30:32 -08:00
Richard Howell
5bcf1bf61f Add access level import diagnostic for named pattern.
This PR adds a missing access level diagnostic when type checking
named patterns by walking the interface type to find the importing
decl to emit the diagnostic for.
2024-12-09 12:30:03 -08:00
Egor Zhdan
e67f2b72d2 [cxx-interop] Allow some C++ fields in public Swift interfaces
This is a follow-up to 8859b629.

This resolves compiler errors when trying to rebuild System.swiftmodule from its textual interface with Xcode 16.1.

rdar://140203932
rdar://141124318
2024-12-09 19:20:20 +00:00
Joe Groff
8436db3b9d Merge pull request #78021 from jckarter/addressable-params-2
Add an `@_addressableSelf` attribute to mark the self param of methods as addressable.
2024-12-09 08:42:49 -08:00
Egor Zhdan
9de29e9cab [cxx-interop] Allow some C++ constructors in public Swift interfaces
This is a follow-up to 8859b629.

This resolves compiler errors when trying to rebuild System.swiftmodule from its textual interface with Xcode 16.1.

rdar://140203932
rdar://141124318
2024-12-09 15:32:05 +00:00
Allan Shortlidge
7cf5d4bca5 AST: Adopt AvailabilityDomain in unavailability diagnostics. 2024-12-08 17:29:04 -08:00
Slava Pestov
d586415a6c Merge pull request #77868 from slavapestov/type-alias-type
AST: Two optimizations to speed up compile time
2024-12-07 16:31:38 -05:00
Egor Zhdan
c041994558 Merge pull request #78022 from swiftlang/egorzhdan/allow-c-like
[cxx-interop] Allow using C-like structs in public Swift interfaces
2024-12-07 18:47:39 +00:00