Commit Graph

512 Commits

Author SHA1 Message Date
Doug Gregor
9552692cd3 [GSB] Eliminate equivalence-class lookup from type canonicalization.
Make GenericSignatureBuilder::getCanonicalTypeParameter() independent of
equivalence classes. This is primarily cleanup, because all of these
equivalence-class resolution calls were actually dead from the point
where we changed the key for the rewrite-roots DenseMap over to
dependent types.
2018-03-11 23:55:23 -07:00
Doug Gregor
0eedacf183 [GSB] Use anchors for the keys in the mapping to rewrite tree roots.
Rather than keying the rewrite tree roots on equivalence classes,
which imply a mapping through potential archetypes, key on (canonical)
types that are currently the anchors of equivalence classes.
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
455cb60a9e [GSB] Don't eagerly minimize the term-rewriting system.
It's not worth the cost in compile times.
2018-03-09 10:42:15 -08:00
Doug Gregor
624f2ba7b0 [GSB] Eliminate redundant rules from the rewrite tree.
As part of minimization, example each rule to determine whether
minimizing the left-hand-side (while ignoring the rule under question)
still produces the right-hand side. If so, the rule is redundant and
will be eliminated from the rewrite tree.
2018-03-09 10:42:14 -08: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
Doug Gregor
8346917b8f [GSB] RewriteTreeNode::bestMatch -> RewriteTreeNode::bestRewritePath. 2018-03-09 10:42:14 -08:00
Doug Gregor
e12abf5363 [GSB] Generalize traversal of all rewrite rules in a rewrite tree.
Use the generalized traversal to implement the mergeInto() operation.
2018-03-09 10:42:14 -08:00
Doug Gregor
1eb18408c8 [GSB] Improve a non-descriptive comment. 2018-03-09 09:51:41 -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
Slava Pestov
1d6256750c AST: Clean up dead code in GenericSignatureBuilder 2018-03-08 16:43:25 -07: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
823d2a990b [GSB] Always ensure that we wire up typealiases in protocol extensions.
During "expansion" of the requirements of a protocol, we check all of
the inherited associated types against definitions within the
protocol. Also look for concrete types within extensions of that
protocol, so we can identify more places where developers have used
typealiases in inheriting protocols to effect a same-type constraint
on an inherited associated type.

Fixes SR-7097 / rdar://problem/38001269.
2018-03-05 17:10:39 -08:00
Doug Gregor
04ec33c53c [GSB] Address feedback from @huon_w on the term-rewriting code.
Various simplifications, including simpler APIs, better in-code
documentation, and simpler memory management.
2018-02-16 16:01:21 -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
c7d0e16313 [GSB] Eliminate unused statistic. 2018-02-16 14:37:37 -08:00
Doug Gregor
31279be0cc [GSB] Properly apply rewrite rules for generic params -> generic params.
Although we were properly recording rewrite rules like tau_0_1 -> tau_0_0
in the rewrite tree, we were failing to apply them, so tau_0_1 wouldn’t
get properly canonicalized. Fix this, and add some assertions to make sure
we catch this with our current test suite.

Fixes rdar://problem/37469390.
2018-02-16 14:37:30 -08:00
Doug Gregor
20ffe48425 [GSB] Fix GraphViz printing of equivalence classes. 2018-02-16 11:16:22 -08:00
Doug Gregor
8b04dd3dbe [GSB] Avoid unresolved dependent member types in generic signatures.
The GenericSignatureBuilder is allowing unresolved dependent member
types to creep into generic signatures, which eventually blows up in
name mangling. Prefer to pick dependent member types that are
fully-resolved when choosing anchors.

This is a spot fix; a better approach would eliminate the notion of
unresolved dependent member types entirely from
PotentialArchetype. That's tracked by rdar://problem/35839277.

Fixes rdar://problem/36549499.
2018-02-13 15:40:53 -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
3804f8a0cd [GSB] Fix term rewriting code to fully-restart after a rewrite. 2018-02-07 16:37:28 -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
swift-ci
e06db58739 Merge pull request #14431 from DougGregor/gsb-cleanups 2018-02-05 21:53:15 -08:00
Doug Gregor
5487c85d78 [GSB] Compute minimized sources more generally. 2018-02-05 20:33:49 -08:00
Doug Gregor
ca45fb0187 [GSB] Make GraphViz output show all of the edges. 2018-02-05 20:14:30 -08:00
Huon Wilson
bc37733daf Merge pull request #14293 from huonw/nested-conditional-types
[GSB] Infer requirements from parents of types.
2018-02-05 09:50:10 +11:00
Huon Wilson
9aed3e83a8 [GSB] Infer requirements from parents of types.
A type Foo<...>.Bar may only exist conditionally (i.e. constraints on the
generic parameters of Foo), in which case those conditional requirements
should be implied when Foo<...>.Bar is mentioned.

