Commit Graph

212 Commits

Author SHA1 Message Date
Huon Wilson
10b30fef78 [GSB] Explicit error on unsupported conditional conformance recursion.
This doesn't fix the fundamental problem of correctly handling such cases, but
it is better than the "error message" that occurred previously:

    Assertion failed: ((bool)typeSig == (bool)extensionSig && "unexpected generic-ness mismatch on conformance").

Fixes the crash rdar://problem/41281406 (that in
https://bugs.swift.org/browse/SR-6569 (rdar://problem/36068136)),
https://bugs.swift.org/browse/SR-8019 (rdar://problem/41216423) and
https://bugs.swift.org/browse/SR-7989 (rdar://problem/41126254).
2018-06-21 10:53:30 +10:00
Huon Wilson
1ba49949f9 [NFC]/[GSB] Move addConditionalRequirements to be a method. 2018-06-20 18:30:27 +10:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07:00
Huon Wilson
0e337acae0 Revert "[AST] Ensure requirements are correctly identified as conditional." 2018-03-14 08:12:37 +11:00
Doug Gregor
31792ac3f7 [GSB] Eliminate potential archetypes from the same-type constraint representation.
Same-type constraints are one of the primary places where potential
archetypes are used explicitly in the GenericSignatureBuilder. Switch
the representation over to a dependent type (represented by a `Type`)
instead, furthering the demise of PotentialArchetype.
2018-03-12 10:33:39 -07:00
Doug Gregor
4481a42c49 [GSB] Push potential archetype realization slightly deeper.
Same-type constraints are (still) described in terms of potential
archetypes, so push the "realization" operation for potential
archetypes down into the function that adds a same-type constraint
between type parameters.
2018-03-11 23:55:23 -07:00
Doug Gregor
0f7c822371 [GSB] Add same-type rewrite rules via dependent types.
GenericSignatureBuilder::addSameTypeRewriteRule() was described in
terms of an equivalence class and a potential archetype. Rework it in
terms of two dependent types (i.e., the anchors of their equivalence
classes), so we don't need to rely on the PotentialArchetype
mechanism.
2018-03-11 23:55:23 -07:00
Doug Gregor
35902bbee2 [GSB] Simplifying via the term rewriting system cannot fail.
Now that we no longer have DependentMemberTypes within the GSB that don't
have associated type declarations, we can no longer fail when
unpacking type into a RewritePath, so remove a bunch of optionals and
null Type checks that are now superfluous.
2018-03-11 23:55:22 -07:00
Huon Wilson
be28f6830c Merge pull request #15094 from huonw/conditional-requirement-order
[AST] Ensure requirements are correctly identified as conditional.
2018-03-12 09:53:02 +11:00
Doug Gregor
91aa964714 [GSB] Minimize the right-hand sides of rules in the term-rewriting system.
Introduce the first step of a minimization algorithm for the
term-rewriting system used to produce anchors of equivalence
classes. This step simplifies the right-hand sides of each rewrite
rule, so that each rewrite step goes to the minimal result.

This code is currently not enabled; it *can* be enabled by minimizing
before computing anchors, but it ends up pessimizing compile times to
do so.
2018-03-09 10:42:14 -08:00
Huon Wilson
88b9f2c94d [AST] Ensure requirements are correctly identified as conditional.
Previously, the following code would result in different sets of
conditional requirements:

  struct RedundancyOrderDependenceGood<T: P1, U> {}
  extension RedundancyOrderDependenceGood: P2 where U: P1, T == U {}

  struct RedundancyOrderDependenceBad<T, U: P1> {}
  extension RedundancyOrderDependenceBad: P2 where T: P1, T == U {}

The T: P1 requirement is redundant: it is implied from U: P1 and T == U,
but just checking it in the signature of the struct isn't sufficient,
the T == U requirement needs to be considered too. This uses a quadratic
algorithm to identify those cases. We don't think the quadratic-ness is
too bad: most cases have relatively few requirements.

Fixes rdar://problem/34944318.
2018-03-09 14:00:09 +11:00
Pavel Yaskevich
4b05449c37 [GSB] Eliminate concrete potential archetypes by early substitution
To make generic signature builder more robust it's imperative to
eliminate possibility of out-of-order typealias substitution.

