Commit Graph

230 Commits

Author SHA1 Message Date
Allan Shortlidge
feba547411 AST: Adopt Decl::isUnreachableAtRuntime() in Hashable/Equatable derivation.
When deriving `Hashable` and `Equatable` for enums, use
`Decl::isUnreachableAtRuntime()` to determine whether or not to insert
`_diagnoseUnavailableCodeReached()` traps for specific enum elements. This
fixes a bug where inappropriate traps were inserted for enum elements that are
unavailable for app extensions. It also fixes a bug where traps were inserted
when building a zippered library for macOS and enum elements were unavailable
on macOS but not for macCatalyst clients.

Resolves rdar://125371621
2024-04-04 16:18:35 -07:00
Allan Shortlidge
cfb511469a AST: Don't stub unavailable functions when building zippered libraries.
The declarations in a zippered macOS library may be referenced by clients that
build either for macOS or for macCatalyst. It is therefore inappropriate to
stub functions in zippered libraries as "unreachable" when they are only
unavailable on macOS. Making this logic correct is a larger project, so for now
just disable stubbing when there is a target variant.

Resolves rdar://125371621
2024-04-04 16:18:35 -07:00
Allan Shortlidge
c9a737b370 AST: Don't drop inferred available attributes containing just unavailable.
Fixes a regression from https://github.com/apple/swift/pull/71922.

Resolves rdar://124073829
2024-03-05 13:24:31 -08:00
Doug Gregor
ef7185a4a2 Don't infer empty availability attributes
Due to the mapping of iOS platform availability to tvOS platform availability,
we were ending up inferring an availability attribute `@available(tvOS)` for
an associated type, which does not parse properly. Suppress the creation
of inferred availability attributes when they convey no information
(e.g., because they have no introduced/deprecated/obsoleted/etc. in them).

Fixes rdar://123545422.
2024-02-27 11:28:31 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Allan Shortlidge
2967b80e1a AST: Ignore availability attrs for app extension platforms in some queries.
When determining whether a declaration should be considered unavailable at
runtime, ignore `@available` attributes for application extension platforms but
continue searching for other `@available` attributes that might still make the
declaration unavailable. This ensures corner cases like these are handled:

```
// Dubious, but allowed
@available(macOS, unavailable)
@available(macOSApplicationExtension, unavailable)
public func doublyUnavailableOnMacOSFunc() {}

// Expresses an uncommon, but valid constraint
@available(macCatalyst, unavailable)
@available(iOSApplicationExtension, unavailable)
public func confusingDiamondAvailabilityInheritanceFunc() {}
```
2024-02-14 18:31:43 -08:00
Allan Shortlidge
7b50414d58 AST: Don't optimize decls that are unavailable in extensions.
Unavailable decl optimization is meant to optimize declarations that are
unreachable at runtime. A declaration that is marked unavailable in app
extensions (e.g. `@available(macOSApplicationExtension, unavailable)`) may be
reachable by non-extension processes so it cannot be safely optimized.

Resolves rdar://122924862
2024-02-14 17:03:49 -08:00
Allan Shortlidge
4957fb8a7d AST: Disable stubbing optimization for unavailable declarations.
Necessitated by rdar://122924783&122924862&122924482
2024-02-13 20:38:50 -08:00
Alastair Houghton
c226bf3c3c [AST][IRGen] Add -min-runtime-version option and IRGen support code.
Add a `-min-runtime-version` option that can be used to avoid problems
when building on Linux and Windows where because the runtime isn't
part of the OS, availability doesn't solve the problem of trying to
build the compiler against an older runtime.

Also add functions to IRGen to make it easy to test feature
availability using both the runtime version and the existing Darwin
availability support.

rdar://121522431
2024-02-02 16:17:08 +00:00
Alastair Houghton
c2c4f87ccb [AST] Improve availability support.
Use `.def` files to generate code for feature availability and runtime
version to OS version mappings.

rdar://121522431
2024-02-02 16:14:26 +00:00
Doug Gregor
dc85ae3fc6 Choose between AsyncIteratorProtocol's next() and nextElement() based on availability
This allows us to not break backward deployment
2024-01-25 16:04:47 -08:00
Allan Shortlidge
95175fb826 AST: Rework UnavailableDeclOptimizationMode language option.
Instead of providing a default value for `UnavailableDeclOptimizationMode`,
track it with an optional that defaults to `None`. This way the default
behavior can vary contextually while still honoring an explicit option passed
in on the command line.

