Commit Graph

197 Commits

Author SHA1 Message Date
Roman Levenstein
0aff7c0c78 Produce specialized conformances to AnyObject for bound generic types.
Fixes rdar://31910351 (SR-4750)
2017-05-01 18:12:52 -07:00
Doug Gregor
6ef76a92f4 [AST] Use flat array storage for SubstitutionMap's replacement types.
The replacement types in a SubstitutionMap correspond with the generic
parameters of its generic signature, so replace the DenseMap storage
with a flat array of Types, one element for each generic parameter.
2017-04-28 16:22:07 -07:00
Doug Gregor
93435d17d6 [Substitution Map] Handle substitutions of generic parameters made concrete.
If SubstitutionMap is asked to form a substitution for a generic
parameter that has been made concrete by the generic signature,
substitute into the concrete type. This allows us to better deal with
non-canonical types.
2017-04-28 16:22:07 -07:00
Slava Pestov
0290c2d5d8 AST: Make GenericSignature and GenericEnvironment SubstitutionMaps interchangable
SubstitutionMap::lookupConformance() would map archetypes out
of context to compute a conformance path. Do the same thing
in SubstitutionMap::lookupSubstitution().

The DenseMap of replacement types in a SubstitutionMap now
always has GenericTypeParamTypes as keys.

This simplifies some code and brings us one step closer to
a more efficient representation of SubstitutionMaps.
2017-04-24 14:12:36 -07:00
Slava Pestov
b4aaaee956 AST: Fix crash in SubstitutionMap::dump() with empty SubstitutionMap
The SubstitutionMap type has a "null value" where there's no
generic signature and no entries. There's nothing to dump in
this case.
2017-04-24 00:52:10 -07:00
Slava Pestov
26aa02324b AST: Fix "concrete conformance under abstract conformance" hack for archetypes
This manifested as test/Prototypes/PatternMatching.swift failing
under 'ninja check-swift-validation-optimize'.

I'll add a test later if I have time to reduce a test case, but this
whole hack is hopefully going away soon anyway.

Fixes one of the three failures outlined in <rdar://problem/31780356>.
2017-04-24 00:52:10 -07:00
Slava Pestov
620db5f74c AST: Narrower workaround for "concrete conformance under abstract conformance" bug
Instead of just falling back to module lookup any time conformance
substitution fails, only do it in the case we know doesn't work.

This should shake out some more bugs, hopefully without causing
too much pain.
2017-04-23 01:49:01 -07:00
Slava Pestov
7b8e26b621 AST: Remove unnecessary canonicalization step in SubstitutionMap::lookupConformance() 2017-04-23 01:49:01 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Doug Gregor
6878538ba1 [AST] Remove GenericEnvironment from SubstitutionMap; it's unnecessary. 2017-04-05 11:21:39 -07:00
Doug Gregor
8748601b26 [SubstitutionMap] Dump generic signature in debug output.
You can't interpret a conformance map without a generic signature, so
add it to the debug output.
2017-04-05 11:21:38 -07:00
Doug Gregor
ba10723e9c [SubstitutionMap] Make conformance lookup robust against ill-formed conformances. 2017-04-04 14:07:43 -07:00
Doug Gregor
97c6707910 [SubstitutionMap] Cope with missing conformances in lookupConformance().
When asking a substitution map for a conformance, it's okay if the
conformance isn't there---just detect this case and return None.

Also, collapse a redundant testcase Huon noted.
2017-04-04 10:58:01 -07:00
Doug Gregor
d0499a3613 [SubstitutionMap] Eliminate the parent map and handle archetype conformances.
When looking for a conformance for an archetype, map it out of context
to compute the conformance access path, then do the actual lookups
based on mapping the starting type back into the context. Eliminate
the parent map and "walk the conformances" functionality.
2017-04-04 10:58:01 -07:00
Doug Gregor
6bf4c461aa [SubstitutionMap] Use conformance access paths to find conformances.
When looking up a particular conformance in a SubstitutionMap, use the
associated generic signature to determine how to find that conformance
(via a conformance access path). Follow that conformance access path
to retrieve the desired conformance.

FIXME: There is still one failure, here

  Constraints/generic_overload.swift

which appears to be because we either have the wrong generic signature
for the override case or we are looking in the wrong map.
2017-04-04 10:58:01 -07:00
Doug Gregor
81c21b85f2 [AST] Track the generic signature/environment for a substitution map.
Substitution maps are effectively tied to a particular generic
signature or environment; keep track of that signature/environment so
that we can (eventually) use it to find conformances.
2017-04-04 10:58:01 -07:00
Slava Pestov
f10f00d887 Sema: Narrow fix for a circular protocol conformance checking case
We go to look at the conforming protocols of an associated type
but we haven't built the generic environment for the associated
type's protocol yet.

In the test case given, we find the conformance later via a
different path, so everything continues to work.

