Commit Graph

147 Commits

Author SHA1 Message Date
Doug Gregor
d681f92932 Factor conformance attributes into their own separate structure.
We had an exploded form of conformance attributes (@unchecked,
@preconcurrency, @unsafe) at several different places in the compiler.
Pull these into a single structure so it's easier to manage and extend.

Should have done this a long time ago.
2025-02-25 11:40:38 -08:00
Doug Gregor
37bfa1998e Tighten up checking for uses of unsafe conformances
Check for unsafe conformances for type erasure and opaque type
erasure.

This also uncovered an issue where we were making every conformance of
an unsafe type to an unsafe protocol @unsafe implicitly, even though
that's not really what we want.
2025-01-11 12:43:41 -08:00
Doug Gregor
f14baef7f4 Ensure that we record the @unsafe for conformances introduced via extensions 2025-01-11 12:43:40 -08:00
Doug Gregor
1b7707d2cc Remove the now-unused @safe(unchecked) 2025-01-10 10:39:16 -08:00
Doug Gregor
5b90b8851a Generalize protocol conformance options spelled via attribute and incorporate @unsafe
Protocol conformances have a handful attributes that can apply to them
directly, including @unchecked (for Sendable), @preconcurrency, and
@retroactive. Generalize this into an option set that we carry around,
so it's a bit easier to add them, as well as reworking the
serialization logic to deal with an arbitrary number of such options.

Use this generality to add support for @unsafe conformances, which are
needed when unsafe witnesses are used to conform to safe requirements.
Implement general support for @unsafe conformances, including
producing a single diagnostic per missing @unsafe that provides a
Fix-It and collects together all of the unsafe witnesses as notes.
2024-12-20 23:16:23 -08:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Holly Borla
b1397703a5 [ConformanceLookup] Just kidding, the compiler needs to prefer available
Sendable conformances for source compatibility.

If conformance lookup always prefers the conformance from the defining module,
libraries introducing unavailable Sendable conformances can break source in
clients that declare retroactive Sendable conformances. Instead, still prefer
the available conformance, and always diagnose the client conformance as
redundant (as a warning). Then, when the retroactive conformance is removed,
the errors will surface, so the programmer has to take explicit action to
experience the source break.
2024-07-11 23:03:33 -07:00
Holly Borla
85b66d1dc2 [ConformanceLookup] Always prefer unavailable Sendable conformances from the
defining module, and diagnose redundant Sendable conformances.

We still allow re-stating inherited unchecked Sendable conformances in
subclasses because inherited Sendable conformances are surprising when
they opt out of static checking. Otherwise, warning on redundant Sendable
conformances nudges programmers toward cleaning up unnecessary retroactive
Sendable conformances over time as libraries incrementally add the
conformances directly.
2024-07-11 20:33:24 -07:00
Holly Borla
7356fe8c8a [ConformanceLookup] Don't allow skipping inherited unavailable conformances
in favor of explicit available ones.

The type checker does not support the notion of multiple protocol
conformances; there can only be one conformance, and if that conformance
is unavailable, you cannot specify your own available conformance. This
is important for Sendable checking; if a framework specifies that a type
is explicitly not Sendable with an unavailable Sendable conformance,
clients cannot ignore Sendable violations involving that type. If a
superclass wants to allow subclasses to add a Sendable conformance, it
should not declare an unavailable Sendable conformance.
2024-07-10 11:58:17 -07:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -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
artemcm
d70863501e [Dependency Scanning] Collect and report each module dependency's Link Libraries 2024-06-05 10:59:41 -07:00
Doug Gregor
640042f396 Suggest @preconcurrency on conformances it could help
When diagnosing a case where an actor-isolated witness cannot satisfy
a non-isolated requirement, also suggest that the conformance could be
annotated with `@preconcurrency`.
2024-05-20 22:06:40 -07:00
Doug Gregor
e9220b48d8 Add a Fix-It to the warning about unnecessary @preconcurrency conformance
When we diagnose an unnecessary `@preconcurrency` on a conformance,
also provide a Fix-It to remove the `@preconcurrency`.
2024-05-19 17:03:08 -07:00
Nate Chandler
ba467d2bd2 [Sema] Enable suppression of inferred conformances.
Add the machinery to support suppression of inference of conformance to
protocols that would otherwise be derived automatically.

