Commit Graph

234 Commits

Author SHA1 Message Date
Doug Gregor
f5e9ca57dd [GSB] Inline getGenericSignature() into it’s only caller.
It’s small and straightforward. NFC
2017-09-25 08:47:40 -07:00
Doug Gregor
a048194041 [GSB] Tighten up interfaces for computing a generic signature.
Funnel all places where we create a generic signature builder to compute
the generic signature through a single entry point in the GSB
(`computeGenericSignature()`), and make `finalize` and `getGenericSignature`
private so no new uses crop up.

Tighten up the signature of `computeGenericSignature()` so it only works on
GSB rvalues, and ensure that all clients consider the GSB dead after that
point by clearing out the internal representation of the GSB.
2017-09-25 08:47:40 -07:00
Doug Gregor
32260c8d00 [GSB] Simplify some interfaces. NFC 2017-09-25 08:47:39 -07:00
Doug Gregor
122d07b063 [GSB] Centralize lookup of nested types.
We had two similar loops that performed name lookup for nested types on a
potential archetype, so consolidate those into a single implementation on
the equivalence class itself.
2017-09-25 08:47:39 -07:00
Joe Shajrawi
00f44ce24a Revert "Create fewer generic signature builders" 2017-09-22 21:57:53 -07:00
Doug Gregor
3a3e8874d5 [GSB] Cache the nested types of an equivalence class.
The GSB performs repeated lookups of the same nested type (by name) within
a given equivalence class. Cache the results of this lookup.
2017-09-22 17:11:06 -07:00
Doug Gregor
76a532b3af [GSB] Move a well-formed GenericSignatureBuilder to be the canonical builder.
Once we compute a generic signature from a generic signature builder,
all queries involving that generic signature will go through a separate
(canonicalized) builder, and the original builder can no longer be used.
The canonicalization process then creates a new, effectively identical
generic signature builder. How silly.

Once we’ve computed the signature of a generic signature builder, “register”
it with the ASTContext, allowing us to move the existing generic signature
builder into place as the canonical generic signature builder. The builder
requires minimal patching but is otherwise fully usable.

Thanks to Slava Pestov for the idea!
2017-09-22 17:11:05 -07:00
Doug Gregor
ca4b83b40e [GSB] Inline getGenericSignature() into it’s only caller.
It’s small and straightforward. NFC
2017-09-22 11:32:26 -07:00
Doug Gregor
115d81a327 [GSB] Tighten up interfaces for computing a generic signature.
Funnel all places where we create a generic signature builder to compute
the generic signature through a single entry point in the GSB
(`computeGenericSignature()`), and make `finalize` and `getGenericSignature`
private so no new uses crop up.

Tighten up the signature of `computeGenericSignature()` so it only works on
GSB rvalues, and ensure that all clients consider the GSB dead after that
point by clearing out the internal representation of the GSB.
2017-09-22 11:32:26 -07:00
Doug Gregor
56a3ea8d10 [GSB] Simplify some interfaces. NFC 2017-09-22 11:32:25 -07:00
Doug Gregor
9bdeff2e3d [GSB] Centralize lookup of nested types.
We had two similar loops that performed name lookup for nested types on a
potential archetype, so consolidate those into a single implementation on
the equivalence class itself.
2017-09-22 11:32:25 -07:00
Doug Gregor
0d53a73f41 [GSB] Collapse same-type components using delayed requirements.
Perform the actual "collapse" of same-type components by looking
through the delayed requirements of an equivalence class.
2017-09-12 06:20:38 -07:00
Doug Gregor
43f3833b96 [GSB] Process same-type requirements needed to collapse same-type components.
Same-type constraints are rederived based on the potential archetypes
within an equivalence class and the same-type constraints that link
them. In some cases, there may be parts that connect those same-type
constraints that are stored in "delayed" requirements (for which one
or both end-points have not been realized in a potential archetype);
consider those as well for connectedness.

In a sense, this is an artifact of the generic signature builder's
approach of realizing potential archetypes too readily, and this can
likely be simplified in the future. For now, it's important for the
minimization of same-type constraints in generic signatures.
2017-09-12 06:14:25 -07:00
Doug Gregor
9b54f6a585 [GSB] Dump delayed requirements to aid in debugging. NFC 2017-09-12 06:14:25 -07:00
Doug Gregor
56a07d2e73 [GSB] Split out the operation to expand the requirement signature of a conformance.
NFC for now; we are going to re-use this code.
2017-09-12 06:14:25 -07:00
Doug Gregor
70d4e12285 [GSB] Introduce resolveEquivalenceClass() and start using it.
Queries through the GenericSignatureBuilder about a particular type
parameter only really need information about the equivalence class in which that type parameter resides. Introduce a new entry point
GenericSignatureBuilder::resolveEquivalenceClass() that only