SubstitutionMap::lookupConformance() is hopefully getting a
makeover soon, and this hack will go away.

Fixes <rdar://problem/31302713>.
2017-03-30 02:03:52 -07:00
Roman Levenstein
791c449f7c Disable SubstitutionMap::verify() for now. We are not ready for it yet.
We should enable it again once the substitutions machinery and GenericSignatureBuilder always generate correct SubstitutionMaps.
But for now let’s disable it to unblock the CI jobs.

Should fix SR-4322 and rdar://31224655
2017-03-24 16:51:57 -07:00
Saleem Abdulrasool
ef141eb2c1 AST: avoid "deprecation" warning
The "dump()" interface is meant for use in the debugger only.  Use the
parameter form to avoid the -Wdeprecated-declaration warning.
2017-03-21 14:49:46 -07:00
Roman Levenstein
6f54798df8 Verify the construction of SubstitutionMaps
After substitution maps are constructed, check their invariants. It helps to catch very subtle bugs related to substitutions.
2017-03-20 15:30:12 -07:00
Slava Pestov
5381921131 AST: Generalize SubstitutionMap::combineSubstitutionMaps() 2017-03-08 13:54:28 -08:00
Slava Pestov
de2f5f78ac AST: Remove Substitution::subst()
I want to get rid of Substitution entirely, and now we have
the right abstractions to do everything with SubstitutionMap.
2017-03-07 16:04:54 -08:00
Slava Pestov
13cc1ad07c AST: Remove SubstitutionMap::getConformances() 2017-03-07 16:04:53 -08:00
Slava Pestov
b41888acdf AST: Add SubstitutionMap::subst()
This will replace Substitution::subst().
2017-03-07 16:04:46 -08:00
Doug Gregor
042e6510c3 [AST] Drop ProtocolDecl's "inherited protocols" list.
The list of directly inherited protocols of a ProtocolDecl is already
encoded in the requirement signature, as conformance constraints where
the subject is Self. Gather the list from there rather than separately
computing/storing the list of "inherited protocols".
2017-02-20 09:41:00 -08:00
Slava Pestov
879a7586ba AST: Fix crash with invalid protocols in SubstitutionMap::getProtocolSubstitutions()
If a protocol appears inside a generic context, the Self parameter
will not be the only generic parameter; outer generic parameters
will be part of the protocol's signature also.

Change the asserts in getProtocolSubstitutions() to return error
types instead in this case.
2017-02-17 14:53:17 -08:00
Slava Pestov
41eba98902 Gardening: Fix some unused variable warnings in no-assert builds 2017-02-15 12:57:35 -08:00
Slava Pestov
e98ac967d4 AST: Fix warning about missing return in SubstitutionMap::getProtocolSubstitutions()
The branch without the return is actually unreachable, so let's
add an assert.
2017-02-06 21:36:31 -08:00
Slava Pestov
1954665a95 AST: Remove more uses of SubstitutionMap::add{Substitution,Conformance}()
A new SubstitutionMap::getProtocolSubstitutions() method handles
the case where we construct a trivial SubstitutionMap to replace
the protocol Self type with a concrete type.

When substituting one opened existential archetype for another,
use the form of Type::subst() that takes two callbacks instead of
building a SubstitutionMap. SubstitutionMaps are intended to be
used with keys that either come from a GenericSignature or a
GenericEnvironment, so using them to replace opened archetypes
doesn't fit the conceptual model we're going for.
2017-02-06 20:33:39 -08:00
Slava Pestov
b22f9ea487 AST: Remove SubstitutionMap::getMap()
We don't want to expose the fact that SubstitutionMaps are
backed by a DenseMap, since that's going to change soon.
2017-02-06 19:43:33 -08:00
Slava Pestov
1c21b88260 AST: New SubstitutionMap::combineSubstitutionMaps() method
In several places, we construct a SubstitutionMap by taking
generic parameters from one SubstitutionMap up to a certain
depth, with the rest coming from a second SubstitutionMap.

Factor this out into a new utility method, to help with
hiding the internal representation of SubstitutionMap from
clients.

This was meant to be NFC, but it actually fixes a crash in
the devirtualizer, because the old logic there was slightly
wrong, so I added a test for this.
2017-02-06 16:14:07 -08:00
Slava Pestov
cf4043b668 AST: Get rid of old form of Type::subst()
First, add some new utility methods to create SubstitutionMaps:

- GenericSignature::getSubstitutionMap() -- provides a new
  way to directly build a SubstitutionMap. It takes a
  TypeSubstitutionFn and LookupConformanceFn. This is
  equivalent to first calling getSubstitutions() with the two
  functions to create an ArrayRef<Substitution>, followed by
  the old form of getSubstitutionMap() on the result.

- TypeBase::getContextSubstitutionMap() -- replacement for
  getContextSubstitutions(), returning a SubstitutionMap.

- TypeBase::getMemberSubstitutionMap() -- replacement for
  getMemberSubstitutions(), returning a SubstitutionMap.

