Commit Graph

461 Commits

Author SHA1 Message Date
Slava Pestov
e1cea881ba AST: Rip out "defaulted definitions" from NormalConformance, NFC
It appears we were only using this to see if an associated type was
derived or defaulted. This code didn't mesh well with the other stuff
I was doing for default implementations, so I'd rather rip it out and
just rely on calling 'isImplicit' to check for derived associated
types instead.

Note that there's a small change of behavior -- if an associated type
is derived for one conformance, and then used as a witness in another,
we were previously only marking it as defaulted in the first one,
but now it is marked as defaulted in both. I do not believe this has
any meaningful consequences.
2016-03-17 03:57:23 -07:00
Doug Gregor
86b7322e87 [Sema -> AST] Refactor "is representable in Objective-C?" checking.
Migrate the check for whether a given type is representable in
Objective-C, which is currently used to verify when @objc can be
inferred or verify that an explicitly-written @objc is well-formed,
from Sema into a set of queries on the Type within the AST library, so
it can be used in other parts of the compiler.

As part of this refactoring, clean up and improve a number of aspects
of this code:

* Unify the "trivially representable" and "representable" code paths
  into a single code path that covers these cases. Clarify the
  different levels of "representable" we have in both the code and
  in comments.

* Distinguish between representation in C vs. representation in
  Objective-C. While we aren't using this now, I'm anticipating it
  being useful to allow exporting C interfaces via @_cdecl (or
  similar).

* Eliminate the special cases for bridging String/Array/Dictionary/Set
  with their Foundation counterparts; we now consult
  _ObjectiveCBridgeable conformances exclusively to get this
  information.

* Cache foreign-representation information on the ASTContext in a
  manner that will let us more easily get the right answer across
  different contexts while providing more sharing than the TypeChecker
  version.

Annoyingly, this only seemed to fix a small class of error where we
were permitting Unsafe(Mutable)Pointer<T> to be representable in
Objective-C when T was representable but not trivially representable,
e.g., T=String or T=AnyObject.Type.
2016-03-16 23:53:48 -07:00
Doug Gregor
a31edf53d0 Simplify the interface to Module::lookupConformance.
Rather than returning a weird PointerIntPair, return an
Optional<ProtocolConformanceRef>. NFC
2016-03-15 22:08:24 -07:00
Joe Groff
77dd9b2992 Split exact-subclass and bindable-to-subclass queries.
In many places, we're interested in whether a type with archetypes *might be* a superclass of another type with the right bindings, particularly in the optimizer. Provide a separate Type::isBindableToSuperclassOf method that performs this check. Use it in the devirtualizer to fix rdar://problem/24993618. Using it might unblock other places where the optimizer is conservative, but we can fix those separately.
2016-03-09 11:14:45 -08:00
Joe Groff
a1ef412815 Sema/SILGen: Get property behavior implementations to codegen.
Fix some interface type/context type confusion in the AST synthesis from the previous patch, add a unique private mangling for behavior protocol conformances, and set up SILGen to emit the conformances when property declarations with behaviors are visited. Disable synthesis of the struct memberwise initializer if any instance properties use behaviors; codegen will need to be redesigned here.
2016-02-20 15:01:06 -08:00
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
practicalswift
8efa5f587e [gardening] Remove "-*- C++ -*-" tag from .cpp files
Emacs assumes .h files are C files by default which is why the
tag "-*- C++ -*-" is needed.

.cpp files do not have this problem.
2016-01-23 12:09:32 +01:00
Slava Pestov
8b57337ca6 Merge pull request #910 from jtbandes/invalid-proto
[Sema] Improve handling of invalid protocols during conformance checking
2016-01-09 23:27:41 -08:00
John McCall
5112864dad Remove the archetype from Substitution.
This eliminates some minor overheads, but mostly it eliminates
a lot of conceptual complexity due to the overhead basically
appearing outside of its context.
2016-01-08 15:27:13 -08:00
Jacob Bandes-Storch
8cc8c5cc22 [Sema] Improve handling of invalid protocols during conformance checking 2016-01-08 00:27:24 -08:00
John McCall
2df6880617 Introduce ProtocolConformanceRef. NFC.
The main idea here is that we really, really want to be
able to recover the protocol requirement of a conformance
reference even if it's abstract due to the conforming type
being abstract (e.g. an archetype).  I've made the conversion
from ProtocolConformance* explicit to discourage casual
contamination of the Ref with a null value.