These changes try to make it so potential archetypes could only be
constructed with associated types by doing early concrete type (typealias)
subsitution while trying to resolve equivalence class.
2018-03-06 23:34:19 -08:00
Doug Gregor
bd266b0520 [GSB] Introduce type-rewriting rules only when we merge equivalence classes.
Instead of representing every same-type constraint we see as a rewrite rule,
only record rewrite rules when we merge equivalence classes, and record
rules that map the between the anchors of the equivalence classes. This
gives us fewer, smaller rewrite rules that (by construction) build correct
anchors.
2018-02-16 15:09:58 -08:00
Doug Gregor
1e7562a527 [GSB] Simplify the anchor cache to always rely on term rewriting.
Use term rewriting exclusively in the computation of the canonical
dependent type (anchor) for an equivalence class, eliminating any dependence
on the specific potential archetypes and eliminating the hacks around
generic type parameters.
2018-02-07 16:37:29 -08:00
Doug Gregor
f2bef2da54 [GSB] Generalize rewrite paths to support “absolute” paths.
Previously, the term rewriting logic for same-type constraints was only 
able to express “relative” rewrites, where the base of the type being 
rewritten (i.e., the generic type at the root) is unchanged. This meant
that we were unable to capture same-type constraints such as “T == U” or
“T.Foo == T” within the rewriting system.

Separate out the notion of a rewrite path and extend it with an optional
new base. When we’re simplifying a term and we encounter one of these
replacements, the whole type from the base up through the last-matched
path component gets replaced with the replacement path.
2018-02-07 16:37:29 -08:00
Doug Gregor
a41922169a [GSB] Use term rewriting to compute anchors of equivalence classes.
Fully rewriting a given type will produce the canonical representation of that type, because all rewrite rules take a step toward a more-canonical type. Use this approach to compute the anchor of an equivalence class, replacing the existing ad hoc approach of enumerating known potential 
archetypes—which was overly dependent on having the “right” set of
potential archetypes already computed.
2018-02-07 16:37:29 -08:00
Doug Gregor
bfeb846475 [GSB] Record same-type constraints as term rewrite rules.
Introduce a new representation of same-type constraints as rewrite
rules in a term-rewriting system, where each same-type constraint maps
to a rewrite rule that produces a "more canonical"
term. Fully-simplifying a type according to these rewrite rules will
produce the canonical representation of that type, i.e., the "anchor"
of its equivalence class.

The rewrite rules are stored as a prefix tree, such that a "match"
operation walks the tree matching the prefix of a path of associated
type references, e.g., SubSequence -> SubSequence -> Element, and any
node within the tree can provide a replacement path (e.g.,
"Element"). The "dump" operation provides a visualization of the tree,
e.g.,

