Commit Graph

461 Commits

Author SHA1 Message Date
Doug Gregor
9bd774fd57 Eliminate ExtensionDecl::(get|set|)Conformances.
Stop storing a conformances array on ExtensionDecls. Instead, always use the conformance lookup table to retrieve conformances (which is lazy and supports multi-file, among other benefits).

As part of this, space-optimize ExtensionDecl's handling of conformance loaders. When one registers a conformance loader, it goes into a DenseMap on ASTContext and gets erased once we've loaded that data, so we get two words worth of space back in each ExtensionDecl.

Swift SVN r26353
2015-03-20 16:32:21 +00:00
Doug Gregor
a5a46752c2 Teach SILGen to use getAllConformances() when emitting witness tables we might need.
Sort the conformances here so we keep the order deterministic.

Swift SVN r26330
2015-03-19 22:10:11 +00:00
Doug Gregor
ad497366bd Eliminate useless scratch buffer from getAllProtocols/getAllConformances. NFC
Swift SVN r26323
2015-03-19 22:10:06 +00:00
Doug Gregor
b75e03b724 Eliminate the annoying scratch buffer from getLocalConformances(). NFC
Swift SVN r26322
2015-03-19 22:10:05 +00:00
Doug Gregor
7d57195508 Centralize the creation/lookup of normal protocol conformances further.
This simplifies and isolates the "deep conformance checking" behavior
of LazyResolver::checkConformance (renamed from
LazyResolver::resolveConformance). We actually don't want to be
triggering this from lookup, because it's exceedingly non-lazy, but
our lazy resolution of witnesses isn't good enough to support that
just yet. NFC

Swift SVN r26319
2015-03-19 22:10:03 +00:00
Doug Gregor
940ba832e5 Use the conformance lookup table to find protocol conformances.
Replace the loop over all known protocols with a query into the
actual conformance lookup table, which more properly deals with
out-of-order conformance queries, inheritance of protocol
conformances, and conformance queries in multi-file situtations.

The SILGen test change is because we're no longer emitting redundant
conformances, while the slight diagnostic regression in
circular-inheritance cases is because we handle circular inheritance
very poorly throughout the compiler.

While not the end, this is a major step toward finishing
rdar://problem/18448811.

Swift SVN r26299
2015-03-19 06:35:28 +00:00
Doug Gregor
dc27688eca Generalize the importer-only RawOptionSet attribute to a SynthesizedProtocol attribute.
This lets us tag imported declarations with arbitrary synthesized
protocols. Use it to handle imported raw option sets as well as the
RawRepresentable conformances of enums that come in as structs.

Swift SVN r26298
2015-03-19 06:35:25 +00:00
Doug Gregor
99ec43ca0d Start building conformances in the conformance lookup table.
We're still not using these generated conformances as our primary
source of conformances, but now we can create them here and it doesn't
break anything.

Swift SVN r26297
2015-03-19 06:35:22 +00:00
Doug Gregor
3e4632e4d6 Reinstate "Centralize the logic for synthesized conformances.""
This reinstates r26115 along with a small fix to the synthesization of
witnesses (an existing bug exposed by this change).

Swift SVN r26146
2015-03-15 04:15:36 +00:00
Erik Eckstein
fe84d94938 Revert "Centralize the logic for synthesized conformances."
It causes some fails in compiler_crashers:

    Swift :: compiler_crashers/0986-swift-unboundgenerictype-get.swift
    Swift :: compiler_crashers/1103-swift-unboundgenerictype-get.swift
    Swift :: compiler_crashers/1223-swift-lexer-leximpl.swift
    Swift :: compiler_crashers/1276-swift-metatypetype-get.swift
    Swift :: compiler_crashers/1287-swift-printingdiagnosticconsumer-handlediagnostic.swift



Swift SVN r26136
2015-03-14 13:00:13 +00:00
Doug Gregor
78892d78c0 Centralize the logic for synthesized conformances.
This is effectively NFC, but we had two implementations of "figure out
the protocols that this type should implicitly conform to". The one in
the conformance table is what will matter going forward.

Swift SVN r26115
2015-03-13 23:37:21 +00:00
Doug Gregor
b752f9b140 Remove the unused NominalTypeDecl::registerSynthesizedConformance().
The conformance lookup table should ask for registration, it should
*know* what the conformances will be based on the form of the AST. NFC

Swift SVN r26114
2015-03-13 23:37:13 +00:00
Doug Gregor
746141dc64 Introduce a __raw_option_set attribute that the Clang importer adds for option sets.
Effectively NFC; this is part of teaching the new conformance registry
about all synthesized conformances.