Partially resolves rdar://121344690
2024-01-24 22:19:32 -08:00
Allan Shortlidge
61111ed4fe AST: Fix macCatalyst availability for synthesized declarations.
The macCatalyst platform availability of a declaration may be inferred from the
iOS platform availability of that declaration in the absence of an explicit
macCatalyst availability attribute. This means that when inheriting macCatalyst
platform availability, the explict iOS availability of an inner ancestor should
take precedence over explicit macCatalyst availability of an outer ancestor.
The algorithm that computes inferred availability attributes for synthesized
declarations was treating macCatalyst and iOS as independent platforms, though,
sometimes yielding inapproparite macCatalyst availability.

Resolves rdar://107766644
2023-12-18 23:49:26 -08:00
eeckstein
295b6accef Merge pull request #62226 from eeckstein/read-only-static-arrays
IRGen: fix and re-enable static read-only arrays
2023-12-13 16:43:58 +01:00
Arnold Schwaighofer
5869aa3d2f Merge pull request #70385 from aschwaighofer/enable_objc_protocol_symbolic_refs
Enable the use of objective-c protocol symbolic references in type metadata encodings
2023-12-13 06:42:51 -08:00
Erik Eckstein
e4ea7491a2 IRGen: fix and re-enable static read-only arrays
Static read-only arrays didn't work when passed to ObjectiveC as NSArray.
The storage class of static read-only arrays doesn't carry information about the Element type.
The new `__SwiftDeferredStaticNSArray` is generic over the element type and doesn't have to rely on the element type information of the array storage.

rdar://94185998
2023-12-13 11:45:01 +01:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Arnold Schwaighofer
eaedad5747 Enable the use of objective-c protocol symbolic references in type metadata encodings
rdar://111536582
2023-12-11 11:31:09 -08:00
Doug Gregor
40e07cf900 [Typed throws] IR generation and runtime support for function type metadata
Extend function type metadata with an entry for the thrown error type,
so that thrown error types are represented at runtime as well. Note
that this required the introduction of "extended" function type
flags into function type metadata, because we would have used the last
bit. Do so, and define one extended flag bit as representing typed
throws.

Add `swift_getExtendedFunctionTypeMetadata` to the runtime to build
function types that have the extended flags and a thrown error type.
Teach IR generation to call this function to form the metadata, when
appropriate.

Introduce all of the runtime mangling/demangling support needed for
thrown error types.
2023-10-29 09:12:32 -07:00
Arnold Schwaighofer
b0424759d7 Add support for objective c protocol symbolic references
Using symbolic references instead of a text based mangling avoids the
expensive type descriptor scan when objective c protocols are requested.

rdar://111536582
2023-10-05 13:11:32 -07:00
Allan Shortlidge
fddf6cebb5 SILGen: Don't stub unavailable raw value initializers for clang enums.
We must be conservative when generating SIL for raw value initializers of clang
enums and not insert an unavailable code reached trap, even if the enum is
technically unavailable. It appears that there is a long-standing loophole that
allows a clang module to typedef an unavailable type in order to make that type
available in Swift even though the underlying type is declared to be
unavailable in Swift. This loophole is load-bearing for some existing Swift
overlays.

Resolves rdar://116378269
2023-10-03 18:05:50 -07:00
Allan Shortlidge
117a5ecdae Merge pull request #67920 from tshortli/unavailable-decl-opt-complete-resilient-enum-switch
SILOptimizer: Remove switch cases matching unavailable enum elements
2023-08-16 09:06:20 -07:00
Alejandro Alonso
44198d16a4 Merge pull request #67611 from Azoy/raw-types-are-cool!
[WIP] Implement dependent layouts for raw types
2023-08-16 06:55:42 -07:00
Allan Shortlidge
455f3dcaee AST: Clang enum elements can't be unavailable during lowering.
There aren't sufficiently strong compiler checks that would prevent unavailable
elements of clang enums from being instantiated at runtime, so don't consider
any clang enum elements as unavailable during lowering. Since there aren't any
symbols associated with clang enum elements, we don't have to worry about
linker failures that might result from leaving references to them in SIL.
2023-08-15 17:13:10 -07:00
Allan Shortlidge
c3af951f38 AST: Get SPI status of PatternBindingDecl from anchoring VarDecl.
For property declarations, the `@_spi` attribute is attached to the VarDecl AST
node, rather than the PatternBindingDecl AST node, so the `isSPI()` query
should take this into account. Failing to do so caused the availability checker
to erroneously require that `@_spi` properties of types in `-library-level api`
libraries have availability annotations.