For now, resolveEquivalanceClass() is a thin layer over
resolveArchetype().
2017-09-08 21:45:09 -07:00
Doug Gregor
71eaa5d773 [GSB] Add visualization of same-type constraints in an equivalence class.
Introduce support for writing a GraphViz graph describing the
same-type constraints within an equivalence class. This visualization
helps debugging the minimization algorithm.

LLVM's GraphWriter is actually kinda awful, so there is some hackery
here both to dodge an overzealous static assertion and to munge the
output into an undirected graph.

NFC except for debugging.
2017-08-16 20:29:04 -07:00
Doug Gregor
bb0595b1a2 [GSB] Drop unused parameter to FloatingRequirementSource::forNestedTypeNameMatch. 2017-08-16 11:07:46 -07:00
Doug Gregor
ac5d9fbe53 [GSB] Factor out the introduction of nested-type-name-match constraints. 2017-08-16 11:05:16 -07:00
Doug Gregor
fb7f4b26d1 [GSB] Maintain written requirement location when removing redundant subpaths. 2017-08-15 17:02:36 -07:00
Doug Gregor
bebac49cee [GSB] Form minimal requirement sources by removing redundant subpaths.
When we detect that a requirement source is self-derived, identify the
redundant subpath and remove it to produce a new, smaller requirement
source that computes the same result. We were doing this form the
limited case where the redundant subpath ended at the end of the
requirement source; generalize that notion.

Fixes SR-5601.
2017-08-10 17:00:07 -07:00
Doug Gregor
9d71b152fe [GSB] Add a requirement source modeling the binding of a concrete type.
When a potential archetype refers to a concrete (non-associated) type
declaration, we bind to that concrete type. Add a new requirement
source kind for this case that is always derived, separating it from
the nested-type-name-match source.

One important aspect of this is that typealiases in protocols that
"override" an associated type an inherited protocol will generate the
same requirement signature as the equivalent protocol that uses a
same-type constraint, making the suppression of the "hey, this is
equivalent to a same-type constraint now!" warning an ABI-preserving
change.

With this, remove a now-unnecessary hack for nested-name-match
requirement sources.
2017-08-10 10:38:13 -07:00
Doug Gregor
d589cb720f [GSB] Retain minimal conformance sources within self-derived sources.
More correctly fix SR-5485: we were retaining self-derived conformance
sources when we shouldn't, which led to spurious "redundant
conformance" diagnostics and (much worse) incorrect minimized generic
signatures. Now, when we detect a self-derived conformance source,
return the minimal source that will derive the same conformance... and
retain that one if it's new.
2017-07-27 11:06:48 -07:00
Doug Gregor
469e9e08a5 SE-0157: Enable recursive protocol constraints by default.
We don't know if they work in general, but specific cases of recursive
protocol constraints are working, and enabling them doesn't break
anything.
2017-07-26 20:57:24 -07:00
Doug Gregor
e7077b10de [GSB] Allow reprocessed requirements to go back into the equivalence classes.
processDelayedRequirements() was putting reprocessed-but-still-unresolved
requirements directly into the global delayed-requirements queue,
which meant that they didn't benefit from the optimization of putting
delayed requirements into the appropriate equivalence class.

Revise the meaning of UnresolvedHandlingKind::ReturnUnresolved (and
change its name to GenerateUnresolved) to still generate the
unresolved constraint, but notify the caller that it remained
unresolved. That way, we can track what happens when reprocessing
requirements (the statistics are *really* useful), but we still get
the optimization from putting delayed requirements onto the
equivalence class that can resolve them.

