Commit Graph

221 Commits

Author SHA1 Message Date
Doug Gregor
ae5091b09a [GSB] Clean up and audit uses of ArchetypeResolutionKind.
Use ArchetypeResolutionKind::CompleteWellFormed whenever we need to
ask questions about the potential archetype, and
ArchetypeResolutionKind::WellFormed when we need only evaluate whether
there is a legitimate type with that name (and possibly get a handle
to it).
2017-06-30 10:05:12 -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
ac8e8aa1b3 [GSB] Stop creating unresolved potential archetypes; rework typo correction.
The small-but-significant change to the generic signature builder is
to refuse to create unresolved potential archetypes. Instead, delay
any requirement that depends on such type, to be reprocessed once
we've seen all of the other requirements. If the type can be resolved
later, it will be; Otherwise, the type checker will complain when it
sees an unresolvable type. By itself, this fixes the crash in SR-2796.

Doing this by itself regresses diagnostics because typo correction in
the generic signature builder no longer kicks in. Therefore, implement
typo correction for these cases in the type checker proper, using its
existing facilities for typo correction. The result is more consistent
code with a better result.

Fixes SR-2796 / rdar://problem/28544316.
2017-06-29 16:52:46 -07:00
Doug Gregor
623d72db3c [AST] Make the "requirement signature" of a protocol a flat array.
Rather than pretend that the requirement signature of a protocol is a
full, well-formed generic signature that one can meaningfully query,
treat it as a flat set of requirements. Nearly all clients already did
this, but make it official. NFC
2017-06-29 14:01:49 -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
c226ff8dc4 [GSB] Eliminate some duplicated code. NFC 2017-06-28 06:39:46 -07:00
Doug Gregor
be5bc3fd07 [GSB] Eliminate two more uses of ArchetypeResolutionKind::AlwaysPartial.
We don't need to force the creation of potential archetypes when
finding anchors, because new potential archetypes will only be created
by this process in ill-formed generic signatures. Tolerate failure
whenever this happens (for now) and the failure paths will become dead
once AlwaysPartial is eliminated fully.
2017-06-28 06:23:52 -07:00
Doug Gregor
d9b9604326 [GSB] Don't use archetype anchor ordering when merging equivalence classes.
When we merge equivalence classes (by choosing a new representative),
we already prefer the potential archetype with the shorter nesting
depth. However, barring that, we were preferring the potential
archetype that is a better archetype anchor. At one point we depended
on this, but now it's an extra cost that has the side effect of
building more potential archetypes than we really need at this stage.

Eliminate the compareDependentTypes() check when merging equivalence
classes. The intent is that we only need to form archetype anchors
when enumerating requirements or canonicalizing---not during the
normal "solving" path of the GSB.
2017-06-27 22:53:33 -07:00
Doug Gregor
deef22fc7f [GSB] Don't crash when substitution fails to produce a type.
Fixes two more recently-found GSB crashers.
2017-06-26 09:43:13 -07:00
Doug Gregor
239a0f5f4c [GSB] Ensure that we have superclass constraints from merged equiv class.
Fixes one recently-found crasher.
2017-06-26 08:40:40 -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
aeb5b0172b [GSB] When retrieving the archetype anchor, allow partial results.
Specifically, we need to be able to add a new potential archetype for
the anchor. This API might need refinement.
2017-06-23 17:25:52 -07:00
Doug Gregor
c47aea7150 [GSB] Cope with typealiases within protocol hierarchies.
When we see two type(aliase)s with the same name in a protocol
hierarchy, make them equal with an implied same-type requirement. This
detects inconstencies in typealiases across different protocols, and
eliminates the need for ad hoc consistency checking. This is a step
toward simplifying away the need for direct-diagnosis operations
involving concrete type mismatches.

While here, warn when we see an associated type with the same as a
typealias from an inherited protocol; in this case, the associated
type is basically useless, because it's going to be equivalent to the
typealias.
2017-06-23 17:25:45 -07:00
Doug Gregor
b51529f9e7 [GSB] Diagnose all same-type-to-concrete conflicts consistently. 2017-06-23 17:25:37 -07:00
Doug Gregor
bf730fff08 [GSB] Concretize nested types when merging two potential archetypes.
When two potential archetypes are merged and only one of them was a
concrete type beforehand, concretize the nested types in the
equivalence class of the non-concrete potential archetype. Otherwise,
we're liable to miss redundancies.

This feels like an ad hoc extension to an ad hoc mechanism, but gets
us back to parity with this patch series.
2017-06-23 17:25:05 -07:00
Doug Gregor
c879b95917 [GSB] Centralize diagnosis of concrete types and conformance requirements.
Ensures that we don't admit invalid cases where the concrete type does
not conform to the required protocol.
2017-06-23 17:24:02 -07:00
Doug Gregor
791ac7fad4 [GSB] Clean up the meaning of ArchetypeResolutionKind::(Complete)WellFormed. 2017-06-23 17:23:07 -07:00
Doug Gregor
a72a2bf730 [GSB] Avoid recursively growing increasingly-nested potential archetypes.
In some circumstances, we could end up growing increasingly-nested
potential archetypes due to a poor choice of representatives and
anchors. Address this in two places:

