Commit Graph

95 Commits

Author SHA1 Message Date
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
Doug Gregor
971a6e3cd3 [Name lookup] Use decl-based name lookup for the conformance lookup table.
Add API to get all of the nominal types directly referenced from the
inheritance clause of a given declaration. Use that to find the protocols
to enter into the conformance lookup table based on a given declaration,
without going through the type checker [*].

[*] Except for unqualified lookup still needing to use the type checker.
2018-08-06 09:15:57 -07:00
Doug Gregor
3018becde7 [Type checker] Eliminate type checking from early extension binding.
Use ExtensionDecl::getExtendedNominal() to wire up extensions to their
nominal types early in type checking (the bindExtensions()) operation,
rather than going through type validation to do so.
2018-08-03 11:26:49 -07:00
Slava Pestov
00b3ce18ca AST: Fix crash when doing name lookup into class with circular inheritance 2018-07-10 17:01:12 -07:00
Huon Wilson
d21eedb088 Merge pull request #17786 from huonw/crossfile-synthesis-implication
[AST] Choose an implied conformance source next to the type, if possible.
2018-07-10 08:10:11 +10:00
Huon Wilson
80a74b4dfc [AST] Choose an implied conformance source next to the type, if possible.
If a conformance to a protocol is implied by several other
conformances (i.e. protocol P: Equatable {} and protocol Q: Equatable {} and a
type declares conformance to both P and Q), we should choose a source that's in
the same file as the type, if we can, because automatic synthesis of
conformances (for Equatable, Hashable, etc.) only works in that case.

Fixes rdar://problem/41852654.
2018-07-06 18:24:21 +10:00
Slava Pestov
45fb11ce3c AST: Add ExistentialLayout::getSuperclass(), rename superclass to explicitSuperclass
More groundwork for protocols with superclass constraints.
In several places we need to distinguish between existential
types that have a superclass term (MyClass & Proto) and
existential types containing a protocol with a superclass
constraint.

This is similar to how I can write 'AnyObject & Proto', or
write 'Proto1 & Proto2' where Proto1 has an ': AnyObject'
in its inheritance clause.

Note that some of the usages will be revisited later as
I do more refactoring and testing. This is just a first pass.
2018-07-02 22:06:33 -07:00
Doug Gregor
89032e4581 [AST] Check the signature of a nominal type before forming a conformance.
The introduction of the request-evaluator is causing some conformances
to be formed before the nominal type's signature is being checked,
causing a crash while type-checking the standard library. Ensure that this
cannot happen.
2018-06-15 21:38:57 -06:00
Doug Gregor
2b2e143f59 [Request-Evaluator] Introduce a request for getting an "inherited type". 2018-06-14 15:29:57 -07:00
Doug Gregor
ef337bb8ba [Evaluator] Use the request-evaluator for the superclass of a class.
Wire up the request-evaluator with an instance in ASTContext, and
introduce two request kinds: one to retrieve the superclass of a class
declaration, and one to compute the type of an entry in the
inheritance clause.

Teach ClassDecl::getSuperclass() to go through the request-evaluator,
centralizing the logic to compute and extract the superclass
type.

Fixes the crasher from rdar://problem/26498438.
2018-06-14 15:28:36 -07:00
Doug Gregor
2456868db5 [Conformance lookup table] Eliminate an unnecessary use of LazyResolver::resolveDeclSignature() 2018-06-04 17:48:14 -07:00
Slava Pestov
75db43b978 AST: Replace ProtocolType::compareProtocols() with TypeDecl::compare() 2018-04-06 15:41:07 -07:00
Huon Wilson
48a06be73d [AST] Implied conformances store which conformance implied them. 2018-04-04 10:34:33 +10:00
Huon Wilson
d44303d9c8 [AST] Prefer non-conditional implied conformances.
Given

  protocol P1 {}
  protocol P2: P1 {}
  protocol P3: P1 {}

  struct S<T> {}

  extension S: P2 {}
  extension S: P3 where T: P1 {}

Per SE-0143, we need to make sure that we notice that we can infer S: P1
completely unconditionally from S: P2, and ignore the constrained
one. This is a fairly crude approximation, because doing it accurately
would make this extremely recursive. The approximation used is a
conformance non-conditional if the conformance is from the type, or
there's syntactically no 'where' clause, which is a sufficient condition
for being non-conditional but not necessary (an extension *could* have a
completely redundant 'where' clause).

