Commit Graph

859 Commits

Author SHA1 Message Date
Slava Pestov
08aab383e8 RequirementMachine: Don't record rewrite loops for protocol signature machines
A requirement machine built from existing protocol requirement signatures only
exists to cache those rules and allow them to be inherited by other machines.

Recording loops is unnecessary in this case, since no minimization will be
performed.
2022-03-26 00:56:41 -04:00
Slava Pestov
4d097da73c RequirementMachine: Re-use requirement machines constructed by minimization for queries
Fixes rdar://problem/88135641.
2022-03-26 00:56:41 -04:00
Slava Pestov
3d2559f79e RequirementMachine: Don't skip subst-simplified rules when importing
Property map construction is still not incremental, and considers
all rules. Skipping subst-simplified rules here violates invariants
checked by the next commit.
2022-03-26 00:56:41 -04:00
Slava Pestov
3777054c1c RequirementMachine: Fix dump() output for a recent change
Protocol machines now have the 'Self' generic parameter in 'Params', so
we have to change the order of the two 'if' statements.
2022-03-26 00:56:41 -04:00
Slava Pestov
af99ac55de RequirementMachine: Add -debug-requirement-machine=timers 2022-03-26 00:56:41 -04:00
Slava Pestov
6110866065 RequirementMachine: Propagate requirement IDs only once 2022-03-25 22:28:11 -04:00
Slava Pestov
06a25fa452 RequirementMachine: Don't normalize replacement paths for redundant rules 2022-03-25 22:28:11 -04:00
Slava Pestov
d8215bb155 RequirementMachine: Rework 'implied rules' computation in computeRedundantRequirementDiagnostics()
For efficiency I want to keep replacement paths for redundant rules
unsubstituted, so that earlier replacement paths can reference
redundant rules that appear later in the RedundantRules array.

Right now we expand replacement paths so that their RewriteSteps
only mention non-redundant rules.

This patch refactors the computeRedundantRequirementDiagnostics()
method a bit:

The impliedRequirements set is now named nonExplicitNonRedundantRules,
and in addition to storing these rules themselves, the set also
stores any _redundant_ rules that reference these rules via their
replacement paths.

Since this is computing a transitive closure, we walk the
RedundantRules array in reverse. A replacement path can only
reference a redundant rule if that redundant rule appears later
in the array.
2022-03-25 22:28:03 -04:00
Slava Pestov
81f49a863f Merge pull request #42018 from slavapestov/rqm-assoc-type-inference-cycle
RequirementMachine: Fix subtle bug in isRecursivelyConstructingRequirementMachine()
2022-03-25 11:45:16 -04:00
Slava Pestov
7d0215e6e0 RequirementMachine: Tweak rule limit non-termination heuristic
Add the length of the longest *initial* rule to the rule length limit
before comparing.

Fixes https://bugs.swift.org/browse/SR-16024.
2022-03-25 01:00:49 -04:00
Slava Pestov
3fe4aaae5a RequirementMachine: Fix subtle bug in isRecursivelyConstructingRequirementMachine()
I don't have a reduced test case. It was possible for computing the requirement
signatures of a connected component to have finished, and yet for the
ProtocolDecl::hasComputedRequirementSignature() method to return false, if
we had evaluated a RequirementSignatureRequestRQM but not the top-level
RequirementSignatureRequest.

Instead, track whether we've computed the signatures for a component directly.

I don't have a reduced test case. It would arise with associated type inference,
which uses this predicate to break nasty cycles.
2022-03-24 23:45:32 -04:00
Slava Pestov
57d1600b99 RequirementMachine: Better tests for concrete type requirements with opaque archetypes
Also fix a weird latent bug. In lookupConcreteNestedType(), we would
push nullptr onto the concreteDecls vector if the opaque archetype
did not have a nested type with this name. However, this did not
turn out to be a problem, since in this code path we would only
have a single element in this vector, and the later call to
std::min_element() did not end up dereferencing the null pointer.