Swift SVN r26113
2015-03-13 23:37:12 +00:00
Doug Gregor
9271a24a92 Introduce a protocol conformance registry for nominal types.
(Note that this registry isn't fully enabled yet; it's built so that
we can test it, but has not yet taken over the primary task of
managing conformances from the existing system).

The conformance registry tracks all of the protocols to which a
particular nominal type conforms, including those for which
conformance was explicitly specified, implied by other explicit
conformances, inherited from a superclass, or synthesized by the
implementation.

The conformance registry is a lazily-built data structure designed for
multi-file support (which has been a problematic area for protocol
conformances). It allows one to query for the conformances of a type
to a particular protocol, enumerate all protocols to which a type
conforms, and enumerate all of the conformances that are associated
with a particular declaration context (important to eliminate
duplicated witness tables).

The conformance registry diagnoses conflicts and ambiguities among
different conformances of the same type to the same protocol. There
are three common cases where we'll see a diagnostic:

1) Redundant explicit conformance of a type to a protocol:

    protocol P { }
    struct X : P {  }
    extension X : P { } // error: redundant explicit conformance

2) Explicit conformance to a protocol that collides with an inherited
  conformance:

    protocol P { }
    class Super : P { }
    class Sub : Super, P { } // error: redundant explicit conformance

3) Ambiguous placement of an implied conformance:

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

    struct Y { }
    extension Y : P2 { }
    extension Y : P3 { } // error: ambiguous implied conformance to 'P1'

  This happens when two different explicit conformances (here, P2 and
  P3) placed on different declarations (e.g., two extensions, or the
  original definition and other extension) both imply the same
  conformance (P1), and neither of the explicit conformances imply
  each other. We require the user to explicitly specify the ambiguous
  conformance to break the ambiguity and associate the witness table
  with a specific context.

Swift SVN r26067
2015-03-12 21:11:23 +00:00
Doug Gregor
eb572e6a63 Simplify and improve our handling of generic parameters/signatures/interface types in ProtocolConformance.
This ends up being NFC right now because we're not really creating
inherited conformances consistently, so the cases where it might
matter (reshuffling generic parameters as we go up an inheritance
chain) either fail for other reasons or don't change.

Swift SVN r25993
2015-03-11 20:47:33 +00:00
Doug Gregor
bc799c3fbc Remove ProtocolConformance::getSubstitutedGenericParams().
It's unused and wrong. NFC

Swift SVN r25992
2015-03-11 20:42:47 +00:00
Chris Willmore
5b51620d1d Be more tolerant of invalid protocol conformances when applying constraint solution.
* Lift NormalProtocolConformance::hasTypeWitness() to
  ProtocolConformance. This method can be used to determine whether a
  potentially invalid conformance has a particular type witness.

* ProtocolConformance::getWitness() may return nullptr if the witness
  does not exist.

* In TypeChecker::getWitnessType(), don't complain that a builtin
  protocol is broken if it's the conformance that's broken.

* ConformanceChecker should only emit diagnostics from within calls to
  checkConformance().

* TypeChecker::conformsToProtocol() now returns true if the type
  has a declared conformance to the protocol, regardless of whether the
  conformance is valid. Clients must check the validity of the
  ProtocolConformance themselves if necessary.

<rdar://problem/19495341> Can't upcast to parent types of type constraints without forcing

Swift SVN r25326
2015-02-16 22:41:52 +00:00
Doug Gregor
4994d702b5 Clean up ProtocolConformance::getTypeWitnessByName(). NFC
Swift SVN r24876
2015-01-31 06:12:08 +00:00
Joe Groff
bf3c92c743 Sema: Move 'getBridgedToObjC' out of the type checker.
It is useful for other code to be able to query whether a type is bridged sharing the same logic as the type checker. NFC yet.

Swift SVN r24758
2015-01-27 21:20:39 +00:00
Doug Gregor
e98094859c Allow substitution into dependent types to resolve nested types.
Fixes the reduced forms of rdar://problem/19009056 and
rdar://problem/18475138.

Swift SVN r24352
2015-01-10 04:16:20 +00:00
Dmitri Hrybenko
8fdd6aca87 Fix warnings about falling off the end of a function without a return
Swift SVN r22317
2014-09-27 23:34:22 +00:00
Manman Ren
bc004b6b88 [Generic Specializer] handle specializing inherited conformance.
When we have a WitnessMethodInst in the original SILFunction and
self of the WitnessMethodInst will be replaced with the actual type
that has an inherited protocol conformance, we need to upcast the
lookup type of the WitnessMethodInst to make sure the lookup type
and the conformance type matches.

rdar://18068002