::
  `--(cont'd)
      `--Sequence.Iterator
      |   `--IteratorProtocol.Element --> [Sequence.Element]
      `--Sequence.SubSequence --> []
      |   `--Sequence.Element --> [Sequence.Iterator -> IteratorProtocol.Element]
      |   |   `--(cont'd) --> [Sequence.Element]
      |   `--Sequence.Iterator --> [Sequence.Iterator]
      |   |   `--IteratorProtocol.Element --> [Sequence.Iterator -> IteratorProtocol.Element]
      |   `--Sequence.SubSequence --> []
      |   |   `--Sequence.Element --> [Sequence.Element]
      |   |   `--Sequence.Iterator --> [Sequence.Iterator]
      |   `--C.Index --> [C.Index]
      |   `--C.Indices --> [C.Indices]
      |       `--Sequence.Element --> [C.Indices -> Sequence.Element]
      |       `--Sequence.SubSequence --> [C.Indices -> Sequence.SubSequence]
      |       `--C.Index --> [C.Indices -> C.Index]
      |       `--C.Indices --> [C.Indices -> C.Indices]
      `--C.Index --> [Sequence.Element]
      `--C.Indices
          `--Sequence.Element --> [C.Index]

Thus far, there are no clients for this information: this commit
starts building these rewriting trees and can visualize them for debug
information.
2018-02-07 16:33:10 -08:00
David Zarzycki
3da6fe9c0d [AST] NFC: Do not reinterpret_cast pointers into CanTypeWrappers
This also introduces 'TypeArrayView' for when a 'Type' is statically
known to be a given TypeBase subclass.
2018-01-31 11:20:05 -05:00
Doug Gregor
e10ecbd80e [GSB] Only check protocol requirements not generated from the signature.
If we used the requirement signature to create a protocol requirement
element in a requirement source, there's no need to verify that it's
from the requirement signature (duh).
2017-12-13 20:17:18 -08:00
Doug Gregor
5d5e6122f3 [GSB] Add verification of all generic signatures within a module.
Add a verification pass to ensure that all of the generic signatures in
a module are both minimal and canonical. The approach taken is quite
direct: for every (canonical) generic signature in the module, try
removing a single requirement and forming a new generic signature from
the result. If that new generic signature that provide the removed
requirement, then the original signature was not minimal.

Also canonicalize each resulting signature, to ensure that it meets the
requirements for a canonical signature.

Add a test to ensure that all of the generic signatures in the Swift
module are minimal and canonical, since they are ABI.
2017-11-07 15:20:38 -08:00
Doug Gregor
6fc218ba7d [GSB] Make compareDependentTypes() visible to other translation units. 2017-11-04 22:46:01 -07:00
Doug Gregor
5b8c914582 [GSB] Reimplement equivalence class "anchor" logic.
Replace the pair of PotentialArchetype's getArchetypeAnchor() and
getNestedArchetypeAnchor() with a straightforward, more-efficient
computation based on equivalence classes. This reduces the number of
times we query the archetype anchor cache by 88% when building the
standard library, as well as eliminating some
PotentialArchetype-specific APIs.
2017-10-27 21:46:45 -07:00
Doug Gregor
4ff05fa51b [GSB] Eliminate a bunch of unnecessary PotentialArchetype* interfaces.
All of these constraint-checking functions work on the equivalence class.
2017-10-27 14:00:43 -07:00
Doug Gregor
1117eff2a8 [GSB] Eliminate PotentialArchetype from enumerateRequirements(). 2017-10-27 13:49:18 -07:00
Doug Gregor
29390e095a [GSB] Switch the anchor of a derived same-type component to UnresolvedType.
It was a PotentialArchetype*, but we don't want to have to realize a
potential archetype to model this.
2017-10-27 13:32:59 -07:00
Doug Gregor
16ea8aedf2 [GSB] Switch some more APIs away from PotentialArchetype. 2017-10-27 12:58:42 -07:00
Doug Gregor
d2f0df4b3b [GSB] Eliminate unused Constraint<T>::realizeSubjectPotentialArchetype(). 2017-10-24 16:18:07 -07:00
Doug Gregor
f5e44a4968 [GSB] Don't realize potential archetypes for same-type-to-concrete constraints. 2017-10-24 11:11:21 -07:00
Doug Gregor
642b45835c [GSB] Use result of recordSameTypeConstraint() to short-circuit.
Centralizes the logic for short-circuiting based on same-type
constraints.
2017-10-23 21:51:54 -07:00
Doug Gregor
bd0625f083 [GSB] Flatten list of same-type requirements.
Equivalence classes stored their same-type constraints in a MapVector
keyed on the source potential archetype, which allowed traversal along the
paths of the graph. However, this capability isn't required, because we
end up walking all of the edges each time. Flatten the list of same-type
constraints to a single vector, eliminating constraint duplication between
the source and the target out-edge lists.

This cuts down on the number of same-type constraints we record by 50%,
but performance gains are limited (6% of stdlib type-checking time)
because most of the time these same-type constraints were skipped
anyway.
2017-10-23 21:33:41 -07:00
Doug Gregor
e8c9b3ffa2 [GSB] Eliminate the GenericSignatureBuilder from PotentialArchetype.
Now that getBuilder() is gone, stash an ASTContext into root
PotentialArchetypes instead of a GenericSignatureBuilder. This eliminates
some ickiness where we had to re-root potential archetypes when moving a
GenericSignatureBuilder.
2017-10-23 14:21:43 -07:00
Doug Gregor
2f3b2dfc7b [GSB] Plumb generic params through getDependentType() fully.
This allows us to eliminate `getBuilder()`.
2017-10-23 14:11:38 -07:00
Doug Gregor
214f9d54ca [GSB] Eliminate the penultimate caller to PotentialArchetype::getBuilder(). 2017-10-23 11:38:36 -07:00
Doug Gregor
a510e9b56d [GSB] Eliminate most uses of PotentialArchetype::getBuilder().
The remaining two uses after this refactor are... stubborn.
2017-10-23 10:39:21 -07:00
Doug Gregor
42c1619088 [GSB] Eliminate a debugging-only source of potential archetype realization. 2017-10-20 21:57:23 -07:00
Doug Gregor
972f04832b [GSB] Teach expandConformanceRequirement() to use a resolved type.
Eliminates another too-early source of realization of potential archetypes.
2017-10-20 21:52:40 -07:00
Doug Gregor
28e3e5cf40 [GSB] Eliminate potential archetypes from requirement sources entirely. 2017-10-20 21:38:12 -07:00
Doug Gregor
1f09f3bc10 [GSB] Eliminate potential archetypes from minimizing requirement sources.
Refactor the interfaces that involve “walking” a requirement source
(e.g., minimization, computation of the affected type, etc.) to rely
only on interface types and not potential archetypes.
2017-10-20 16:42:39 -07:00
Doug Gregor
aa9755d149 [GSB] Start moving RequirementSource off of potential archetypes.
Eliminate potential archetypes from most of the public interface of
RequirementSource.
2017-10-20 15:18:36 -07:00
Doug Gregor
f379ba5a20 [GSB] Move definition of GenericSignatureBuilder::Constraint out of the class.
It's getting big, and there will soon be an ordering dependency with
FloatingRequirementSource. NFC
2017-10-20 13:41:52 -07:00
Doug Gregor
fd191fe59c [GSB] Switch Constraint's "subject" over to an unresolved type.
We're still eagerly resolving the subject to a potential archetype
everywhere, but this refactoring allows us to start introducing laziness.
2017-10-20 13:37:47 -07:00
Doug Gregor
81d35d3f78 [GSB] Abstract away the PotentialArchetype stored in a constraint. 2017-10-20 13:16:59 -07:00
Doug Gregor
b046a35994 [GSB] Eliminate visitPotentialArchetypes().
Walking all of the potential archetypes is expensive. Instead, walk the
equivalence classes, which are the fundamental unit.
2017-10-17 21:00:24 -07:00
Doug Gregor
476a557e55 [GSB] Eliminate some now-unused API on PotentialArchetype. 2017-10-17 21:00:24 -07:00
Doug Gregor
0d42fbf691 [GSB] Delay realization of potential archetypes for conformance requirements.
While here, consolidate PotentialArchetype::addConformance() into
EquivalenceClass::recordConformanceConstraint().
2017-10-17 21:00:24 -07:00
Doug Gregor
3ac1b3b14b [GSB] Push potential archetype realization into addConformanceRequirement(). 2017-10-17 21:00:24 -07:00
Doug Gregor
708412672e [GSB] Teach resolveConcreteConformance() to use ResolvedType. 2017-10-17 21:00:24 -07:00
Doug Gregor
760f23af7c [GSB] Teach much of superclass-constraint handling to use ResolvedType.
We're still realizing potential archetypes far too often, but it's a start.
2017-10-17 21:00:24 -07:00
Doug Gregor
4f339eb0ca [GSB] Push potential archetype realization layout-constraint handling.
We want to delay the realization of potential archetypes as late as
possible; for layout constraints, push realization of potential archetypes
down past the point where we know whether we’re learning anything new
about the equivalence class.

No functionality change, yet.
2017-10-17 21:00:24 -07:00
Doug Gregor
de5e0470ce [GSB] Rename ResolveResult to ResolvedType.
The old name was the better one; now we have a better (single)
representation.
2017-10-17 21:00:24 -07:00