With these in place, almost all existing uses of subst() taking
a ModuleDecl can now use the new form taking a SubstitutionMap
instead. The few remaining cases are explicitly written to use a
TypeSubstitutionFn and LookupConformanceFn.
2017-02-03 19:55:40 -08:00
Slava Pestov
00700c1729 AST: Always call getMemberForBaseType() with non-null origBase 2017-02-03 19:55:39 -08:00
Doug Gregor
6bcfce5f00 [AST] Teach SubstitutionMap::lookupConformance to search parent conformances.
Conformances for an associated type might be attached to any
same-named associated type of the parent, so search those as
well. Overally, this is a fairly expensive search, and there is likely
a better architectural solution that will require some refactoring of
SubstitutionMap.
2017-01-24 19:36:44 -08:00
Doug Gregor
51fbd297bc [AST] Prefer concrete to abstract conformances in SubstitutionMap's lookup. 2017-01-24 19:36:44 -08:00
Doug Gregor
d3c7a71f85 [AST] Factor the search for conformances more cleanly. NFC 2017-01-24 19:36:44 -08:00
Doug Gregor
227919a97e [AST] Add SubstitutionMap::dump() debugging utility 2017-01-24 19:36:44 -08:00
Slava Pestov
24b2531e5f AST: Have SubstitutionMaps manage the memory for the conformance array 2017-01-18 00:15:59 -08:00
Doug Gregor
a232b41f87 [Archetype builder] Use archetype anchors exclusively in requirements.
When enumerating requirements, always use the archetype anchors to
express requirements. Unlike "representatives", which are simply there
to maintain the union-find data structure used to track equivalence
classes of potential archetypes, archetype anchors are the
ABI-stable canonical types within a fully-formed generic signature.

The test case churn comes from two places. First, while
representatives are *often* the same as the archetype anchors, they
aren't *always* the same. Where they differ, we'll see a change in
both the printed generic signature and, therefore, it's
mangling.

Additionally, requirement inference now takes much greater
care to make sure that the first types in the requirement follow
archetype anchor ordering, so actual conformance requirements occur in
the requirement list at the archetype anchor---not at the first type
that is equivalent to the anchor---which permits the simplification in
IRGen's emission of polymorphic arguments.
2017-01-12 11:07:05 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Doug Gregor
16585992f6 [AST] Strengthen signature of SubstututionMap::addSubstitution().
It requires a CanSubstitutableType internally, so use that in the
signature and fix up all of the callers.
2017-01-05 16:04:19 -08:00
Slava Pestov
ac7a3030e3 Sema: Fix inheritance of designated initializers with default arguments in generic context
We were dropping the substitutions required to call the default
argument generator on the floor, when the correct solution is to
remap them to substitutions in terms of the base class signature.

Fixes <rdar://problem/29721571>.
2016-12-22 14:33:01 -05:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Doug Gregor
85ba4fe40f [AST] Narrow TypeSubstitutionMap to SubstitutableType keys.
Type substitution works on a fairly narrow set of types: generic type
parameters (to, e.g., use a generic) and archetypes (to map out of a
generic context). Historically, it was also used with
DependentMemberTypes, but recent refactoring to eliminate witness
markers eliminate that code path.

Therefore, narrow TypeSubstitutionMap's keys to SubstitutableType,
which covers archetypes and generic type parameters. NFC
2016-11-15 11:34:09 -08:00
Doug Gregor
e3ce039705 Remove SubstitutionMap::removeType(). 2016-11-01 20:42:25 -07:00
Doug Gregor
b5511bd3ea [Serialization] (De-)serialize witnesses completely.
The witnesses in a NormalProtocolConformance have never been
completely serialized, because their substitutions involved a weird
mix of archetypes that blew up the deserialization code. So, only the
witness declarations themselves got serialized. Many clients (the type
checker, SourceKit, etc.) didn't need the extra information, but some
clients (e.g., the SIL optimizers) would end up recomputing this
information. Ick.

Now, serialize the complete Witness structure along with the AST,
including information about the synthetic environment, complete
substitutions, etc. This should obsolete some redundant code paths in
the SIL optimization infrastructure.

This (de-)serialization code takes a new-ish approach to serializing
the synthetic environment in that it avoids serializing any
archetypes. Rather, it maps everything back to interface types during
serialization, and deserialization forms a new generic environment
(with new archetypes!) on-the-fly, mapping deserialized types back
into that environment (and to those archetypes). This way, we don't
have to maintain identity of archetypes in the deserialization code,
and might get some better re-use of the archetypes.

More of rdar://problem/24079818.
2016-11-01 17:14:17 -07:00
Slava Pestov
a993e36c06 AST: Add a new SubstitutionMap data structure
This replaces the TypeSubstitutionMap / ConformanceMap pair that
has been appearing more and more lately.
2016-09-08 21:59:11 -07:00