As part of this change, always make conformance arrays in
Substitutions fully parallel to the requirements, as opposed
to occasionally being empty when the conformances are abstract.

As another part of this, I've tried to proactively fix
prospective bugs with partially-concrete conformances, which I
believe can happen with concretely-bound archetypes.

In addition to just giving us stronger invariants, this is
progress towards the removal of the archetype from Substitution.
2016-01-08 00:19:59 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
ken0nek
fcd8fcee91 Convert [Cc]an not -> [Cc]annot 2015-12-23 00:55:48 +09:00
practicalswift
8ab8847684 Fix typos. 2015-12-16 22:09:32 +01:00
Doug Gregor
855d661c94 Factor the conformance lookup table into a separate .h/.cpp. NFC
Swift SVN r31457
2015-08-25 22:49:43 +00:00
Jordan Rose
babdb9e9f9 Delay loading the witnesses of a protocol conformance.
Apart from being general compile-time goodness, this helps break a
circularity issue involving serialization cross-references and the
Clang importer.

The test is being added to validation-tests because it relies on
several levels of non-laziness in the compiler, all of which we'd
like to fix. It's making sure we don't regress here, but it isn't
actually verifying this change in particular.

rdar://problem/22364953

Swift SVN r31455
2015-08-25 21:47:18 +00:00
Doug Gregor
00040112cd Eliminate special handling of deserialized inherited protocols.
Now that we have the inheritance lists hanging around, use them: it
makes the conformance lookup table use the same code whether we're
deserializing the conformances or parsing them.

Swift SVN r31383
2015-08-21 17:51:26 +00:00
Slava Pestov
ab70e9b554 ASTDumper: Fix crash when printing invalid conformances
As far as I can tell the only way to exercise this code path is
with -dump-ast.

Swift SVN r31302
2015-08-18 18:52:48 +00:00
Doug Gregor
40abcd03ea Generalize TypeChecker::resolveInheritanceClause().
It'll soon have to deal with the "inheritance" clauses of abstract
type parameter declarations (generic type parameters and associated
types). NFC at the moment, however.

Swift SVN r31295
2015-08-18 17:57:33 +00:00
Doug Gregor
80ce752197 Provide default _code/_domain for any nominal type conforming to ErrorType.
This allows any type to conform to ErrorType without having to
implement any requirements, so the requirements can remain
underscored. Implements rdar://problem/21867608.

Swift SVN r30342
2015-07-17 23:37:57 +00:00
Doug Gregor
ad544b7d8e Deal with recursion when walking extensions in the conformance lookup table.
In insanely-hard-to-reproduce recursion walks of the extensions of a
nominal type within the conformance lookup table, make sure that we
don't visit an extension twice by checking our state each time. Fixes
rdar://problem/21861932.

Swift SVN r30280
2015-07-16 22:57:22 +00:00
Doug Gregor
c52f920ad4 Fix an over-eager assertion.
Fixes rdar://problem/21382194; the deeper issue with this test case
was fixed recently, and only this assertion remained.

Swift SVN r30196
2015-07-14 21:17:07 +00:00
Doug Gregor
3855bb824b Sort protocol conformances for serialization and SILGen emission.
Determinism++, otherwise NFC

Swift SVN r30169
2015-07-13 22:16:57 +00:00
Doug Gregor
8bd093d36b Eliminate the "ambiguous implied conformance" error.
Instead of requiring the user to disambiguate where an implied
protocol conformance goes---which they really, really don't care
about---just pick an arbitrary-but-deterministic location for the
conformance, which corresponds to the file unit in which the witness
table will be emitted. Fixes rdar://problem/21538899.

Swift SVN r30168
2015-07-13 21:56:40 +00:00
Doug Gregor
161f309975 Improve recursion-breaking when resolving type witnesses.
Specifically track when we're resolving type witnesses for a given
conformance, and refuse to recursively attempt to resolve those type
witnesses. The standard library patch in rdar://problem/21789238
triggers this, and isolating it in a small test case has proven
illusive.

Swift SVN r30160
2015-07-13 20:06:57 +00:00
Doug Gregor
3023a710fc Split TypeBase::isDependentType() into isTypeParameter() and hasTypeParameter().
The isDependentType() query is woefully misunderstood. Some places
seem to want it to mean "a generic type parameter of dependent member
type", which corresponds to what is effectively a type parameter in
the language, while others want it to mean "contains a type parameter
anywhere in the type". Tease out these two meanings in
isTypeParameter() and hasTypeParameter(), respectively, and sort out
the callers.