Swift SVN r21311
2014-08-20 18:36:56 +00:00
Doug Gregor
0184d9719b Partially revert “Maintain the DeclContext of a NormalProtocolConformance as the type declaration or extension.”
We now have this information during parsing and throw it away during deserialization. This half-baked state works because all non-generic-extension clients only care about the module context.

Swift SVN r20833
2014-07-31 17:30:08 +00:00
Doug Gregor
182e0b0d24 Revert "When determining the generic parameters used in a conformance, query the DeclContext."
This reverts commit r20794.

Swift SVN r20832
2014-07-31 17:30:07 +00:00
Doug Gregor
73d50e45ce Don't permit creating specialized conformances of specialized conformances...
... and stop doing it when we're separating the archetypes of
extensions from the types they extend. I wasn't able to isolate a
useful test case for this; it triggers when building the standard
library with extension archetypes separated.


Swift SVN r20814
2014-07-31 05:56:27 +00:00
Doug Gregor
37b354b693 When determining the generic parameters used in a conformance, query the DeclContext.
Swift SVN r20794
2014-07-31 01:00:31 +00:00
Doug Gregor
bd75425b0e Remove the notion of non-inheritable conformances entirely <rdar://problem/16880016>.
Swift SVN r20627
2014-07-28 16:29:05 +00:00
Doug Gregor
1cc28d4f80 An initializer requirement can only be satisfied by a required class initializer in a non-final class.
This is part of eliminating the notion of non-inheritable
conformances. Fixes <rdar://problem/17408284>.

Swift SVN r20430
2014-07-23 22:16:38 +00:00
Joe Groff
623aba1786 Encapsulate Substitution's state.
Expose Substitution's archetype, replacement, and conformances only through getters so we can actually assert invariants about them. To start, require  replacement types to be materializable in order to catch cases where the type-checker tries to bind type variables to lvalue or inout types, and require the conformance array to match the number of protocol conformances required by the archetype. This exposes some latent bugs in the test suite I've marked as failures for now:

- test/Constraints/overload.swift was quietly suffering from <rdar://problem/17507421>, but we didn't notice because we never tried to codegen it.
- test/SIL/Parser/array_roundtrip.swift doesn't correctly roundtrip substitutions, which I filed as <rdar://problem/17781140>.

Swift SVN r20418
2014-07-23 18:00:38 +00:00
Doug Gregor
f6ff2977f3 Tighten up the semantics of inherited conformances.
'Self' can be used within parameters whenever the corresponding
parameter in a subclass will be contravariant, and in result types
when the method returns dynamic Self. This also applies to subscript
indices. More of <rdar://problem/16996872>.

Swift SVN r18788
2014-06-10 23:13:17 +00:00
Doug Gregor
2af6e0800b An operator requirement whose input type involves Self does not make a a conformance noninheritable.
Addresses <rdar://problem/16996872>. making NSObject's Equatable
conformance inheritable.

Swift SVN r18783
2014-06-10 22:01:00 +00:00
Michael Gottesman
c1bd3560af [devirtualization] Make ProtocolConformance::getGenericParams() more
sane and ad ProtocolConformance::getSubstitutedGenericParams() for
returning the generic params that were substituted into by a specialized
protocol conformance.

I am going to use this to handle inherited specialized inherited
conformance devirtualization.

Swift SVN r16907
2014-04-27 02:47:44 +00:00
Michael Gottesman
e2cde3c7f4 [devirtualization] Properly substitute in types for upcast when handling
a specialized inherited conformance.

Swift SVN r16900
2014-04-27 00:06:46 +00:00
Michael Gottesman
0676121334 Fix what looks like a copy-pasto error in the protocol conformance code.
This code is supposed to check that each subclass of ProtocolConformance
implements the invoked method. The copy-pasto was that when we were
supposed to be checking if SpecializedProtocolConformance overrode a
method, we were instead checking if InheritedProtocolConformance did so.
Luckily when I fixed this it looks like we are implementing all the
appropriate methods. But still a nice catch I feel.

Swift SVN r15273
2014-03-20 18:30:12 +00:00
Ted Kremenek
c87ac8167a Use SWIFT_FALLTHROUGH instead of [[clang::fallthrough]].
Swift SVN r15028
2014-03-14 05:15:05 +00:00
Ted Kremenek
39a368f0ec Add [[clang::fallthrough]] in obvious cases to satisfy -Wimplicit-fallthrough.
Swift SVN r15018
2014-03-14 00:34:25 +00:00
Joe Groff
205d286211 Knock off some bogus Substitution::Archetype uses.
Swift SVN r14967
2014-03-12 20:46:13 +00:00
Joe Groff
a404b0cd49 Rework Substitution::subst not to depend on the meaningless 'Archetype' field of Substitutions.
Pass in the context generic parameters that correspond to the substitution vector in Substitution::subst. When we build the type substitution map, also collect the conformances from the substitutions into a map we can use to fill in those conformances in substituted substitutions where necessary, without relying on the '.Archetype' field.

