Commit Graph

125 Commits

Author SHA1 Message Date
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
Becca Royal-Gordon
c517b45bb4 Merge pull request #38948 from beccadax/the-copypasta-is-stale
[NFC] Factor out ASTContext `operator new`s
2021-08-20 11:04:56 -07:00
Becca Royal-Gordon
59bb325e4b [NFC] Factor out ASTContext operator news
Many, many, many types in the Swift compiler are intended to only be allocated in the ASTContext. We have previously implemented this by writing several `operator new` and `operator delete` implementations into these types. Factor those out into a new base class instead.
2021-08-19 11:19:52 -07:00
Doug Gregor
5ba80ee35a Work around issue with inherited Sendable conformance lookup.
Implicit synthesis of `Sendable` conformances for global actor-isolated
class types interacts poorly with the conformance lookup table's
attempt at modeling inherited conformances, so a `Sendable` conformance
will get created and inherited, but is then "missing" when we try to
form the actual inherited conformance.

The proper fix for this issue is likely to eliminate the modeling of
inherited conformances within the conformance lookup table, which
introduces a lot of redundance and, apparently, some bugs. For now,
patch over the issue to work around a crash.

Narrowly works around rdar://81700570.
2021-08-17 21:43:21 -07:00
Robert Widmann
808220510e [NFC] Remove Unused Module Parameter to Conformance Lookup
It's been quite a long time since this unused parameter was introduced.
The intent is to produce the module as a root for the search - that is,
computing the set of conformances visible from that module, not the set
of conformances inside of that module. Callers have since been providing
all manner of module-scoped contexts to it.

Let's just get rid of it. When we want to teach protocol conformance
lookup to do this, we can revert this commit as a starting point and try
again.
2021-08-04 14:43:31 -07:00
Doug Gregor
29f5d7a64a [SE-0302] Implement '@unchecked Sendable' syntax.
Parse and provide semantic checking for '@unchecked Sendable', for a
Sendable conformance that doesn't perform additional semantic checks
for correctness.

Part of rdar://78269000.
2021-07-11 12:29:53 -07:00
Slava Pestov
a250688a50 AST: Use availability to disambiguate multiple overlapping conformances
If a conformance is found in an imported module as well as the current module,
and one of the two conformances is conditionally unavailable on the current
deployment target, pick the one that is always available.

Fixes <rdar://problem/78633800>.
2021-06-07 00:45:59 -04:00
Doug Gregor
114f856537 Reimplement IterableDeclContext::getLocalProtocls() using getLocalConformances()
The uncached, rarely-used getLocalProtocols() does not benefit from
having its own distinct implementation. Reimplement it on top of
getLocalConformances() to simplify things and benefit from the
request-evaluator infrastructure.
2021-03-01 22:05:24 -08:00
Slava Pestov
445d747622 AST: Move GenericParamList and friends to GenericParamList.{h,cpp} 2020-09-29 19:51:03 -04:00
Robert Widmann
ec885b027b [NFC] const-qualify Inheritance-Clause-Bearing PointerUnion 2020-07-23 20:45:24 -07:00
Kita, Maksim
ea6a2dc094 SR-11889: Fixed code review issues
1. Updated Located field names with Pascal Case
2. Updated Located constuctor
3. Formatted lines with more than 80 symbols
2019-12-20 17:18:59 +03:00
Kita, Maksim
c1444dea18 SR-11889: Fixed code review issues 2019-12-20 17:18:59 +03:00
Brent Royal-Gordon
17169fc1fe Merge pull request #27950 from brentdax/dumpster-fire
[NFC] Standardize dump() methods in frontend
2019-10-31 20:36:26 -07:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Robert Widmann
aad82fd9c5 [NFC] Remove unnecessary validation noise
Drop some callers to validateDecl and resolveDeclSignature that did not
actually need the interface type.
2019-09-21 09:56:31 -07:00
Robert Widmann
ae60618f3b Remove resolveExtension and validateExtension
Push the relevant work this was doing down into the callers.  Most of them didn't want to validate the entire extension and force its generic parameters, they just wanted to validate the nominal type.

We must eagerly validate the generic signature of extensions, though.  This avoids a class of cycles where non-generic members of an extension with a generic signaure will call through to getGenericSignatureOfContext and force the generic signature anyways.  When this calls through to protocol witness matching, the signature can be recursively computed.
2019-09-20 22:22:49 -07:00
Jordan Rose
8d7f1b7c5d [AST] Separate SourceFile from FileUnit.h
Like the last commit, SourceFile is used a lot by Parse and Sema, but
less so by the ClangImporter and (de)Serialization. Split it out to
cut down on recompilation times when something changes.

This commit does /not/ split the implementation of SourceFile out of
Module.cpp, which is where most of it lives. That might also be a
reasonable change, but the reason I was reluctant to is because a
number of SourceFile members correspond to the entry points in
ModuleDecl. Someone else can pick this up later if they decide it's a
good idea.

No functionality change.
2019-09-17 17:54:41 -07:00
Jordan Rose
853caa66d4 [AST] Split FileUnit and its subclasses out of Module.h
Most of AST, Parse, and Sema deal with FileUnits regularly, but SIL
and IRGen certainly don't. Split FileUnit out into its own header to
cut down on recompilation times when something changes.

No functionality change.
2019-09-17 17:54:41 -07:00
Slava Pestov
4c499fd4ac AST: Stop passing around LazyResolvers in various places 2019-07-06 00:43:22 -04:00
Jordan Rose
c506747a9c [Serialization] Drop inherited conformances on classes (#23347)
These can be recreated if needed in a client library. To do this, I've
added a new ConformanceLookupKind::NonInherited, which can also be
used elsewhere in the project where we're already filtering out
inherited conformances some other way.

Note that this doesn't drop inherited conformances from the entire
serialized interface, just from the list that a class explicitly
declares. They still get referenced sometimes.

rdar://problem/50541451 and possibly others
2019-05-13 13:41:10 -07:00
Slava Pestov
5062a81e3d AST: Start returning SelfProtocolConformances from ModuleDecl::lookupConformance()
Fixes <rdar://problem/49241923>, <https://bugs.swift.org/browse/SR-10015>.
2019-04-16 23:02:50 -04:00
Doug Gregor
a4d464fb94 [Conformance lookup] Don’t form an inherited conformance to an error type.
Fixes SR-8642, a crash-on-invalid.
2018-08-28 09:55:45 -07:00