This commit does not enable any conformances to be suppressed.
2024-04-15 16:46:22 -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
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
6657882de3 AST: Inline ConformanceLookupTable::addInheritedProtocols() 2024-03-07 12:20:17 -05:00
Slava Pestov
dde28e96a2 AST: Don't call getDirectlyInheritedNominalTypeDecls() on a protocol in ConformanceLookupTable 2024-02-24 07:25:59 -05:00
Slava Pestov
7909d83229 AST: Refactor directReferencesForTypeRepr() to track inverses 2024-02-24 07:25:59 -05:00
Pavel Yaskevich
e8b7a26eac [AST] Add a flag to indicate that the conformance is @preconcurrency 2024-01-16 11:51:42 -08:00
Kavon Farvardin
ef5976c3e2 [nfc] remove stray whitespace 2023-10-18 13:52:14 -07:00
Kavon Farvardin
f76360c5b1 [Sema] "Noncopyable" means no Copyable conformance 2023-10-18 13:52:14 -07:00
Allan Shortlidge
eb4a93d5ad AST: Revert inherited type request in ConformanceLookupTable.
One of the request triggers added to `ConformanceLookupTable` in
https://github.com/apple/swift/pull/68384 can cause circular request
evaluation. Revert the request trigger since it doesn't appear to be necessary
for the test cases introduced in that PR.

