Commit Graph

539 Commits

Author SHA1 Message Date
Doug Gregor
2b2e143f59 [Request-Evaluator] Introduce a request for getting an "inherited type". 2018-06-14 15:29:57 -07:00
Doug Gregor
d9c0ca951a Merge pull request #16997 from DougGregor/resolve-fewer-decl-signatures
[AST] Resolve fewer decl signatures
2018-06-06 13:08:55 -07:00
Doug Gregor
bc0445d1f7 Narrow a few LazyResolver::resolveDeclSignature() calls to what they really need.
Specifically, introduce entrypoints for "resolve overridden decl" and "is @objc". These can
be computed cheaply in many more cases.
2018-06-04 18:15:39 -07:00
Huon Wilson
ec1ed4ca57 Merge pull request #16893 from huonw/errors-are-unresolved-equivalence-class
[GSB] Error types end up as unresolved equivalence classes.
2018-06-04 16:27:48 -07:00
Huon Wilson
10c3e6cea9 [GSB] Error types end up as unresolved equivalence classes.
We need to make sure they don't end up as "concrete" equivalence
classes, because they behave more like unresolved ones.

Fixes rdar://problem/40009245.
2018-05-29 16:21:31 -07:00
Slava Pestov
a1c6f069c5 AST: Don't infer requirements from existential types 2018-05-28 19:09:33 -07:00
Doug Gregor
5ed639fec6 [GSB] Canonicalize types when checking concrete and superclass constraints.
Concrete and superclass constraints may be written involving type parameters
that are later resolved to concrete types. Perform the substitution to
ensure that type equality within constraint checking accounts for other
same-type constraints.

Fixes assertion in rdar://problem/40428709.
2018-05-23 14:49:58 -07: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
ff779663b2 [GSB] Use a larger set for checking for visited requirements in 'isRecursive'.
This claws back some of the regression of
2feb830b58 on certain generics heavy code.

rdar://problem/40005262 (no-opt) goes from 7.83 -> 5.75 and
rdar://problem/40010847 (opt) goes from 90.7 -> 66.6 (both -27%).
2018-05-17 14:56:51 +10:00
Doug Gregor
539c352353 [GenericSignatureBuilder] Block recursion through getTypeInContext().
We could end up recursing through getTypeInContext() before we had time to
diagnose the recursion, so be *certain* that we can't recurse here to fix
a few crashes.
2018-05-03 18:11:35 -07:00
David Zarzycki
95473a10d7 [Misc] NFC: Fix random build warnings
Unused variables/methods, language extensions, extra semicolons, intentional
self assignment, platform specific quirks, etc.
2018-04-30 12:52:43 -04:00
Huon Wilson
7c435adc94 [GSB] withoutRedundantSubpath should leave Concrete RequirementSource as Concrete.
Fixes rdar://problem/39316039 and https://bugs.swift.org/browse/SR-7397.
2018-04-12 17:09:10 +10:00
Slava Pestov
75db43b978 AST: Replace ProtocolType::compareProtocols() with TypeDecl::compare() 2018-04-06 15:41:07 -07:00
Huon Wilson
6e39fcf98c [GSB] Use addConditionalRequirements for all conditional-requirement-adding. 2018-03-28 16:59:52 +11:00
Doug Gregor
b2b69e8abf Rename BoundNameAliasType to NameAliasType.
NameAliasType is dead! Long live NameAliasType!
2018-03-25 21:35:17 -07:00
Doug Gregor
e82e7ee908 [Type checker] Use BoundNameAliasType for all typealiases.
Rather than relying on the NameAliasType we get by default for references
to non-generic typealiases, use BoundNameAliasType consistently to handle
references to typealiases that are formed by the type checker.
2018-03-25 21:35:16 -07:00
Doug Gregor
ff5dc6081c [GSB] Infer requirements from protocol inheritance and where clauses.
Fixes rdar://problem/29231937.
2018-03-22 14:24:35 -07:00
Doug Gregor
728831b2ac [GSB] Infer requirements from uses of generic typealiases.
Generic typealiases can add requirements that aren't used by their
underlying type. For example, CountableRange in the standard library:

  public typealias CountableRange<Bound: Comparable> = Range<Bound>

Perform requirement inference based on uses of generic typealiases,
such that a generic function like this:

  func f<T>(_: CountableRange<T>) { }

will infer T: Bound from the use of CountableRange.

Note that this does not yet work for extensions.
2018-03-22 11:05:09 -07:00
Doug Gregor
59077bdfc6 [GSB] Allow redundant inheritance for all Objective-C-defined protocols.
Swift complains about redundant inheritance of a protocol, and
canonicalizes away such redundancies in its metadata. Clang does not
warn about such redundancies, nor does the Objective-C "conforms to
protocol" check take inheritance into account. Extend the existing
"redundant inheritance" hack (designed for JSExport) to cover all
protocols defined in Objective-C, so we match Clang's output of
Objective-C metadata.

Fixes SR-7130 / rdar://problem/38394637.
2018-03-19 10:07:07 -07:00
Doug Gregor
4a358bafb5 [GSB] Merge layout constraints when merging equivalence classes.
Fixes SR-7168 / rdar://problem/38394648.
2018-03-16 13:11:49 -07:00
Doug Gregor
5b03b8da3e Merge pull request #15220 from DougGregor/gsb-cleanups
[GSB] Cleanups regarding potential archetypes
2018-03-14 17:48:14 -07:00
Doug Gregor
acef21be66 [GSB] When merging equivalence classes, only merge each named nested type once.
Eliminate some redundant work in the GenericSignatureBuilder by only merging
one nested type per name, rather than all nested types that have that same
name.
2018-03-13 16:23:38 -07:00
Doug Gregor
9031fc3228 [GSB] More cleanup now that nested potential archetypes don't contain concrete types. 2018-03-13 15:46:31 -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
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