Commit Graph

468 Commits

Author SHA1 Message Date
Pavel Yaskevich
65599ce1f1 [AST/Sema] Allow Sendable suppression on Objective-C class declarations
Expressed as `__swift_attr__("~Sendable")` this acts like `: ~Sendable`
on Swift type declarations and supersedes `@_nonSendable(_assumed)`.

Resolves: rdar://140928937
2025-10-24 20:36:24 +09:00
Konrad Malawski
d73b907d66 [Distributed] Avoid redundant conformance to DistributedActor in interface files
Forgetting to handle DistributedActor next to Actor strikes again:
Actors are special that their conformance is implicit and needs not be
printed in swift interface files.

an actor's Actor conformance was filtered out, however the same logic
needs to be applied to DistributedActor, as otherwise there can be
redundant conformance errors when validating module interface files.

Resolves rdar://160252579
2025-09-19 09:01:23 +09:00
John Hui
0e4575ea1d [cxx-interop] Do not consider extensions blessed by SWIFT_PRIVATE_FILEID to be retroactive
Swift warns users when they conform an imported type to an imported
protocol, because such retroactive conformances are prone to producing
conflicting conformances in multiple modules.

When a user annotates a class as SWIFT_PRIVATE_FILEID, they are
explicitly conveying that extensions in the blessed file are part of
the class's private implementation (and thus privy to elevated access),
so we should also not treat conformances in that file as retroactive.

This patch fixes that and adds a test case.

rdar://148083096
2025-07-28 21:30:30 -07:00
Anthony Latsis
ad8c52237c Sema: Fix the insertion location for conformances attributes 2025-06-24 14:49:03 +01:00
Slava Pestov
401d297ee2 AST: Fix crash on invalid in isVanishingTupleConformance() 2025-06-17 09:16:26 -04:00
Slava Pestov
52afa15a74 AST: Fix two bugs with SelfProtocolConformance
There were two problems that have been there for years:

- SubstitutionMap::lookupConformance() assumes that every concrete conformance
  in the path has a root normal conformance. But calling getRootNormalConformance()
  on a self conformance would assert.

- SelfProtocolConformance::getAssociatedConformance() was not implemented. While
  self-conforming protocols do not have associated types, they can inherit from
  other protocols, and we model this with an associated conformance requirement
  having a subject type of `Self`.

Both problems were hidden by the fact that ProtocolConformanceRef::subst()
directly handled self-conforming existentials without calling into the
substitution map. But that is the wrong place for other reasons. The refactoring
in a209ff8869 exposed both of the above issues.

Fixes rdar://problem/151162470.
2025-05-14 23:34:53 -04:00
Doug Gregor
9d493db479 [SE-0470] Fix synthesized conformances with default main actor isolation
When defaulting to main-actor isolation, types that have synthesized
conformances (e.g., for Equatable, Hashable, Codable) were getting
nonisolated members by default. That would cause compiler errors
because the conformances themselves defaulted to main-actor isolation
when their types were.

Be careful to only mark these members as 'nonisolated' when it makes
sense, and leave them to get the isolation of their enclosing type
when the conformance might have isolation. This ensures that one can
use synthesis of these protocols along with default main-actor mode.

There is a one-off trick here to force the synthesized CodingKeys to
be nonisolated, because the CodingKey protocol requires Sendable.
We'll separately consider whether to generalize this rule.

