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
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
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.
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() {}
```
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
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
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
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
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
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.
Using symbolic references instead of a text based mangling avoids the
expensive type descriptor scan when objective c protocols are requested.
rdar://111536582
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
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.
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.
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.
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.
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
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.
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.
Unfortunately, I don't have reduced reproduction for the crash that motivated
this fix, but similar precautions are taken elsewhere when handling
PatternBindingDecls.
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
This ensures SIL lowering will avoid generating code for unavailable global
vars when `-unavailable-decl-optimization=complete` is specfied.
Part of rdar://106674022
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.
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
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
`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