The recursion problem is checking if something is truly conditional
requires knowing the generic signature of the DeclContext, but knowing
this generic signature requires validating the extension/knowing the
type's conformances, which means hitting this table. (This is made worse
by actual recursive use of conformances, as in SR-6569.)
2018-04-04 10:34:33 +10:00
Huon Wilson
a23808f5d2 [AST] Introduce 'resolveExtensionForConformanceConstruction' for lazier conformance searching.
We need to be able to find which conformances need to be
declared/constructed without forcing extensions to be completely
validated. This is important for both SR-6569 and
rdar://problem/36499373. The former due to the source-level recursion,
and the latter because implied conformances weren't always
constructed (but are needed for good diagnostics).

They weren't always constructed because:

1. ConformanceLookupTable's updateLookupTable on an early stage (before
   implied conformances are found) triggers extension
   validation *before* constructing any conformances, but *after*
   updating the stage state
2. extension validation validates the conditional requirements
3. validating the conditional requirements requires setting up generic
   signatures
4. setting up generic signatures forces the types conformances and so
   ends up in updateLookupTable on the same nominal again, skipping over
   the earlier stages that are complete/in progress
5. we expand the conformances that are implied by all the conformances we
   know about... But we don't know any, because we haven't finished the
   first updateLookupTable.

This breaks the loop at step 2: we instead do the minimal work needed to
know what conformances an extension (might) declare, which is connect
the extension to a type, and then resolve the inherited TypeReprs to
Types.
2018-04-04 10:34:33 +10:00
Huon Wilson
d2290b1d33 [AST] Ignore implied conformance candidates much less aggressively.
The old `existingEntry->getProtocol() == protocol` condition was always
true (the only way for `existingEntry` to get into the array it comes
from is if is exactly for `protocol`), which meant this checking loop
always bailed out, and that there was never more than one implied
candidate ever. This probably isn't what was intended, given there's
more code for handling this case directly after this condition, and
`compareConformances` has extensive code for ranking two implied
conformances for the same nominal/protocol pair.
2018-04-04 10:26:52 +10:00
Tony Allevato
c92a08df2d Remove unused NominalDecl args from ConformanceLookupTable 2017-11-28 17:42:35 -08:00
Huon Wilson
5f70f68c0d [AST] Store only interface types in NormalProtocolConformances.
Rather than storing contextual types in the type witnesses and associated
conformances of NormalProtocolConformance, store only interface types.

@huonw did most of the work here, and @DougGregor patched things up to
complete the change.
2017-11-16 11:45:18 -08:00
Doug Gregor
f4135f963d [Conformance lookup table] Prefer synthesized conformances to deserialized ones.
When a conformance can either be synthesized or implied, we tend to prefer
implied. However, if the implied conformance comes from a deserialized
conformance, it will lead to an incomplete conformance and cause a crash.

This is a narrow fix for SR-6105 / rdar://problem/34911378.
2017-10-16 15:35:11 -07:00
Doug Gregor
a92d8d5760 [Cleanup] Reduce dependencies on swift/AST/LazyResolver.h. 2017-10-10 10:01:39 -07:00
Slava Pestov
05d81081e1 AST: Remove implicit conversion from Witness to ConcreteDeclRef 2017-10-06 22:44:49 -07:00
Slava Pestov
472ea7f1a0 AST: Propagate the LazyConformanceLoader from SynthesizedProtocolAttr to the conformance 2017-09-12 16:36:54 -07:00
Slava Pestov
c3f2f2b567 AST: Small cleanups 2017-09-11 21:48:01 -07:00
Slava Pestov
9f8760b942 AST: Remove unused 'resolver' parameter from ModuleDecl::lookupConformance()
... as well as a bunch of downstream plumbing that is no
longer necessary.
2017-09-07 03:36:17 -07:00
Slava Pestov
3e2acb8ab0 Parse: Allow protocol compositions in all inheritance clauses
We allowed them for generic parameter inheritance clauses but
not anywhere else. While arguably this has stylistic benefits,
the restriction was not enforced consistently and was mostly a
result of implementation limitations.

Lift the restriction and fix things up where needed to make them
work. This brings us closer to allowing protocols to constrain
the 'Self' type to a subclass of a class by listing the class in
the protocol's inheritance clause, which was a feature from SE-0156,
but this doesn't quite work.

Fixes <https://bugs.swift.org/browse/SR-4678> and
<rdar://problem/31785092>.
2017-09-05 21:53:52 -07:00