Commit Graph

244 Commits

Author SHA1 Message Date
Allan Shortlidge
5dc654c73b AST: Move UnmetAvailabilityRequirement to Availability.h.
This type will need to be used outside of Sema.
2024-11-03 11:37:01 -08:00
Allan Shortlidge
0e4c881eb3 AST: Store the IsSPI bit of AvailableAttr inline in DecAttribute.
Reduces the layout requirements for `AvailableAttr` by one byte.
2024-10-30 14:29:45 -07:00
Allan Shortlidge
4c6b8a1d97 AST: Introduce AvailabilityInference::availableRangeAndAttr().
Also, clean up some documentation.
2024-10-24 20:06:35 -07:00
Allan Shortlidge
0730209302 AST: Move AvailabilityContext into its own header and cpp file. 2024-10-21 15:16:08 -07:00
Allan Shortlidge
4cb783fc92 AST: Store unavailability and deprecation status in AvailabilityContext. 2024-10-21 15:16:08 -07:00
Allan Shortlidge
47e41855d0 AST/Sema: Adopt AvailabilityContext in TypeRefinementContext.
Begin using `AvailabilityContext` as the availability representation in
`TypeRefinementContext`, instead of only storing platform introduction
availability ranges.

There should be no functional changes since this just changes the
representation of the existing information stored by `TypeRefinementContext`.
However, in the future `AvailabilityContext` will be expanded to represent
additional availability constraints.
2024-10-17 17:32:05 -07:00
Allan Shortlidge
439c5951ec AST: Introduce AvailabilityContext.
This class is designed to be a compact representation of the active
availability constraints in a specific scope. For now, it only models platform
introduction availability but it will soon be updated to cover additional
availability constraints, like platform unavailability.

In anticipation of needing to reference `AvailabilityContext`s from
`TypeRefinementContext`s and increasing memory requirements for these contexts,
a cache of uniqued instances of `AvailabilityContext` are stored in a
`llvm::FoldingSet` on `ASTContext`.
2024-10-17 11:26:28 -07:00
Allan Shortlidge
7819dd7833 AST/Sema: Remove unnecessary ASTContext parameters from availability APIs.
Many of the methods on `AvailabilityInference` take both a `Decl` and an
`ASTContext`, which is redundant.
2024-10-14 17:46:53 -07:00
Allan Shortlidge
02dbb96b94 AST: Rename AvailabilityContext to AvailabilityRange.
The generality of the `AvailabilityContext` name made it seem like it
encapsulates more than it does. Really it just augments `VersionRange` with
additional set algebra operations that are useful for availability
computations. The `AvailabilityContext` name should be reserved for something
pulls together more than just a single version.
2024-09-13 16:25:18 -07:00
Allan Shortlidge
01215b0e3d AST: Remove UnavailabilityReason.
`UnavailabilityReason` does not live up to its name because it only models
potential unavailability due to an unmet OS version requirement. It does not
model unavailability caused by explicit annotation, language mode requirements,
obsoletion, etc. `AvailabilityContext` already models an OS version
requirement, so use it instead.

NFC.
2024-09-02 16:47:14 -07:00
Allan Shortlidge
8052e3f9dc AST: Remove 'OS' from AvailabilityContext member names.
An `AvailabilityContext` represents an abstract version range in which
something is available. In the future, these version ranges may not necessarily
always correspond to operating system version ranges.

NFC.
2024-09-02 16:47:14 -07:00
Allan Shortlidge
018234fabf AST: Remove IsSPI from AvailabilityContext.
It only existed to make `Decl::isAvailableAsSPI()` convenient to implement.

NFC.
2024-09-02 16:47:14 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Artem Chikin
1f14158a1d Introduce VisionOS Platform
This change introduces a new compilation target platform to the Swift compiler - visionOS.

- Changes to the compiler build infrastrucuture to support building compiler-adjacent artifacts and test suites for the new target.
- Addition of the new platform kind definition.
- Support for the new platform in language constructs such as compile-time availability annotations or runtime OS version queries.
- Utilities to read out Darwin platform SDK info containing platform mapping data.
- Utilities to support re-mapping availability annotations from iOS to visionOS (e.g. 'updateIntroducedPlatformForFallback', 'updateDeprecatedPlatformForFallback', 'updateObsoletedPlatformForFallback').
- Additional tests exercising platform-specific availability handling and availability re-mapping fallback code-path.
- Changes to existing test suite to accomodate the new platform.
2024-04-10 09:38:02 -07:00
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