Commit Graph

753 Commits

Author SHA1 Message Date
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
Graydon Hoare
b5292f04f8 Add an assortment of new "always-on" metrics. 2017-04-28 13:56:13 -07:00
Doug Gregor
e858c553cb [GSB] Start inferring same-type requirements from inherited type declarations.
Infer same-type requirements among same-named associated
types/typealiases within inherited protocols. This is more staging; it
doesn't really have teeth until we stop wiring together these types as
part of lookup.
2017-04-25 16:13:48 -07:00
Doug Gregor
de66b0c25c [GSB] Warn about redeclarations of associated types from inherited protocols.
Introduce a warning about redeclaring the associated types from an
inherited protocol in the protocol being checked:

* If the new declaration is an associated type, note that the
  declaration could be replaced by requirements in the protocol's
  where clause.
* If the new declaration is a typealias, note that it could be
  replaced by a same-type constraint in the protocol's where clause.
2017-04-24 07:55:42 -07:00
swift-ci
930c488ff6 Merge pull request #8893 from practicalswift/gardening-20170420b 2017-04-20 16:18:09 -07:00
Doug Gregor
583d567705 Merge pull request #8888 from DougGregor/the-subsequence-of-my-subsequence
[Stdlib] For Collections, the SubSequence of a Subsequence is SubSequence
2017-04-20 15:46:37 -07:00
Doug Gregor
65c8334b99 [GSB] Hack: self-derived sources are showing up very late; ignore them.
There's a more pervasive issue here that needs further study;
essentially, we can end up with self-derived sources getting
reinjected later, and will either need to avoid that reinjection or
need to expand what this localized hack does, filtering out
self-derived constraints when we enumerate requirements.
2017-04-20 13:11:21 -07:00
practicalswift
797c2d8118 [gardening] Fix end of namespace comments 2017-04-20 22:01:01 +02:00
practicalswift
431e5a1440 [gardening] Use consistent end of namespace comments 2017-04-20 13:47:10 +02:00
Slava Pestov
16b033c111 Merge pull request #8876 from slavapestov/subclass-existentials-sema-casts
Update Sema cast checks for subclass existentials... and more!
2017-04-20 01:28:53 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
Doug Gregor
a926beab5b [GSB] Hack to limit recursion in the type graph.
This stops after 5 recurrences of the same associated type. It is a
gross hack and a terrible idea, here as a placeholder to prevent us
from running off the rails in ill-formed code. This will go away when
we get further along the path with recursive protocol constraints.
2017-04-20 00:09:23 -07:00
Doug Gregor
b91bd28aba [GSB] Use precise potential-archetype identity when forming protocol-requirement sources.
When we’re determining what type should be used to key a ProtocolRequirement based on the source’s potential archetype and the target’s potential archetype, use potential archetype identity (==) rather than equivalence-class membership to cover the basis case. This ensures that interesting identity relationships in the enclosing context (e.g., the current GenericSignatureBuilder) don’t cause us to compute incorrect stored types.
2017-04-19 23:15:32 -07:00
Doug Gregor
eba4affe3e [GSB] Don’t drop derived-via-concrete constraints too early.
When computing the connected components of within an equivalence class (based on derived same-type requirements), we might need derived-via-concrete constraints to maintain connectedness of the larger graph. Therefore, delay their removal until after we’ve computed connected components.
2017-04-19 08:42:05 -07:00
Doug Gregor
b676e43889 [GSB] Use RequirementSource::isProtocolRequirement() more. 2017-04-19 08:42:05 -07:00
Doug Gregor
e48f4191ca [GSB] Add EquivalenceClass::dump(). NFC 2017-04-19 08:42:05 -07:00
Doug Gregor
98dbd23fe2 [GSB] Add RequirementSource::isProtocolRequirement() to check both protocol-requirement kinds.
It’s too easy to forget to check both ProtocolRequirement and InferredProtocolRequirement, so abstract the check into a method.
2017-04-18 15:47:40 -07:00
Doug Gregor
54f132c487 [GSB] Delete all of the "visited" sets, which are now unused.
Now that we detect recursion based on repetition within the potential
archetypes, we no longer rely on passing down "visited" sets to
detect/diagnose recursion. Remove them.
2017-04-17 23:13:21 -07:00
Doug Gregor
a5a162a17c [GSB] Install basic recursion checking.
Recursive protocol conformances still aren't functional, so reinstate
some checking for recursive protocol conformances. It doesn't catch
everything that the previous version did---but we don't crash on such
cases anymore, either.
2017-04-17 23:13:21 -07:00