Swift SVN r29945
2015-07-07 21:20:54 +00:00
Doug Gregor
0c1b625773 Propagate error types out of specialized conformances.
Fixes rdar://problem/21598514.

Swift SVN r29809
2015-06-30 21:50:12 +00:00
Doug Gregor
fea40a4dd2 Rank conformances implied by synthesized conformances as "synthesized".
This means that we prefer conformances implied by explicit
conformances (ones that the user wrote) over conformances implied by
synthesized conformances (those that the compiler would
generate). This resolves the ambiguity causing rdar://problem/21007417.

Swift SVN r28880
2015-05-21 17:14:28 +00:00
Jordan Rose
2ad796627a Revert "Prefer compiler-provided conformances to those required by other protocols."
This may not be the right solution. Even if it is, there are SourceKit tests
that need updating.

This reverts commit r28849 / rdar://problem/21007417.

Swift SVN r28852
2015-05-20 21:32:56 +00:00
Jordan Rose
1ad7651e19 Prefer compiler-provided conformances to those required by other protocols.
Previously, we'd warn on this code:

  enum Suit { case Spades, Hearts, Clubs, Diamonds }
  extension Suit : Comparable {}
  func <(...) {...}

because both Comparable and the synthesized conformance to Hashable imply
a conformance to Equatable. However, that's silly: Suit already has a
synthesized conformance to Equatable associated with the main 'enum'
declaration, not the extension. These compiler-provided conformances are
part of the language and something people rely on, so rank them higher than
conformances implied by conforming to a refined protocol.

rdar://problem/21007417

Swift SVN r28849
2015-05-20 20:48:04 +00:00
Doug Gregor
74050648de Make @objc enums that conform to ErrorType also conform to _ObjectiveCBridgeableErrorType.
This allows @objc enum error types produced in Objective-C (e.g., via
+[NSError errorWithDomain:code:userInfo:]) to be bridged back to their
original enum types in Swift via pattern matching/catch blocks.

This finishes rdar://problem/20577517.

Swift SVN r28803
2015-05-20 00:16:39 +00:00
Joe Pamer
ca6e909808 Use a MapVector for the conformance tables we plan on iterating, instead of DenseMaps. Not having stable ordering of conformances results in non-deterministic type checker behavior, and causes all sorts of other mischief. (This unblocks rdar://problem/20824955, and also seems to address some other fishy things we've been seeing.)
Swift SVN r28374
2015-05-09 22:10:04 +00:00
Doug Gregor
d0282e0d62 Switch the AST printer over to getLocalProtocols().
Swift SVN r27968
2015-04-30 16:13:37 +00:00
Doug Gregor
8a8895de75 Populate the conformance lookup table for a deserialized protocol.
When deserializing a protocol, the conformance lookup table would not
contain entries for the inherited protocols of that protocol. They
were stashed in the "Protocols" array in TypeDecl (which will
eventually go away), but since there are no conformances for a
protocol, the conformance lookup table never got updated.

Nothing important seems to query this now; that will change soon.

Swift SVN r27967
2015-04-30 16:13:35 +00:00
Doug Gregor
f985ba6542 Conformance lookup table: delay parsed extensions when we have no resolver.
If we try to visit a parsed extension (to find its protocol
conformances) when we don't have a resolver, we would get no
information out of the extension and never revisit it. Queue up these
extensions to be processed later, when we do have a resolver.

I’ve only ever seen this happen when building the standard library,
where verifying imported modules after checking a source file can
cause otherwise-unused extensions to get skipped, although they would
be needed later. The following commit depends on this, but this issue
is separable.

Swift SVN r27945
2015-04-29 23:20:53 +00:00
Doug Gregor
02d25888c9 Clean up the interface to Type::subst(), NFC.
Replace the 'ignoreMissing' boolean flag with a new option set type,
SubstOptions, which is easier to extend. It is not an OptionSet<>
because a follow-on commit will introduce a non-trivial option that
will require more storage.

Also eliminate the LazyResolver parameter, which is no longer
needed. Eliminate the silly TypeChecker::substType(), whose only
purpose was to provide the resolver.

Swift SVN r27656
2015-04-23 23:36:18 +00:00
Doug Gregor
4ae7b13820 Stop trying to fully check a conformance when we query its existence.
This eliminates nonsensical recursion when working with protocol
conformances, and makes their checking more lazy. Start to improve
name lookup to deal with protocol witnesses more lazily, generalizing
the solution we had to all protocol conformances and making it more
directed. We're not done here, as the FIXMEs in the code completion
test imply: this replace-the-requirement-with-the-witness behavior
needs to be sunk down into the AST level so it also applies to
unqualified lookup, visible-decl lookup, etc.

Swift SVN r27639
2015-04-23 18:20:57 +00:00
Argyrios Kyrtzidis
707394aea8 [AST] Refactor implementation for ValueDecl::getSatisfiedProtocolRequirements().
Move the map that keeps track of conforming decl -> requirement from ASTContext
to a nominal type's ConformanceLookupTable, and populate it lazily.

This allows getSatisfiedProtocolRequirements() to work with declarations from module files.
Test on the SourceKit side.

Part of rdar://20526240.

Swift SVN r27353
2015-04-16 06:23:56 +00:00
Argyrios Kyrtzidis
5f6d091efc [AST] Keep track of the type decl that the type witness came from, for protocol conformances.
Swift SVN r27352
2015-04-16 06:23:54 +00:00
Doug Gregor
7c4823ec71 Convert PrintAsObjC over to conformance lookup table entrypoints.
Another step toward using the conformance lookup table for
everything. This uncovered a tricky little bug in the conformance
lookup table's filtering logic (when asking for only those
conformances explicitly specified within a particular context) that
would end up dropping non-explicit conformances from the table (rather
than just the result).