Resolves rdar://113587321.
2023-08-08 16:06:36 -07:00
Allan Shortlidge
ca8bf981a4 NFC: Hoist queries for unavailable decl optimizations into the AST library.
Moving the query implementation up to the AST library from SIL will allow
conveniences to be written on specific AST element classes. For instance, this
will allow `EnumDecl` to expose a convenience that enumerates element decls
that are available during lowering.

Also, improve naming and documentation for these queries.
2023-08-04 17:39:26 -07:00
Alejandro Alonso
f840191e14 Emit call to old initialization function for older OSes
Add test

Update raw_layout.swift

Update raw_layout.swift
2023-08-03 09:30:37 -07:00
Doug Gregor
37959de29e Establish type refinement contexts for pattern binding decls directly
The type refinement context builder had a bunch of logic to try to
model type refinement contexts for the first variable declaration that
shows up within a pattern binding declaration. Instead, model this
more syntactically by creating a type refinement context for the
pattern binding declaration itself. This both addresses a regression
in the handling of `if #available` within a closure that's part of an
initializer, and fixes a bug in the same area where similar code has
explicit availability annotations.
2023-08-02 15:07:09 -07:00
Konrad `ktoso` Malawski
95920b2570 [Concurrency] Fix init task group with flags availability
resolves rdar://112469076
2023-08-02 12:02:27 +09:00
Mike Ash
2d238bcea5 Merge pull request #67168 from mikeash/ptrauth-struct-context-descriptors
[Runtime][IRGen] Sign type context descriptor pointers.
2023-07-11 17:03:53 -04:00
Mike Ash
fe7e13bba5 [Runtime][IRGen] Sign type context descriptor pointers.
Ensure that context descriptor pointers are signed in the runtime by putting the ptrauth_struct attribute on the types.

We use the new __builtin_ptrauth_struct_key/disc to conditionally apply ptrauth_struct to TrailingObjects based on the signing of the base type, so that pointers to TrailingObjects get signed when used with a context descriptor pointer.

We add new runtime entrypoints that take signed pointers where appropriate, and have the compiler emit calls to the new entrypoints when targeting a sufficiently new OS.

rdar://111480914
2023-07-07 18:10:35 -04:00
Slava Pestov
3f45c17336 AST: Fill in ASTContext::getSwift59Availability() 2023-07-07 13:57:13 -04:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Allan Shortlidge
40648b7617 AST: Handle malformed PatternBindingDecls in parentDeclForInferredAvailability().
Unfortunately, I don't have reduced reproduction for the crash that motivated
this fix, but similar precautions are taken elsewhere when handling
PatternBindingDecls.
2023-05-31 18:23:43 -07:00
Slava Pestov
4318a36be6 Sema: Availability checking for variadic generic types 2023-05-12 22:50:25 -04:00
Allan Shortlidge
93a96e81c2 Sema: Suppress explicit availability diagnostics on non-Darwin targets.
Suggesting the addition of an `@available` attribute on Linux and Windows is
inapprorpriate given that Swift is not ABI stable on those platforms.