Swift SVN r14964
2014-03-12 20:29:46 +00:00
Joe Pamer
3684658771 When resolving generic type aliases within protocols, the type aliases themselves may be modeled as substitutions, and not strictly as type variables. Not accounting for this lead to rdar://problem/16228026 and rdar://problem/16228058, wherein the compiler could crash if the open type alias was referenced from within a generic type instantiation.
Swift SVN r14746
2014-03-06 19:47:41 +00:00
Doug Gregor
15be2159a2 Rename "abstract initializers" to "required initializers".
It's better than what we have.

Swift SVN r14620
2014-03-03 23:12:40 +00:00
Jordan Rose
37c1d8cdbd Don't assert when using a type variable as a specialized conformance witness.
Previously, we would try to generate a conformance for the type variable
(to the witness's requirement), fail, and then assert that we should have
succeeded. Now, we just let the type variable pass through with a null
conformance.

<rdar://problem/16078944>

Swift SVN r14617
2014-03-03 22:47:42 +00:00
Adrian Prantl
4b2ca8bd34 Silence warning.
Swift SVN r14422
2014-02-26 22:52:33 +00:00
Doug Gregor
4af20816a6 Make the interface type for an initializer in a protocol return 'Self'.
Swift SVN r14352
2014-02-25 21:54:31 +00:00
Doug Gregor
9c860f1b22 A conformance is non-inheritable if it has a non-abstract initializer witness.
Swift SVN r14323
2014-02-24 23:28:46 +00:00
Doug Gregor
926e3711d0 Only permit inheritance of protocol conformance when it is semantically valid.
A protocol conformance of a class A to a protocol P can be inherited
by a subclass B of A unless
  - A requirement of P refers to Self (not an associated type thereof)
  in its signature, 
    + *except* when Self is the result type of the method in P and the
    corresponding witness for A's conformance to B is a DynamicSelf
    method.

Remove the uses of DynamicSelf from the literal protocols, going back
to Self. The fact that the conformances of NSDictionary, NSArray,
NSString, etc. to the corresponding literal protocols use witnesses
that return DynamicSelf makes NSMutableDictionary, NSMutableArray,
NSMutableString, and other subclasses still conform to the
protocol. We also correctly reject attempts to (for example) create an
NSDecimalNumber from a numeric literal, because NSNumber doesn't
provide a suitable factory method by which any subclass can be literal
convertible.



Swift SVN r14204
2014-02-21 07:48:28 +00:00
Joe Groff
68db63b45d AST: Have GenericFunctionType use GenericSignature.
Change GenericFunctionType to reference a GenericSignature instead of containing its generic parameters and requirements in-line, and clean up some interface type APIs that awkwardly returned ArrayRef pairs to instead return GenericSignatures instead.

Swift SVN r13807
2014-02-12 03:44:28 +00:00
Joe Groff
8cecd9fcf7 AST: ProtocolConformance::getInheritedConformance() should preserve specialization.
When applying getInheritedConformance to a specialized conformance, reapply the specialization to the found inherited conformance so we get a conformance for the same type we put in, making the specializer's job easier when finding conformances to insert into archetype_methods. To expose the problems this fixes, add a check in the SIL verifier that ArchetypeMethodInsts carry a ProtocolConformance that actually matches their lookup type.

Swift SVN r12988
2014-01-27 07:18:43 +00:00
Joe Groff
92f7b5512d AST: Add a "Substitution::subst" API.
Substitute a...um...substitution by remapping its Replacement mapping and recursively remapping any ProtocolConformances it has. Modify the Specializer to use Substitution::subst when specializing the substitutions of ArchetypeMethod and Apply instructions.

Swift SVN r12900
2014-01-24 04:35:49 +00:00
Doug Gregor
e9fdec95fe Put specialized and inherited conformances into an appropriate arena.
This is infrastructure toward allowing us to construct conformances
where there are type variables <rdar://problem/15168483>, which keeps
tripping up library work.

Swift SVN r12899
2014-01-24 04:13:17 +00:00
Joe Groff
0776c4b6b8 SIL: Reorient function type lowering toward interface types.
Lower types for SILDeclRefs from the interface types of their referents, dragging the old type along for the ride so we can still offer the context to clients that haven't been weaned off of it. Make SILFunctionType's interface types and generic signature independent arguments of its  Derive the context types of SILFunctionType from the interface types, instead of the other way around. Do a bunch of annoying inseparable work in the AST and IRGen to accommodate the switchover.

Swift SVN r12536
2014-01-18 19:42:02 +00:00