Ween a few tests off of -enable-source-import, because they'll break
otherwise.

Swift SVN r27021
2015-04-05 13:44:47 +00:00
Doug Gregor
38cc1fe5c6 Remove LazyResolver arguments from API entry points to the conformance lookup table.
Swift SVN r26838
2015-04-02 00:06:01 +00:00
Joe Groff
ebed0946fa Rename 'isSimpleEnum' to 'hasOnlyCasesWithoutAssociatedValues'.
Eliminate a bit of jargon that never caught on. This condition isn't used pervasively enough to need a short name.

Swift SVN r26781
2015-03-31 22:10:23 +00:00
Doug Gregor
557751ba7e Protocol extensions cannot have inheritance clauses.
Swift SVN r26770
2015-03-31 18:55:58 +00:00
Doug Gregor
75d0c16036 Perform substitution in ProtocolConformance::getInheritedConformance() properly
Fixes the regression in rdar://problem/20301648, which was triggered
by the separation of extension archetypes (r26483).

Swift SVN r26638
2015-03-27 18:30:10 +00:00
Doug Gregor
159ecf7b84 SILPrinter: print the name of protocol conformance using its generic signature.
When we print the name of a protocol conformance, we normally use the generic parameters as written. However, since some requirements may be inferred (particularly by an extension of a generic type), it's better for the SIL printer to use the generic signature, which is fully expanded and will also be correct, e.g., when working with a deserialized conformance.

Swift SVN r26437
2015-03-23 17:51:39 +00:00
Doug Gregor
9c7280c3d9 Use ProtocolConformance::subst() when digging out inherited conformances of specialized conformances.
Just blindly forming a specialized conformance is wrong here; I'm surprised we haven't tripped over this before.

Swift SVN r26419
2015-03-22 12:35:30 +00:00
Doug Gregor
7ee6f7b666 Conformance table: stop expanding deserialized conformances.
We (de-)serialize everything in the table already, so this is just useless work.

Swift SVN r26414
2015-03-22 12:35:24 +00:00
Doug Gregor
813df95fca Register protocol conformances more eagerly with the conformance lookup table during deserialization.
Swift SVN r26413
2015-03-22 12:35:23 +00:00
Doug Gregor
7708c5a65e Start moving away from (Nominal)?TypeDecl::getProtocols().
Instead, use other entry points, particularly those that use the conformance lookup table.

Swift SVN r26412
2015-03-22 12:35:21 +00:00
Doug Gregor
3f6a14ade6 Eliminate NominalTypeDecl::(get|set)Conformances.
We now access the conformances of a nominal type through the
conformance lookup table, so there is no reason to continue storing
conformances directly on the nominal type declaration, which was
error-prone regardless. This mirrors the change to ExtensionDecl from
my previous commit.

Swift SVN r26354
2015-03-20 16:32:25 +00:00