However this is very dodgy, so tweak the code to prevent this from
happening and add a test case (which already passed anyway).
2022-03-22 23:53:40 -04:00
Slava Pestov
5c5d2eadf6 Merge pull request #41945 from slavapestov/rqm-split-concrete-equiv-class-improvements
RequirementMachine: Improve the 'split concrete equivalence classes' hack a bit
2022-03-22 20:59:07 -04:00
Slava Pestov
02e402721d RequirementMachine: Print opaque archetypes using the 'stable' representation 2022-03-22 17:20:48 -04:00
Slava Pestov
441fa1679a RequirementMachine: Splitting concrete equivalence classes in protocol requirement signatures 2022-03-22 15:02:06 -04:00
Slava Pestov
ff40f109ca RequirementMachine: Allow RequirementMachine::isConcreteType() and ::getCanonicalTypeInContext() to be used with protocol connected components 2022-03-22 15:02:06 -04:00
Slava Pestov
4446f2afcf RequirementMachine: Move some code out of RuleBuilder and into RequirementSignatureRequest 2022-03-22 15:02:06 -04:00
Slava Pestov
466d6a9468 RequirementMachine: Refactor shouldSplitConcreteEquivalenceClasses() and splitConcreteEquivalenceClasses() a bit
Note that this changes the behavior of a test slightly when the
-disable-concrete-contraction flag is used. This is because we're
not using the Requirement Machine that minimized the signature
and not the Requirement Machine built from the minimized
signature; the former includes a concrete conformance rule.

The isConcreteType() query returns true on the former when
given the generic parameter τ_0_0.

Since -disable-concrete-contraction is only meant for debugging,
I'm just removing that line from the test.
2022-03-22 15:02:06 -04:00
Slava Pestov
11b45ca269 RequirementMachine: splitConcreteEquivalenceClass() uses getConcreteType() instead of getCanonicalTypeInContext() 2022-03-22 15:02:06 -04:00
Slava Pestov
24b6624275 RequirementMachine: Move some code around in RequirementMachineRequests.cpp 2022-03-22 15:02:06 -04:00
Slava Pestov
bf779d31a0 RequirementMachine: Allow query operations to be invoked on requirement machine instances for fresh signatures 2022-03-22 15:02:06 -04:00
Slava Pestov
9ccdd15d58 RequirementMachine: Add upper bound on number of attempts at splitting concrete equivalence classes 2022-03-22 15:02:06 -04:00
Slava Pestov
4069434100 RequirementMachine: Preserve sugar when splitting concrete equivalence classes
Instead of kicking off an AbstractGenericSignatureRequest recursively,
handle the rebuilding in a loop in {Abstract,Inferred}GenericSignatureRequest.

This also avoids an unnecessary call to verify() when rebuilding.
2022-03-22 15:02:06 -04:00
Slava Pestov
ac7efc1de7 Merge pull request #41935 from slavapestov/rqm-concrete-contraction-unrelated-alias
RequirementMachine: Another silly GenericSignatureBuilder compatibility hack for concrete contraction
2022-03-21 22:44:40 -04:00
Slava Pestov
b07b700428 Merge pull request #41936 from slavapestov/rqm-preserve-sugar-fix
RequirementMachine: Try harder to preserve sugar
2022-03-21 18:37:00 -04:00
Slava Pestov
9f9b81b156 RequirementMachine: Explicitly specify SmallVector size 2022-03-21 16:59:03 -04:00
Slava Pestov
dc82433c78 RequirementMachine: Another silly GenericSignatureBuilder compatibility hack for concrete contraction 2022-03-21 16:31:18 -04:00
Slava Pestov
298a84a202 RequirementMachine: Preserve sugar in desugarSameTypeRequirement() 2022-03-21 16:26:40 -04:00
Slava Pestov
cca936e790 RequirementMachine: Don't consider protocol typealiases with UnboundGenericType
These are stealth generic typealiases and should not participate in the
rewrite system.
2022-03-21 12:19:29 -04:00
Slava Pestov
2206b1c54d RequirementMachine: Split up equivalence classes with inferred concrete type
I'll clean this up, comment it and generalize it to work with protocol requirement
signatures soon. Landing this now to unblock the Linux corelibs-foundation build.