More of rdar://150691429.
2025-05-09 14:35:56 -07:00
Slava Pestov
cea72e35a9 AST: Remove ProtocolConformanceRef::getAssociatedType() 2025-04-03 17:35:34 -04:00
Slava Pestov
0616333129 AST: Remove origType parameter from ProtocolConformanceRef::getAssociatedType() 2025-04-03 17:35:33 -04:00
Slava Pestov
a209ff8869 AST: Remove origType parameter from ProtocolConformanceRef::subst() 2025-04-03 17:35:33 -04:00
Doug Gregor
1c5372858b [SE-0470] Ensure that one cannot form an isolated conformance when Self: Sendable
While here, fix some issues around implied isolated conformances (we
could get into an inconsistent state). Also provide an educational
note discussing isolated conformances and the kinds of errors one can
see when they are used from outside of their isolation domain.
2025-04-02 18:19:09 -07:00
Doug Gregor
c7fd48aa67 Rework storage for conformance isolation
Switch over to split caching for the conformance isolation request,
which optimizes for the common case where the conformance is
nonisolated. Also put the explicit global actor TypeExpr* in an
ASTContext side table, so we don't take a pointer's worth of storage
in every conformance.

For that side table, introduce a new "ASTContext::GlobalCache" that's
there only for side tables, so we don't have to go add get/set
operations to ASTContext and recompile the world every time we want to
add a side table like this.