With this change, type checking the standard library is now only 1%
slower with ``enable-recursive-constraints`.
2017-07-26 16:41:42 -07:00
Doug Gregor
c1eb47ff35 [GSB] Add delayed requirements to the equivalence class that can resolve them.
When a requirement is delayed, we know the equivalence class that
would have to change to make the requirement potentially
resolvable. Record the delayed requirement on that equivalence class.

When an equivalence class is modified for any reason, move all of the
delayed requirements to the global delayed-requirements queue so
they'll be reprocessed. This cuts the number of
reprocessed-but-still-unresolved requirements in half when
type-checking the standard library, taking us from 4x slower to 3x
slower.
2017-07-26 16:41:42 -07:00
Doug Gregor
ceab6ae10f [GSB] Route modifications through EquivalenceClass.
Replace the overly-general bumpGeneration() with a "modified()"
operation on EquivalenceClass, indicating that the given
EquivalenceClass has been modified in some way. There are no
modifications in the system that do not directly affect an equivalence
class.
2017-07-26 16:41:42 -07:00
Doug Gregor
d132f50c9d [GSB] Track the affected equivalence class for an unresolved type.
When we fail to resolve a particular type to a potential archetype,
track which equivalence class would have to change for the resolution
of that type to succeed. For now, this is "just" more bookkeeping.
2017-07-26 16:41:42 -07:00
Doug Gregor
6177f83f01 [GSB] SE-0157: Reprocess delayed requirements when we need a complete PA.
Whenever we need a complete, well-formed potential archetype,
reprocess any delayed requirements, so that we pick up additional
requirements on that potential archetype.

This relies on us tracking a generation count for the GSB instance as
a whole, which gets bumped each time we add some new requirement or
create a new potential archetype, and only actually reprocessing
delayed requirements when the generation count exceeds the point at
which we last processed delayed requirements.

This gets the most basic recursive protocol constraint working
end-to-end and doesn't seem to break things.
2017-07-25 12:56:50 -07:00
Doug Gregor
1b2a2c9b90 [GSB/IRGen] Allow redundant inheritance of the JSExport protocol.
Inheritance of a protocol from JavaScriptCore's JSExport protocol is
used to indicate that the methods and properties of that protocol
should be exported to JavaScript. The actual check to determine
whether a protocol (directly) inherits JSExport is performed via the
Objective-C runtime. Note that the presence of JSExport in the
protocol hierarchy is not sufficient; the protocol must directly
inherit JSExport.

Swift warns about redundant conformance requirements and eliminates
them from the requirement signature (and, therefore, the Objective-C
metadata). This behavior is incorrect for JSExport, because the
conformance is actually needed for this API to work properly.

Recognize a protocol's inheritance JSExport specifically (by
name) when computing the requirement signature of the protocol. When
we find such a redundancy, suppress the "redundant conformance
constraint" diagnostic and add a new (hidden) attribute
@_restatedObjCConformance(proto). The attribute is used only by Objective-C
protocol metadata emission to ensure that we get the expected metadata
in the Objective-C runtime.

Fixes rdar://problem/32674145.
2017-07-24 17:02:34 -07:00
Mark Lacey
ed23eb3761 Revert "SE-0157: Enable recursive protocol constraints by default."
This reverts commit afbdbae9d9.

Commit ded45a6e1c more than triples the
type checking time when building Swift.o, so I am going to revert that ,
and it looks like this needs to be reverted as well if that commit is
reverted.
2017-07-13 16:05:03 -07:00
Doug Gregor
afbdbae9d9 SE-0157: Enable recursive protocol constraints by default.
We don't know if they work in general, but specific cases of recursive
protocol constraints are working, and enabling them doesn't break
anything.
2017-07-13 13:01:11 -07:00
Doug Gregor
d5a55d9676 [GSB] Eliminate "allow unresolved" from PotentialArchetype::getDependentType().
We don't need this now that there are no more unresolved types.
2017-06-30 09:05:27 -07:00
Doug Gregor
84e3efa228 [GSB] Delete the "invalid" bit on potential archetypes.
We no longer set this bit anywhere, so remove it. NFC
2017-06-29 16:52:46 -07:00
Doug Gregor
a24a139d10 [GSB] Rip out all of the code related to unresolved potential archetypes.
All of this is dead code now that we don't use
ArchetypeResolutionKind::AlwaysPartial and, therefore, cannot ever
produce an unresolved potential archetype.
2017-06-29 16:52:46 -07:00
Doug Gregor
e3b09f3850 [GSB] Remove typo correction. NFC
Remove typo correction and all notions of "renamed" potential
archetypes from the generic signature builder. They're no longer used
anywhere.
2017-06-29 16:52:46 -07:00
Doug Gregor
cb93331f66 [GSB] Distinguish archetype resolution kind for getNestedType().
PotentiallArchetype::getNestedType(Identifier...) was using
ArchetypeResolutionKind::AlwaysPartial, even though only one client
(the code that itself handles AlwaysPartial) needed it. Add an
ArchetypeResolutionKind parameter to pass through, updating clients
accordingly.

Eliminates 5 effective uses of AlwaysPartial. Only two left!
2017-06-28 09:41:06 -07:00
Doug Gregor
dd3869739e [GSB] Don't add invalid concrete requirements.
When a concrete requirement is invalid due to the concrete type
lacking a conformance to a particular, required protocol, don't emit
that incorrect requirement---it causes invalid states further down the
line.

Fixes SR-5014 / rdar://problem/32402482.

While here, fix a comment that Huon noticed trailed off into oblivion.
2017-06-23 22:23:46 -07:00
Doug Gregor
a4e35ed151 [GSB] Remove unused form of addSameTypeRequirementDirect() 2017-06-23 10:00:11 -07:00
Doug Gregor
52e52b564b [GSB] Improve handling of conformances resolved by concrete types.
Centralize and simplify the handling of conformance requirements
resolved by same-type-to-concrete requirements in a few ways:

* Always store a ProtocolConformanceRef in via-superclass and
  via-concrete requirement sources, so we never lose this information.

* When concretizing a nested type based on its parent, use the
  via-concrete conformance information rather than performing lookup
  again, simplifying this operation considerably and avoiding
  redundant lookups.

* When adding a conformance requirement to a potential archetype that
  is equivalent to a concrete type, attempt to find and record the
  conformance.

Fixes SR-4295 / rdar://problem/31372308.
2017-06-23 09:34:57 -07:00
Doug Gregor
ffea1b35ca [GSB] Separate out a "structurally derived" requirement source kind.
Rather than abusing the "superclass" requirement source with a null
protocol conformance, introduce a separate "structurally derived"
requirement source kind for structurally-derived requirements that
don't need any additional information, e.g., the class layout
requirement derived from a superclass requirement.
2017-06-23 09:32:03 -07:00
Doug Gregor
fafeec0037 [GSB] Eliminate PotentialArchetype::NestedTypeUpdate.
NestedTypeUpdate was mostly just the internal name for
ArchetypeResolutionKind, but the translation was a bit lossy and there
was no point in having separate enums. Standardize on
ArchetypeResolutionKind, adding a new case (WellFormed) to capture the
idea that we can create a new potential archetype only when we know
there is a nested type with that name---and avoid creating unresolved
potential archetypes.
2017-06-23 09:31:58 -07:00
swift-ci
ffcca776a2 Merge pull request #10398 from DougGregor/gsb-unresolved-types 2017-06-20 06:56:12 -07:00
Doug Gregor
fe7ba8b79b [GSB] Pull typo correction for nested types into delayed reqs handling.
Rather than performing typo correction at the very end of finalize(),
do it as part of delayed requirement handling when we cannot otherwise
make progress. This is a cleaner way to cope with typo correction that
gives us a better chance of getting to a sane result.

Fixes rdar://problem/31048352 by eliminating the need for tracking the
number of unresolved potential archetypes altogether. Fixes
rdar://problem/32077627.
2017-06-19 22:47:33 -07:00
Doug Gregor
4860607700 [GSB] Record unresolved potential archetypes as delayed requirements.
Whenever we form a potential archetype that is unresolved (because it
names a member wasn't known at the time the potential archetype was
formed), create a corresponding delayed requirement to resolve the
potential archetype. This ensures that all potential archetypes get a
chance to be resolve, fixing the

  nested type should have matched associated type

assertion in rdar://problem/31401161 (and others).
2017-06-19 22:20:21 -07:00
Doug Gregor
aac05746c0 [GSB] Separate out the kind of DelayedRequirement.
DelayedRequirements were using the general RequirementKind, which was
at best an approximation (we smoothed over the difference between
Superclass and Conformance requirements). Split out the Kind into its
own type, so we can extend it with GSB-specific kinds.
2017-06-19 16:23:54 -07:00
Doug Gregor
a9260f2d9a [GSB] Don't warn about redundancies due to inference from result types.
When we infer a requirement from the result type of a function, don't
warn if that requirement was also stated explicitly. This has been a
point of confusion since we introduced the redundancy warnings,
because users don't consider to result type to be an "input" to the
function in the way the compiler does. So, while technically it is
"correct" to warn, it's unintuitive---so stop.

Fixes SR-5072 / rdar://problem/31357967.
2017-06-16 13:45:07 -07:00
Doug Gregor
4fbb3ec611 [GSB] Resolve nested types by looking into superclass constraints.
This was previously handled very late, by the type checker, which led
to weird ordering dependencies and meant that we could end up with
well-formed code where the GSB was left with unresolved types. We want
such states to never exist, so make sure we can resolve everything in
the GSB.
2017-06-01 22:29:46 -07:00
Doug Gregor
899e68ef09 [GSB] Generalize potential archetype from "typealias" to all concrete types.
Potential archetypes can resolve to either an associated type or a
typealias. Generalize the latter to "any concrete type", both because
the current implementation is unnecessarily narrow (typealiases aren't
actually special in this regard) and to get us closer to handling
lookups via superclass constraints when resolving these types.
2017-06-01 13:52:39 -07:00
Doug Gregor
cf5424b617 [GSB] Move the Recursive(Concrete|Superclass)Type bits into EquivalenceClass.
Recursive concrete and superclass constraints are detected
per-equivalence-class; record them that way.

Use that information to drop recursive concrete and superclass
constraints from the resulting signature, which frees the canonical
generic signature builder from having to worry about such recursive
constraints. This eliminates the invalid-code crashes introduced in
the prior commit that disabled finalization for the canonical GSBs, as
well as fixing one other random crash-on-invalid.
2017-05-15 22:05:07 -07:00