Resolves rdar://107387133
2023-05-08 14:53:12 -07:00
Konrad `ktoso` Malawski
10b49bcb85 [Executors] Ensure we treat DA older than 5.9 always as DefaultActor (#64800) 2023-04-04 11:04:53 +09:00
Allan Shortlidge
367dbd069d IRGen: Skip lowering unavailable opaque type descriptors.
Part of rdar://107425181
2023-03-31 09:36:42 -07:00
Allan Shortlidge
6f5b804224 AST: Pattern binding decls inherit their var decl's availability.
This ensures SIL lowering will avoid generating code for unavailable global
vars when `-unavailable-decl-optimization=complete` is specfied.

Part of rdar://106674022
2023-03-27 11:40:54 -07:00
Allan Shortlidge
620462ddf6 Sema: Don't diagnose declarations more available than unavailable container.
It turns out that we must allow declarations with `introduced:` availability
nested inside of other declarations that are `unavailable` in order to
influence weak linking. Stop diagnosing declarations as being more available
than their unavailable containers and revert some changes to availability
inference that were designed to avoid creating these nestings but caused
regressions for declarations marked `@_spi_available.`

Reverts parts of https://github.com/apple/swift/pull/64310,
https://github.com/apple/swift/pull/64015, and
https://github.com/apple/swift/pull/62900.
2023-03-21 13:55:05 -07:00
Allan Shortlidge
4058ad1421 AST: Improved inferred availability accuracy.
Previously, when creating availability attributes for synthesized declarations
we would identify some set of reference declarations that the synthesized
declaration should be as-available-as. The availability attributes of the
reference declarations would then be merged together to create the attributes
for the synthesized declaration. The problem with this approach is that the
reference declarations themselves may implicitly inherit availability from their
enclosing scopes, so the resulting merged attributes could be incomplete. The
fix is to walk though the enclosing scopes of the reference declarations,
merging the availability attributes found at each level.

Additionally, the merging algorithm that produces inferred availability
attributes failed to deal with conflicts between platform specific and platform
agnostic availability. Now the merging algorithm notices when platform agnostic
availability should take precedence and avoids generating conflicting platform
specific attributes.

Resolves rdar://106575142
2023-03-13 18:27:03 -07:00
Mike Ash
a5765abfa9 Merge pull request #61918 from mikeash/future-version-consistency
Standardize "future" OS versions on 99.99.
2023-03-07 12:15:02 -05:00
Allan Shortlidge
9c76e0d1bf AST: Emit correct synthesized availability attributes for unownedExecutor property.
When synthesizing a declaration and inferring its availability, the synthesized attribute should factor in unavailability of the parent declarations. This recently regressed with https://github.com/apple/swift/pull/63361. However, the previous implementation did not produce correct results, either, because the logic for merging availability attributes produced a non-sensical result when both `unavailable` and `introduced:` availability attributes were merged. For example, this was the result for the synthesized `unownedExecutor` property of an actor when the actor was marked unavailable:

```
@available(macOS, unavailable)
actor A {
  // Incorrectly synthesized availability for `unownedExecutor` which results from merging
  // the unavailability of the parent and the availability of the UnownedSerialExecutor type.
  @available(macOS, unavailable, introduced: macOS 10.15)
  @_semantics("defaultActor") nonisolated final var unownedExecutor: UnownedSerialExecutor { get }
}
```

This is fixed by omitting all version components from the synthesized attribute when the overall attribute kind is "unavailable".

Additionally, I discovered that the `concurrency_availability.swift` test case was no longer testing what it intended to test. The conformances to `Actor` for each `actor` in the test were no longer being synthesized and therefore `unownedExecutor` was not being synthesized. That was fixed by importing the `_Concurrency` module directly, which seems to be necessary because of the `-parse-stdlib` flag in the test.

Resolves rdar://106055566
2023-03-02 10:09:30 -08:00
Allan Shortlidge
b53eba02b7 Merge pull request #58545 from cbjeukendrup/fixit_deprec_self_init
Fix #57354: Fix-it for deprecated initializers removes the `.init` part from `self.init`
2023-01-24 09:09:06 -08:00
Allan Shortlidge
3024feb8d1 AST: Consolidate instantiation of AvailabilityContext from AvailabilityAttr.
Related to rdar://104398146
2023-01-18 16:44:07 -08:00
Casper Jeukendrup
7f9885b791 Let generated availability attributes of initializers of subclasses inherit the message and renaming from the initializer of the superclass 2023-01-12 22:35:33 +01:00
Allan Shortlidge
76d1565a9d Sema: Lift restriction that declarations with @_backDeploy must have explicit availability.
Resolves rdar://103880356
2023-01-06 08:18:00 -08:00
Allan Shortlidge
04b37ab163 Sema: Allow AnyColorBox in SwiftUI to derive from a less available base class.
Resolves rdar://101915368
2022-11-30 14:17:27 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00