Commit Graph

676 Commits

Author SHA1 Message Date
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
Slava Pestov
c08601bee9 RequirementMachine: Implement RewriteSystem::getLocalRules() 2022-03-16 00:58:30 -04:00
Slava Pestov
327e7e580b RequirementMachine: Add importedRules parameter to RewriteSystem::initialize() 2022-03-16 00:58:30 -04:00
Slava Pestov
af443bee0a RequirementMachine: Implement RewriteContext::getRequirementMachine(ProtocolDecl *) 2022-03-16 00:58:30 -04:00
Holly Borla
6b64ac26f4 [RequirementMachine] Suppress redundant requirement warnings for inferred
requirements.
2022-03-15 21:14:43 -07:00
Slava Pestov
aaf84ac6c2 RequirementMachine: Implement RequirementMachine::initWithProtocolSignatureRequirements() 2022-03-15 18:34:54 -04:00
Slava Pestov
f3bcc52e6c RequirementMachine: Rename RequirementMachine::initWithProtocols() 2022-03-15 18:34:54 -04:00
Slava Pestov
564f626f62 RequirementMachine: Rename some RuleBuilder methods for clarity 2022-03-15 18:34:54 -04:00
Slava Pestov
1d8dd94501 RequirementMachine: Refactor RuleBuilder in preparation for rule sharing 2022-03-15 18:34:54 -04:00
Slava Pestov
1f125c3b5b RequirementMachine: Make some methods on RuleBuilder private 2022-03-15 18:34:54 -04:00
Slava Pestov
2a634ba468 RequirementMachine: Optimize RewriteSystem::normalizeRedundantRules() 2022-03-15 13:35:34 -04:00
Slava Pestov
afafede086 RequirementMachine: Support for RewriteStep::Relation in computeNormalForm() 2022-03-15 13:35:34 -04:00
Slava Pestov
86561b8865 RequirementMachine: Generalize RewritePath::replaceRuleWithPath() to replaceRulesWithPaths() 2022-03-15 13:35:34 -04:00
Slava Pestov
f810d3f388 RequirementMachine: Optimize Rewrite{Path,Loop}::computeNormalForm() 2022-03-15 13:35:34 -04:00
Slava Pestov
4df7bf91c5 RequirementMachine: Pack RewriteStep more tightly 2022-03-15 13:35:34 -04:00
Slava Pestov
d4732f5a19 RequirementMachine: Move RewritePath and RewriteLoop methods from HomotopyReduction.cpp to RewriteLoop.cpp 2022-03-15 13:35:34 -04:00
Slava Pestov
755941c21f RequirementMachine: Split off Rule.cpp from RewriteSystem.cpp 2022-03-15 13:35:34 -04:00
Slava Pestov
2ee5aeefee RequirementMachine: InferredGenericSignatureRequest returns parent generic signature if completion fails 2022-03-14 12:33:18 -04:00
Slava Pestov
6a74ad7065 RequirementMachine: Drop GSB compatibility hack involving ErrorTypes
Another hack we can remove now that 'verify' mode skips checking if
there was a conflict.
2022-03-14 12:33:18 -04:00
Slava Pestov
7b01ff60f0 RequirementMachine: Simplify RewriteSystem::processConflicts()
Remove the logic which incorrectly attempted to simulate the
GenericSignatureBuilder's minimization behavior in the presence
of conflicts, since it doesn't matter anymore.
2022-03-14 12:33:18 -04:00
Slava Pestov
13c6611ee8 RequirementMachine: Use recordConflict() in concretizeNestedTypesFromConcreteParent()
Instead of calling markConflicting() directly, use the existing
recordConflict() utility.
2022-03-14 12:33:18 -04:00
Slava Pestov
a8a281d637 RequirementMachine: Use recordConflict() for superclass conflicts
Instead of calling markConflicting() directly, use the existing
recordConflict() utility.
2022-03-14 12:33:18 -04:00
Slava Pestov
f5b3d19703 RequirementMachine: Replace hadError() with getErrors() returning an OptionSet
This gives us more fine-grained information which will be plumbed
through the various requests.
2022-03-14 12:33:18 -04:00
Slava Pestov
1578ab578a RequirementMachine: Refactor requests to not be downstream of the GenericSignatureBuilder
This will make it easier to remove the GenericSignatureBuilder (which
still won't happen for a little while).
2022-03-14 12:33:18 -04:00
Slava Pestov
05aeeff386 RequirementMachine: Reconstitute sugar in trivial cases
The Requirement Machine operates on canonical types internally and erases
sugared types appearing in generic requirements as a result.

For trivial cases like Array<T> vs [T], we can use the existing
TypeBase::reconstituteSugar() utility to produce a more aesthetically-pleasing
generic signature.
2022-03-14 12:33:18 -04:00
Slava Pestov
e0c10d412e RequirementMachine: Tweak debug output 2022-03-14 12:33:18 -04:00
Slava Pestov
762bf1e7d0 RequirementMachine: Refine 'derived via protocol typealias' criterion
Preserves concrete type rules on associated types that were derived
from rules indirectly formed from protocol typealias rules.

That is, if you have a pair of rules in another minimization domain:

    [P].A.[concrete: C] => [P].A
    [Q:T].[P] => [Q:T]

Then completion will introduce a new rule:

    [Q:T].A.[concrete: C] => [Q:T].A

Since this rule is outside of our minimization domain, we don't record
a rewrite loop for it, and it will never become redundant.

Now if we have a rule in our own minimization domain:

    T.[Q:T].A => T.[Q:U]

Then we get a new rule:

    T.[Q:U].[concrete: C] => T.[Q:U]

Make sure we keep this rule around on account of it being derived from
([Q:T].A.[concrete: C] => [Q:T].A).
2022-03-14 12:33:18 -04:00
Slava Pestov
3576318fc7 RequirementMachine: Refactor construction of requirements from rules
The final step in minimization is building Requirements and
ProtocolTypeAliases from the minimal Rules in the RewriteSystem.

Move this to a new file and refactor it a bit to handle
Requirements and ProtocolTypeAliases more consistently.
2022-03-14 12:33:18 -04:00
Slava Pestov
a61f67a0e8 RequirementMachine: Hack to allow protocol typealiases to appear in associated type inheritance clauses
If you have something like

    protocol P {
      typealias A = C

      associatedtype T : A
    }

    class C {}

Then ::Structural resolution of 'A' in the inheritance clause will
produce a DependentMemberType 'Self.A'. Check for this case and
attempt ::Interface resolution to get the correct underlying type.

Fixes rdar://problem/90219229.
2022-03-14 12:33:18 -04:00
Slava Pestov
1f83cd0b49 RequirementMachine: Don't eliminate a rule via a path involving a protocol typealias rule
If a rule is not a protocol typealias rule, and does not contain any unresolved
symbols, do not attempt to eliminate it via a protocol typealias rule.

This fixes a bunch of cases where the RequirementMachine was overly-eager to
remove rules.
2022-03-11 17:28:01 -05:00
Slava Pestov
a259844803 RequirementMachine: Record those rewrite loops involving protocol typealiases 2022-03-11 17:28:01 -05:00
Slava Pestov
5cfee4e9ab RequirementMachine: Stricter invariants in verifyRewriteRules() 2022-03-11 17:28:01 -05:00
Slava Pestov
3641b77c21 RequirementMachine: Split up first pass of homotopy reduction into two
Add a separate pass to consider rules with unresolved names which are
not simplified.
2022-03-11 17:28:01 -05:00
Slava Pestov
f082c02e42 RequirementMachine: Plumb through the loop candidate to isRedundantRuleFn 2022-03-11 17:28:01 -05:00