* Always prefer to use the potential archetype with a lower nesting
  depth (== number of nested types) to one with a greater nesting
  depth, so we don't accumulate more nested types onto the
  already-longer potential archetypes, and

* Prefer archetype anchors with a lower nesting depth *except* that we
  always prefer archetype anchors comprised of a sequence of
  associated types (i.e., no concrete type declarations), which is
  important for canonicalization.

Fixes SR-4757 / rdar://problem/31912838, as well as a regression
involving infinitely-recursive potential archetypes caused by the
previous commit.
2017-06-23 16:58:56 -07:00
Doug Gregor
b095c8af2a [GSB] Break recursion based on the “root” type we’re creating requirements for. 2017-06-23 10:00:16 -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
Huon Wilson
61b3bff72a [NFC] Reduce duplication/special cases in GSB typo correction. 2017-06-21 14:06:14 -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
6a2b6c271f [AST] Add TypeBase::findUnresolvedDependentMemberType() and use it 3x.
NFC: Eliminate some duplicated "search for an unresolved dependent
member type" code scattered around.
2017-06-15 23:23:07 -07:00
swift-ci
d84d85a817 Merge pull request #10182 from DougGregor/sr-5165 2017-06-09 15:50:27 -07:00
Doug Gregor
610ffd5680 [GSB] Fall back to "abstract" requirements when we don't find a match.
The previous commit tightened up our choice of requirement sources for
superclass and layout constraints, but always assumed a match
existed. In cases where it does not, don't fail; rather, use an
abstract source, which we do elsewhere when we're synthesizing
requirements.

This is a defensive approach to cope with cases where we might not
have precisely the same value within one of our recorded
constraints. This currently only occurs when there are errors (hence
the elimination of the odd "hasError()"), but will come up more often
when we're deriving information from a combination of constraints.
2017-06-09 15:23:06 -07:00
Doug Gregor
969163f6ea [GSB] Only use matching superclass constraints in enumerated requirements.
Fixes SR-5165 / rdar://problem/32658705.
2017-06-09 00:00:17 -07:00
Slava Pestov
c152fee9f8 Sema: A couple of recursion-breaking hacks 2017-06-07 17:19:09 -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
a9733cb8db [GSB] Resolve dependent member types in same-type and superclass constraints.
Once we're finalizing same-type-to-concrete and superclass
constraints, replace any unresolved DependentMemberTypes with their
resolved counterpairs. This allows us to simplify
DependentGenericTypeResolver, which only builds unresolved
DependentMemberTypes now, and eliminates the penultimate use of
ArchetypeResolutionKind::AlwaysPartial.
2017-05-30 11:43:53 -07:00
Huon Wilson
578097c2b6 [GSB] AbstractProtocol sources are explicit when computing req. sig. and not inferred.
If they're considered explicit, requirement inference will complain when
it infers X: SomeProtocol for some concrete type X.

Fixes SR-4693, rdar://problem/31819616
2017-05-24 13:32:01 -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
Doug Gregor
fef69478f6 [GSB] Introduce computeGenericSignature() for generic signature creation.
The GenericSignatureBuilder requires `finalize()` to be called before a
generic signature can be retrieved with `getGenericSignature()`. Most of the former isn’t strictly needed unless you want a generic signature, and the 
latter is potentially expensive. `computeGenericSignature()` combines the two
operations together, since they are conceptually related. Update most of the
callers to the former two functions to use `computeGenericSignature()`.
2017-05-15 17:16:50 -07:00
Doug Gregor
0324920847 [GSB] Cache the archetype anchor of each equivalence class.
Archetype anchors are (re-)computed often, but shouldn't change all
that much. Cache them in the equivalence class for a ~15% speedup
type-checking the standard library.
2017-05-15 17:16:50 -07:00
Doug Gregor
bab127fce0 [GSB] Put archetype anchor consistency checking behind a separate #define.
Put the archetype anchor consistency checking behind a separate
it is really expensive: turning this checking off shaves 5s off the
type-checking time of the standard library in my Release+Asserts build
(~23% speedup).
2017-05-11 16:48:12 -07:00
Doug Gregor
cbccd2d7f3 [GenericSignatureBuilder] Add some simple statistics to track work done. 2017-05-11 11:37:38 -07:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
practicalswift
1924661a55 [gardening] Remove typo 2017-05-04 15:22:23 +02:00
Slava Pestov
e91ab9f978 AST: Rip out some code from GenericSignatureBuilder's inheritance clause processing
I don't fully understand why, but this logic here is redundant
because we end up adding layout constraints implied by types in
the inheritance clause anyway.
2017-05-02 19:24:03 -07:00
Slava Pestov
b0360cebfe AST: Don't build archetypes with an error type as the superclass
This breaks invariants.
2017-05-01 19:30:42 -07:00