Thanks, Slava!
2025-03-13 17:38:54 -07:00
Doug Gregor
c58e22fccc Simplify APIs for accessing conformance isolation
The NormalProtocolConformance APIs for checking for an explicitly-written
isolation on a conformance were easy to get to, and the real semantic
API was buried in the type checker, leading to some unprincipled
checking. Instead, create a central ProtocolConformance::getIsolation()
to get the (semantic) actor isolation, and let that be the only place
that will access the explicitly-written global actor isolation for a
conformance. Update all call sites appropriately.
2025-03-12 23:18:32 -07:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Allan Shortlidge
2358712870 AST/Sema: Remove DeclAttr::isUnavailable().
Update callers to use `Decl::isUnavailable()` instead.
2024-12-02 07:35:58 -08:00
Slava Pestov
2f28b9bcda AST: Don't forget to substitute opaque types in ProtocolConformance::subst()
We checked the wrong condition when deciding if a conformance should
be substituted or not, so we would leave behind opaque return types
in builtin and inherited conformances. This is now flagged by
SubstitutionMap::verify().
2024-11-15 17:26:35 -05:00
Slava Pestov
4a8b7bd003 AST: Sink SubstFlags::DesugarMemberTypes hack down into ProtocolConformance::getTypeWitness() 2024-09-19 14:18:34 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Slava Pestov
252abb5365 Sema: Key the implied Sendable conformance behavior off of -swift-version instead of -strict-concurrency
Also add some comments to explain what in the world is going on
with the new checks.
2024-07-06 12:05:45 -04:00
Harlan Haskins
c2a87aa3a0 Relax @retroactive check to allow same-package declarations (#73512)
* Relax @retroactive check to allow same-package declarations

* Add package decls to the test too

* Fix tests

* Import test libraries publicly so I can use their conformances in public retroactive extensions
2024-07-02 21:09:24 -06:00
Slava Pestov
bb48d32ec4 Sema: Implied 'Sendable' conformance should be unconditional
A conditional conformance to a protocol does not usually imply
a conformance to the protocol's inherited protocols, because
we have no way to guess what the conditional requirements
should be.

A carveout was added for 'Sendable', so that protocols could
inherit from 'Sendable' retroactively. However, the 'Sendable'
conformance would become conditional, which causes us to
reject a _second_ conditional conformance to such a protocol:

    struct G<T> {}
    protocol P: Sendable {}
    protocol Q: Sendable {}

    extension G: P where T: P {}
    extension G: Q where T: Q {}

To make this work, tweak the code so that an implied conformance
has the same generic signature as the conforming type, that is,
we force it to be unconditional.

Fixes rdar://122754849
Fixes https://github.com/swiftlang/swift/issues/71544
2024-07-02 17:04:41 -04: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
Konrad `ktoso` Malawski
168bc7b454 [Concurrency] Fix how we obtain DA-as-A conformance for cross module
Resolves rdar://127206143
2024-05-28 13:37:21 +09:00
Nate Chandler
ed5c7ef7ae [BitwiseCopyable] Promote to feature.
SE-0426 was accepted.
2024-04-24 15:52:20 -07:00
Doug Gregor
79b78acdf6 Use SuppressibleProtocolSet as InvertibleProtocolSet
Collapse the representations of "suppressible" and "invertible"
protocol sets. Only minor adjustments were required.
2024-03-29 11:31:48 -07:00
Kavon Farvardin
149c052ec5 use new noncopyable types infrastructure
The infrastructure underpinning the new feature NoncopyableGenerics is
mature enough to be used.
2024-03-14 23:10:44 -07:00
Allan Shortlidge
64dc2e9f33 SILGen: Avoid crashing for invalid conformances.
Force resolution of value witnesses and check the conformance for validity
before proceeding to witness table emission in SILGen to avoid crashing because
of unexpected errors in the AST.

Resolves rdar://123027739
2024-03-12 21:57:35 -07:00
Konrad `ktoso` Malawski
143d8f9801 [Distributed] Handle distributed thunk requirements/witnesses (#72151)
Co-authored-by: Pavel Yaskevich <pyaskevich@apple.com>
2024-03-11 02:07:22 -07:00
Konrad `ktoso` Malawski
b7ff16baf7 [Distributed] Only synthesize Codable for DA where the ID is Codable (#72081) 2024-03-07 22:40:00 -08:00
Slava Pestov
5afdb987e2 Merge pull request #72146 from slavapestov/ncgenerics-fixes-9
Non-copyable generics fixes, part 9
2024-03-07 22:14:33 -05:00
Slava Pestov
af8c8f1671 Sema: Remove non-experimental associated type inference 2024-03-07 17:30:17 -05:00
Slava Pestov
41df661160 AST: Use a builtin conformance for unconditional Copyable/Escapable
This generalizes what we were already doing for classes.
2024-03-07 15:07:47 -05:00
Slava Pestov
02c30d1c15 AST: Fix confusion when builtin conformance passed to registerProtocolConformance() 2024-03-07 12:24:33 -05:00
Slava Pestov
aa4a1b2b6e AST: The generic conformance of a specialized conformance is always normal 2024-03-07 12:22:33 -05:00
Slava Pestov
08ba88f71b Sema: Remove deriveConformanceForInvertible() 2024-03-07 12:20:17 -05: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
Pavel Yaskevich
6e0cfb5ac4 [AST] NonCopyableGenerics: Remove a few uses for getMarking(...).getPositive() 2024-02-16 17:55:52 -08:00
Kavon Farvardin
3a45393e17 NCGenerics: break cycle with SuperclassTypeRequest
With NoncopyableGenerics, we get a cycle involving
`SuperclassTypeRequest` with this program:

  public struct RawMarkupHeader {}
  final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> { }

Because we generally don't support the following kind of relationship:

  class Base<T: P>: P {}
  class Derived: Base<Derived> {}

This commit works around the root-cause, which is that Derived's
synthesized conformance to Copyable gets superceded by the inherited one
from Base. Instead of recording conformances in the ConformanceLookup
table at all, create builtin conformances on the fly, since classes
cannot be conditionally Copyable or Escapable.
2024-02-13 09:19:12 -08:00
Kavon Farvardin
1c2dbf4498 NCGenerics: avoid synthesizing Equatable, etc.
While certain kinds of enums synthesize Equatable, Hashable, and
RawRepresentable conformances, we can't yet permit noncopyable types to
synthesize those until we upgrade those protocols themselves to not
require `Copyable` conformers. For example, Equatable needs to be
changed in the stdlib to be `protocol Equatable: ~Copyable`, etc.

I'm deferring upgrade work until a bit later.
2024-02-07 11:05:08 -08:00
Slava Pestov
991a6de207 Merge pull request #71131 from slavapestov/conformance-checker-vs-type-witnesses-untangling
Sema: Decouple type witness resolution from the ConformanceChecker
2024-01-25 20:06:01 -05:00
Slava Pestov
979fc28461 AST: Remove ProtocolConformanceState::CheckingTypeWitnesses 2024-01-24 21:45:34 -05:00
Kavon Farvardin
c7c9d80520 [NCGenerics] fix Combine issue
The solution is to add the unconditional conformances when building the
conformance lookup table for each nominal, instead of waiting until
conformance lookup infers it.

At the moment, because the stdlib isn't being built with Copyable
conformances injected into it, we still end up needing to infer Copyable
conformances for nominals like `Int` from the stdlib when trying to test
the feature, since it loads those nominals, but they lack conformance.

That should go away once we do enable NoncopyableGenerics for the stdlib
by default, and we'll be left with only inferring the conditional
conformance in TypeCheckInvertible.
2024-01-23 22:42:38 -08:00
Kavon Farvardin
b4985f7fde [NFC] replace canBeNoncopyable
First, "can have an absence of Copyable" is a rather confusing notion,
so the query is flipped to "can be Copyable". Next, it's more robust to
ask if a conformance exists for the TypeDecl to answer that question,
rather than trying to replicate what happens within that conformance
lookup.

Also renames `TypeDecl::isEscapable` to match.
2024-01-23 22:42:38 -08:00
Doug Gregor
97ea19d191 Introduce a builtin and API for getting the local actor from a distributed one
When an actual instance of a distributed actor is on the local node, it is
has the capabilities of `Actor`. This isn't expressible directly in the type
system, because not all `DistributedActor`s are `Actor`s, nor is the
opposite true.

Instead, provide an API `DistributedActor.asLocalActor` that can only
be executed when the distributed actor is known to be local (because
this API is not itself `distributed`), and produces an existential
`any Actor` referencing that actor. The resulting existential value
carries with it a special witness table that adapts any type
conforming to the DistributedActor protocol into a type that conforms
to the Actor protocol. It is "as if" one had written something like this:

    extension DistributedActor: Actor { }

which, of course, is not permitted in the language. Nonetheless, we
lovingly craft such a witness table:

* The "type" being extended is represented as an extension context,
rather than as a type context. This hasn't been done before, all Swift
runtimes support it uniformly.

* A special witness is provided in the Distributed library to implement
the `Actor.unownedExecutor` operation. This witness back-deploys to the
Swift version were distributed actors were introduced (5.7). On Swift
5.9 runtimes (and newer), it will use
`DistributedActor.unownedExecutor` to support custom executors.

* The conformance of `Self: DistributedActor` is represented as a
conditional requirement, which gets satisfied by the witness table
that makes the type a `DistributedActor`. This makes the special
witness work.

* The witness table is *not* visible via any of the normal runtime
lookup tables, because doing so would allow any
`DistributedActor`-conforming type to conform to `Actor`, which would
break the safety model.

* The witness table is emitted on demand in any client that needs it.
In back-deployment configurations, there may be several witness tables
for the same concrete distributed actor conforming to `Actor`.
However, this duplication can only be observed under fairly extreme
circumstances (where one is opening the returned existential and
instantiating generic types with the distributed actor type as an
`Actor`, then performing dynamic type equivalence checks), and will
not be present with a new Swift runtime.

All of these tricks together mean that we need no runtime changes, and
`asLocalActor` back-deploys as far as distributed actors, allowing it's
use in `#isolation` and the async for...in loop.
2024-01-22 17:27:31 -08:00
Slava Pestov
18b34e55a9 Sema: Remove obsolete circularity break 2024-01-20 17:43:01 -05:00
Pavel Yaskevich
60badd6de3 [AST] Convert BuiltinProtocolConformance to use bitfield for its kind 2024-01-18 15:00:23 -08:00
Pavel Yaskevich
89b69930fc [AST] Convert NormalProtocolConformance to use bitfields for its aux information (state, flags etc.) 2024-01-18 15:00:12 -08:00
Pavel Yaskevich
e8b7a26eac [AST] Add a flag to indicate that the conformance is @preconcurrency 2024-01-16 11:51:42 -08:00