Fixes rdar://problem/89791117.
2022-03-18 01:29:22 -04:00
Slava Pestov
e6d1ef9f6d Merge pull request #41868 from slavapestov/rqm-opaque-archetypes
RequirementMachine: Opaque archetype support (sort of)
2022-03-17 23:06:24 -04:00
Slava Pestov
2f727d6b47 RequirementMachine: Opaque archetype support (sort of)
Complete support is behind a flag, because it can result in a non-convergent
rewrite system if the opaque result type has a recursive conformance of its
own (eg, `some View` for SwiftUI's View protocol).

Without the flag, it's good enough for simple examples; you just can't have
a requirement that mentions a nested type of a type parameter equated to
the concrete type.

Fixes rdar://problem/88135291, https://bugs.swift.org/browse/SR-15983.
2022-03-17 17:45:59 -04:00
Slava Pestov
4d531aea9c RequirementMachine: Remove PropertyMap::ConcreteConformances 2022-03-17 17:45:52 -04:00
Slava Pestov
1e731d3a18 RequirementMachine: Don't assert on abstract conformance in desugarConformanceRequirement() 2022-03-17 17:45:52 -04:00
Slava Pestov
b0a4efba54 RequirementMachine: Fix lookupConcreteNestedType() to cope with opaque archetypes 2022-03-17 17:45:52 -04:00
Slava Pestov
30da30e0ee RequirementMachine: Rule::isProtocolRefinementRule() allows transitive refinement
Without this, we only considered a protocol refinement rule redundant if
it was derived by directly-stated protocol refinements. But this broke
when completion introduced a 'transitive' refinement [P].[R] => [P]
from two directly-stated refinements [P].[Q] => [P] and [Q].[R] => [Q].

Fixes rdar://problem/90402503.
2022-03-17 15:06:05 -04:00
Slava Pestov
c5cef953bc RequirementMachine: Minimal conformances collects protocol refinement rules from all minimization domains 2022-03-17 15:06:05 -04:00
Slava Pestov
6e6c8c29a5 RequirementMachine: Only check local rules against maximum rule limit
Completion has a maximum rule limit since the Knuth-Bendix algorithm
is a semi-decision procedure that does not terminate on all inputs.

However, this means that generic signatures which import a large
number of complex protocols can hit the limit even if they are
convergent.

Since imported rules are essentially free, ignore them here, to
avoid having to increase the limit by hand.

Now the default limit is 4000 local rules per requirement machine;
it seems implausible that you would exceed this, but if anyone has
an example we can bump the limit again.
2022-03-16 13:26:37 -04:00
Slava Pestov
d90e8122aa RequirementMachine: Factor out duplicated lookupConcreteNestedType() utility 2022-03-16 13:26:37 -04:00
Slava Pestov
ef1636a462 RequirementMachine: Split off RuleBuilder.{cpp,h} from RequirementLowering.{cpp,h} 2022-03-16 12:24:20 -04:00
Slava Pestov
7c006190dd RequirementMachine: Fold getRuleForRequirement() into RuleBuilder::addRequirement() 2022-03-16 12:24:20 -04:00
Slava Pestov
c91c76028f RequirementMachine: Correctly handle importing of protocols in inferConditionalRequirements()
This fixes a regression from the rule sharing implementation; I forgot
to handle the imported rules in builder.ImportedRules here.

This makes the usage of RuleBuilder in conditional requirement
inference look like the other usages of RuleBuilder, except
that the results are passed to RewriteSystem::addRules() instead
of RewriteSystem::initialize().
2022-03-16 12:24:20 -04:00
Slava Pestov
652de97cfd RequirementMachine: Introduce RuleBuilder::initWithConditionalRequirements() 2022-03-16 12:24:20 -04:00
Slava Pestov
fb487f80e7 RequirementMachine: Factor out RewriteSystem::addRules() from RewriteSystem::initialize() 2022-03-16 12:24:20 -04:00
Slava Pestov
c25cd24fe9 RequirementMachine: RewriteSystem::initialize() takes writtenRequirements as an rvalue 2022-03-16 12:24:20 -04:00
Slava Pestov
8176b61d60 RequirementMachine: Tweak debug output and add an assertion 2022-03-16 12:24:10 -04:00
Holly Borla
98beadf8f5 Merge pull request #41833 from hborla/redundant-requirement-tweak
[RequirementMachine] Suppress redundant requirement warnings for inferred requirements.
2022-03-16 09:00:24 -07:00
Slava Pestov
d7f84454c4 RequirementMachine: Filter duplicates in ProtocolDependenciesRequest 2022-03-16 00:58:30 -04:00
Slava Pestov
653977a51c RequirementMachine: Rule sharing
All the pieces are now in place so that the RuleBuilder can assemble
a confluent rewrite system for downstream protocol dependencies,
instead of building rules from protocol requirement signatures.
2022-03-16 00:58:30 -04:00
Slava Pestov
9704168351 RequirementMachine: Skip imported rules in various places when iterating over all rules 2022-03-16 00:58:30 -04:00