Commit Graph

119 Commits

Author SHA1 Message Date
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
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Doug Gregor
d1a43617cc [Conformance lookup table] Drop unnecessary LazyResolver parameters.
There is no point in threading LazyResolver parameters through this
data structure; we can recover the resolver in the one place it is 
needed.
2018-08-14 01:05:42 -07:00
Doug Gregor
ba93ed25db [Conformance lookup table] Delete unnecessary call to bindExtension(). 2018-08-13 19:03:35 -07:00
Doug Gregor
064652693b Remove LazyResolver::resolveExtensionForConformanceConstruction().
This is no longer used. NFC
2018-08-13 19:03:35 -07:00
Doug Gregor
cc9ac43f22 [AST] Use declaration-based name lookup in the conformance lookup table.
This avoids a call into the lazy resolver simply to figure out which
protocols a type conforms to.
2018-08-13 19:03:35 -07:00
Doug Gregor
6556926cba [AST] Switch ProtocolDecl::requiresClass() over to decl-based resolution.
Resolving whether a protocol is class-bound only requires declaration-based
resolution (not the full type checker), so switch over to those APIs.
2018-08-06 10:07:16 -07:00