Resolves rdar://115314044
2023-09-13 20:20:31 -07:00
Allan Shortlidge
435f623caf AST: Resolve types when computing inherited conformances for classes.
Previously, conformances inherited through a base class could be missed in lazy
typechecking mode if types in the inheritance clause were not already resolved.
2023-09-07 13:57:39 -07:00
Doug Gregor
596da3121d [SE-0407] Provide member macros with information about "missing" conformances
Provide member macros with similar information about conformances to
what extension macros receive, allowing member macros to document
which conformances they care about (e.g., Decodable) and then
receiving the list of conformances that aren't already available for
the type in question. For example, a macro such as

    @attached(member, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    macro Codable() = ...

Expanded on a type that is not already Decodable/Encodable would be
provided with Decodable and Encodable (via the new
`missingConformancesTo:` argument to the macro implementation) when
the type itself does not conform to those types.

Member macros still cannot produce conformances, so this is likely to
be used in conjunction with extension macros most of the time. The
extension macro declares the conformance, and can also declare any
members that shouldn't be part of the primary type definition---such
as initializers that shouldn't suppress the memberwise initializer. On
the other hand, the member macro will need to define any members that
must be in the primary definition, such as required initializers,
members that must be overridable by subclasses, and stored properties.

Codable synthesis is an example that benefits from member macros with
conformances, because for classes it wants to introduce a required
initializer for decoding and an overridable encode operation, and
these must be members of the nominal type itself. Specifically, the
`Codable` macro above is likely to have two attached member roles:

    @attached(member, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    @attached(extension, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    macro Codable() = ...

where the "extension" role is responsible for defining the conformance
(always), and the "member" creates the appropriate members for classes
(`init` vs. `required init`).

Tracked by rdar://112532829.
2023-09-07 08:20:46 -07:00
Allan Shortlidge
0dd8f4c492 AST: Introduce abstraction for extension/type decl inheritance clauses.
Wrap the `InheritedEntry` array available on both `ExtensionDecl` and
`TypeDecl` in a new `InheritedTypes` class. This class will provide shared
conveniences for working with inherited type clauses. NFC.
2023-09-06 10:41:57 -07:00
Doug Gregor
b7bfaf3522 [Macros] Fix handling of extension macro conformances and witnesses
Fix two inter-related issues with extension macros that provide
conformances to a protocol, the combined effect of which is that one
cannot meaningfully provide extension macros that implement
conformances to a protocol like Equatable or Hashable that also
supports auto-synthesis.

The first issue involves name lookup of operators provided by macro
expansions. The logic for performing qualified lookup in addition to
unqualified lookup (for operators) did not account for extension
macros in the same manner as it did for member macros, so we would not
find a macro-produced operator (such as operator==) in witness
matching.

The second issue is more fundamental, which is that the conformance
lookup table would create `NormalProtocolConformance` instances for
pre-macro-expansion conformance entries, even though these should
always have been superseded by explicit conformances within the macro
expansion buffers. The end result is that we could end up with two
`NormalProtocolConformance` records for the same conformance. Some
code was taught to ignore the pre-expansion placeholder conformances,
other code was not. Instead, we now refuse to create a
`NormalProtocolConformance` for the pre-expansion entries, and remove
all of the special-case checks for this, so we always using the
superseding explicit conformances produced by the macro expansions (or
error if the macros don't produce them).

Fixes rdar://113994346 / https://github.com/apple/swift/issues/66348
2023-08-16 19:18:36 -07:00
Holly Borla
6c18ffba76 Merge pull request #67882 from hborla/extension-macro-availability
[Macros] Always consider pre-macro-expansion conformances as subsumed by other conformance entry kinds, before considering availability.
2023-08-10 21:59:15 -07:00
Holly Borla
309e3403f6 [Macros] Always consider pre-macro-expansion conformances as subsumed by
other conformance entry kinds, before considering availability.
2023-08-10 18:19:33 -07:00
Slava Pestov
ea098b56b7 AST: Allow ConformanceLookupTable::forEachInStage() to find ExtensionDecls in the Builtin module 2023-08-09 17:42:25 -04:00
Slava Pestov
7f9a71cd15 AST: Rename ASTContext::getConformance() to getNormalConformance() 2023-08-09 17:42:25 -04:00
Holly Borla
0bd898eb12 [Macros] Allow extension macros to suppress conformances that are already
stated in the original source.

If an extension macro can introduce protocol conformances, macro expansion
will check which of those protocols already have a stated conformance in the
original source. The protocols that don't will be passed as arguments to
extension macro expansion, indicating to the macro that it should only add
conformances to those protocols.
2023-06-30 16:01:15 -07:00
Holly Borla
c3e214cbde [Macros] Expand conformance macros as extension macros.
ConformanceMacro now refines ExtensionMacro, so these roles can share
the same expansion request.
2023-06-30 14:25:14 -07:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -07:00
Doug Gregor
906033a30b Expand conformance macros within the conformance lookup table
The conformance lookup table is the central point of truth to
establish which protocols a nominal type conforms to. Ensure that we
expand conformance macros into that table.

Fixes rdar://106886651.
2023-03-21 18:03:44 -07:00
zoecarver
5eb7c7a6cf [cxx-interop] Add ability to specify protocol conformance on C++ side. 2022-11-30 17:26:15 -07:00
Slava Pestov
50aaaa4b11 AST: Assert if attempting to do conformance lookup table things on a protocol
Unlike structs, enums and classes, protocols should not have a
conformance lookup table with normal conformances in it.
2022-08-23 00:03:37 -04:00
Slava Pestov
9d090a5fd3 AST: The conforming type of a normal conformance should be the self, not declared, interface type
Builtin.TheTupleType's self interface type is (T...), and the declared
interface type is the non-substitutable Builtin.TheTupleType.
2022-08-23 00:03:37 -04:00
Slava Pestov
d9a3f2e5ce AST: Stop calling getAllConformances() on protocols 2022-08-23 00:03:36 -04:00
Slava Pestov
bd46bdaaaa AST: Narrow the filtering of unavailable conformances to Sendable only
Remove the allowUnavailable parameter to lookupConformance(), and instead
explicitly check the result for hasUnavailableConformance() in the places
where we used to pass 'false'.

Also, narrow down this check in those places to the Sendable protocol
only, fixing a regression with Hashable conformance synthesis.

Fixes rdar://problem/94460143.
2022-06-14 21:24:08 -04:00
Doug Gregor
f74d6f7389 [Conformance checking] Do not inherit unavailable conformances.
When a class has an unavailable conformance to a protocol, do not
inherit that unavailable conformance, because it can get in the way of
subclasses defining their own (properly-available) conformance.

Fixes rdar://89992569.
2022-05-27 13:09:15 -07:00
Anthony Latsis
50adc64acc AST: Add a 'sorted' option to ConformanceLookupTable::getAllProtocols() 2022-02-16 00:34:46 +03:00
Becca Royal-Gordon
79a7f39631 Permit redeclaration of superclass's Sendable-ness
A recent change to `Sendable` conformance handling resulted in subclasses of global-actor-confined classes being rejected if they explicitly declared a conformance to `Sendable`.

This behavior is technically correct because actor-isolated types are implicitly `Sendable`, but the source compatibility regression was not desirable. We are also considering requiring subclasses to explicitly repeat their superclass's `Sendable` conformance, so it makes sense to allow these redundant conformances in the general case to ease that potential transition.

Fixes rdar://88700507.
2022-02-09 16:02:27 -08:00
Doug Gregor
5023a934fc Tolerate missing Sendable conformances on superclasses.
Fixes the crash in rdar://86653457
2022-01-11 11:11:21 -08:00
Becca Royal-Gordon
0842795eb5 [NFC] Let SynthesizedProtocolAttrs be @unchecked
This is not yet used by anything.
2021-11-19 11:34:01 -08:00
Saleem Abdulrasool
4d44953691 Revert "Support __available__((swift_attr("@Sendable")))" 2021-11-19 07:40:24 -08:00
Becca Royal-Gordon
953ffc3c8d [NFC] Let SynthesizedProtocolAttrs be @unchecked
This is not yet used by anything.
2021-11-12 23:12:38 -08:00