Fixes SR-6850.
2018-02-02 08:44:30 +11:00
David Zarzycki
c5dc6ceb4d Merge pull request #14303 from davezarzycki/nfc_do_not_reinterpret_cast_into_CanTypeWrappers
[AST] NFC: Do not reinterpret_cast pointers into CanTypeWrappers
2018-02-01 07:17:03 -05:00
Doug Gregor
9c395a47bd [AST] Report generic signature minimization errors through diagnostics.
Rather than crashing when a generic signature is found to be non-minimal,
report the non-minimal requirement via the normal diagnostics machinery so
we can properly test for it.

Fixes rdar://problem/36912347 by letting us track which cases are
non-minimal in the standard library explicitly, so we can better
decide whether it's worth implementing a complete solution.
2018-01-31 17:14:19 -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
d18ecda796 [GSB] Ensure that we don't build an invalid potential archetype.
Queries against the generic signature might use types that are
ill-formed for that generic signature, e.g., because they refer to
associated types of unrelated protocols. Detect such conditions to
maintain GSB invariants that all potential archetypes in a well-formed
generic signature are well-formed.

Fixes the crash in SR-6797 / rdar://problem/36673825.
2018-01-23 13:52:06 -08:00
Doug Gregor
879b1802f0 [GSB] Downgrade "neither type in same-type has a generic param" error to warning.
There is nothing specifically wrong with uttering a same-type
constraint in a where clause where both sides are concrete
types. Downgrade this to a warning; we'll check that the concrete
types match (of course), and such a well-formed constraint will simply
be canonicalized away.

This aids the migration of IndexDistance from an associated type to
Int.
2017-12-07 14:44:35 -08:00
Doug Gregor
5431400367 [Name lookup] Allow where clauses to find typealiases declared in a protocol.
Within the where clause of (e.g.) an extension, unqualified name lookup is
permitted to find associated types. Extend this to also include finding
typealiases declared within the protocol itself.

This is service of the IndexDistance change (SE-0191), and
fixes rdar://problem/35490504.
2017-12-07 13:46:33 -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
b673e5d113 [GSB] Simplify resolveDependentMemberTypes().
Make sure that we always get the underlying concrete type or the anchor
of a particular equivalence class.
2017-11-06 09:11:02 -08:00
Doug Gregor
6fc218ba7d [GSB] Make compareDependentTypes() visible to other translation units. 2017-11-04 22:46:01 -07:00
Doug Gregor
c845b07964 [GSB] Simplify compareDependentTypes().
Remove a crufty, unnecessary check.
2017-11-04 22:01:38 -07:00
Doug Gregor
7fff801d87 [GSB] Remove the PotentialArchetype-based compareDependentTypes().
It had one caller that could easily use the Type-based variant of this
function.
2017-11-04 21:52:57 -07:00
Doug Gregor
5ad254bf3f [GSB] Stop walking associated types of a protocol just due to a superclass req. 2017-11-02 15:17:42 -07:00
Doug Gregor
0416292e5c [GSB] Eliminate unnecessary walk over the associated types.
We lazily resolve all of this information about nested potential archetypes
anyway; there's no need to do it eagerly.
2017-11-02 14:56:43 -07:00
Davide Italiano
2b1921b207 Merge pull request #12686 from dcci/unused2
[gardening] More unused variables/lambda captures. NFCI.
2017-11-01 12:48:21 -07:00
Doug Gregor
fe54e70fce [GSB] Don't infer requirements from types in the definitions of protocols.
Previously, we were inferring requirements from types within the definitions
of protocols, e.g., given something like:

    protocol P {
      associatedtype A: Collection
      associatedtype B where A.Element == Set<B>
    }

we would infer that B: Hashable. The code for doing this was actually
incorrect due to its mis-use of requirement sources, causing a few
crashers. Plus, it's not a good idea in general because it hides the
actual requirements on B. Stop doing this.

Also stop trying to infer requirements from conditional
requirements---those have already been canonicalized and minimized, so
there's nothing to infer from.
2017-10-31 15:28:19 -07:00
Davide Italiano
87de4637fc [gardening] More unused variables/lambda captures. NFCI. 2017-10-31 10:35:24 -07:00
Doug Gregor
832a154b08 [GSB] Make sure we wire up same-named type declarations consistently.
Fixes a former crasher that included well-formed code that was rejected
by my previous refactoring. Said crasher now passes, and IRGen's properly
as well. Also, account for three more fixed crashers.
2017-10-27 22:17:45 -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
d731a948f4 [GSB] Simplify enumeration of the subject types for requirements.
The first step in enumerating the minimal, canonical set of requirements for
a generic signature is identifying which "subject" types will show up in
the left-hand side of the requirements. Previously, this would require us
to realize all of the potential archetypes, and perform a number of
archetype-anchor computations and comparisons.

Replace that with a simpler walk over the equivalence classes,
identifying the anchor types within each derived same-type component
of those equivalence classes, which form the subject types. This is
more straightforward, doesn't rely on potential archetypes, simplifies
the code, and eliminates a silly O(n^2)-for-small-n that's been
bothering me for a while.
2017-10-27 16:10:02 -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