Commit Graph

859 Commits

Author SHA1 Message Date
Slava Pestov
86a4c9fdb0 RequirementMachine: Stricter invariants in RewriteSystem::verifyRewriteRules() 2022-03-07 23:20:41 -05:00
Slava Pestov
39d486a7fc RequirementMachine: Try to avoid introducing rules of the form T.[P] => T.[Q]
These rules would be fine since RHS simplification eliminates them,
but they cause problems for the minimal conformances algorithm.

To avoid introducing such rules, ensure that the critical pair of
two property-like rules is itself a property-like rule instead of
relying on subsequent simplifications sorting it out. See the
new comment in RewriteSystem::computeCriticalPair() for details.

I need to understand this problem better and either fix minimal
conformances or add stronger assertions, but for now this fixes
the last failure with -requirement-machine-abstract-signatures=verify.
2022-03-07 23:19:43 -05:00
Slava Pestov
0e4c398048 RequirementMachine: Tweak completion loop condition 2022-03-07 23:13:05 -05:00
Slava Pestov
69672ccfd2 RequirementMachine: Fix debug output 2022-03-07 23:13:05 -05:00
Holly Borla
80a2eee2bf [Diagnostics] Remove a few special cases of printing 'any' for specific
error messages.
2022-03-05 14:26:45 -08:00
Slava Pestov
64f049a3d8 RequirementMachine: Fix ConcreteContraction::substTypeParameter() for non-nominal base type
We can handle a non-nominal base type here as long as the DependentMemberType
we're substituting this into is resolved.
2022-03-03 14:05:02 -05:00
Slava Pestov
589c1a3c11 RequirementMachine: A couple of improvements to concrete contraction
- Allow duplicate concrete type and superclass requirements on the
  same generic parameter, as long as they're identical. This can
  arise if requirement inference infers a requirement which the
  user then explicitly re-states.

- If duplicate requirements are found that name different types, drop
  only that generic parameter from consideration without giving up
  entirely.

- If a generic parameter is subject to both a concrete type and a
  superclass requirement, proceed with the concrete type requirement
  since it is more specific instead of giving up.
2022-03-03 14:05:02 -05:00
Slava Pestov
0d094241d8 RequirementMachine: Fix rogue debug output when debug flag was disabled 2022-03-03 14:05:02 -05:00
Slava Pestov
517fb4568e RequirementMachine: Fix minimal conformances algorithm to cope with unordered concrete conformances 2022-03-03 14:05:02 -05:00
Slava Pestov
b497c790fa RequirementMachine: Check invariants in noassert builds 2022-03-03 14:05:02 -05:00
Slava Pestov
9c2d540e69 RequirementMachine: Check invariants using GenericSignature::verify()
Previously only the GenericSignatureBuilder would call this; to ensure
we get the same test coverage when the Requirement Machine is 'on' as
'verify', also call this from the Requirement Machine.

For now, we can't call this from RequirementSignatureRequestRQM due
to circularity issues; that will have to wait until this request is
folded into RequirementSignatureRequest.
2022-03-03 14:05:02 -05:00
Slava Pestov
bd5a437c39 Merge pull request #41615 from slavapestov/rqm-protocol-superclass-circular-conformance
RequirementMachine: Fix minimization when protocol is constrained to a class that conforms to the protocol
2022-03-02 12:17:12 -05:00
Slava Pestov
5281426569 RequirementMachine: Fix minimization when protocol is constrained to a class that conforms to the protocol
Consider this example:

    protocol P : C {}
    class C : P {}

    <T where T : P>

The GenericSignatureBuilder thinks the minimized signature is
<T where T : P>. The RequirementMachine would minimize it down to
<T where T : C>. The latter is more correct, since the conformance
here is concrete and no witness table needs to be passed in at
runtime, however for strict binary compatibility we need to produce
the same signature as the GenericSignatureBuilder.

Accomplish this by changing the minimal conformances algorithm to
detect "circular concrete conformance rules", which take the form

    [P].[concrete: C : Q]

Where Q : P. These rules are given special handling. Ordinarily a
protocol conformance rule is eliminated before a concrete conformance
rule; however concrete conformances derived from circular
conformances are considered to be redundant from the get-go,
preventing protocol conformances that can be written in terms of
such concrete conformances from themselves becoming redundant.

Fixes rdar://problem/89633532.
2022-03-02 03:13:41 -05:00
Slava Pestov
8570b5656c RequirementMachine: Fix compile error on Linux 2022-03-01 00:56:43 -05:00
Slava Pestov
d3c224bb26 RequirementMachine: RewriteSystem::hadError() should ignore rules outside our minimization domain 2022-02-28 18:43:53 -05:00
Slava Pestov
a57a418724 RequirementMachine: Make getTypeForSymbolRange() more robust when asserts are off 2022-02-28 18:42:30 -05:00
Slava Pestov
048a64d4c0 RequirementMachine: Tweak debug output and fix typo 2022-02-28 18:41:42 -05:00
Slava Pestov
8e09ba8b45 RequirementMachine: Introduce 'concrete contraction' pre-processing pass before building rewrite system
See the comment at the top of ConcreteContraction.cpp for a detailed explanation.

This can be turned off with the -disable-requirement-machine-concrete-contraction
pass, mostly meant for testing. A few tests now run with this pass both enabled
and disabled, to exercise code paths which are otherwise trivially avoided by
concrete contraction.

Fixes rdar://problem/88135912.
2022-02-25 11:48:38 -05:00
Slava Pestov
bbbbfbac89 RequirementMachine: Fix assertion when protocol Self is constrained to a non-conforming type
The conflicting rule here is the permanent 'identity conformance'
([P].[P] => [P]). Permanent rules cannot be marked as conflicting,
so just check for this condition first.
2022-02-25 11:48:38 -05:00
Slava Pestov
aa210d29ae RequirementMachine: Prefer to eliminate concrete type requirements over superclass requirements again
If you have something like this:

    protocol P {
      associatedtype A : Q where Self == Self.A.B
    }

    protocol Q {
      associatedtype B
    }

    class C : P {
      typealias A = D
    }

    class D : P {
      typealias B = C
    }

The GSB would minimize the generic signature <T where T : P, T : C>
to <T where T == C>, because of the same-type requirement in
protocol P.

However in reality, the conformance 'C : P' is unsound, because it
is no longer covariant. I added a warning in commit d831eff74879cb.

Because the upcoming 'concrete contraction' pass eliminates 'T : P'
before homotopy reduction gets a chance to run, I'm changing the
Requirement Machine to produce a different minimization from the
GSB. This is technically an ABI break, but it should not impact any
real code in practice. If it does, I'll need to come up with a
different workaround in concrete contraction.
2022-02-25 11:48:38 -05:00
Slava Pestov
ae1ef6d50d RequirementMachine: Eliminate RequirementMachine::initWithAbstractRequirements() 2022-02-23 00:20:57 -05:00
Slava Pestov
e326c01f9b RequirementMachine: Write some comments 2022-02-23 00:20:57 -05:00
Slava Pestov
a3ab5f4cd4 RequirementMachine: Remove no-longer used DebugFlags::Merge 2022-02-23 00:17:56 -05:00
Slava Pestov
c75337c986 RequirementMachine: Remove lookupMemberType() in favor of existing substBaseType() 2022-02-18 22:24:25 -05:00
Slava Pestov
d7f8f2067a Sema: Generalize ProtocolDecl::getPrimaryAssociatedType() to ProtocolDecl::getPrimaryAssociatedTypes() 2022-02-18 22:24:25 -05:00
Slava Pestov
7e5d6f4cb0 AST: Rework ParameterizedProtocolTypes to store multiple argument types
For now, this is NFC since we still assume one argument elsewhere.
2022-02-18 18:22:20 -05:00
Slava Pestov
a69ac78482 RequirementMachine: Rework PropertyMap::recordConflict() for GSB compatibility
We want to prefer explicit rules when an explicit rule and non-explicit
rule conflict. Since the explicit bit hasn't been computed yet when
building the property map, save conflicting rule pairs in a side table,
and then process them later in homotopy reduction.

The side table will also be useful for diagnostics later.
2022-02-17 19:40:58 -05:00
Slava Pestov
966620bbaa RequirementMachine: Swap reduction order of ConcreteType and Superclass symbols
This ensures we produce the same minimal signature as the GSB
in a couple of really silly examples where a superclass requirement
and a concrete type requirement imply each other.
2022-02-17 19:40:58 -05:00
Slava Pestov
93727490df RequirementMachine: Remove old PropertyMap::unifyConcreteTypes() 2022-02-17 19:40:58 -05:00
Slava Pestov
b90dae1f15 RequirementMachine: Unify every pair of superclass requirements that apply to a term 2022-02-17 19:40:58 -05:00
Slava Pestov
2f16ca38e5 RequirementMachine: Unify every pair of concrete type rules that apply to a term
Instead of keeping track of the best one so far, and unifying subsequent rules
against it.

This allows us to record more identities, fixing a regression from an earlier
change where we were unable to eliminate an obviously-redundant rule.
2022-02-17 19:40:58 -05:00
Slava Pestov
ee410b4fe3 RequirementMachine: Another elimination order hack 2022-02-17 19:40:58 -05:00
Slava Pestov
86d47d951d RequirementMachine: Re-visit substitution-simplified rules for concrete unification 2022-02-17 19:40:58 -05:00
Slava Pestov
46d07fc739 RequirementMachine: Add new assertion to ConcreteTypeMatcher::verify() 2022-02-17 19:40:58 -05:00
Slava Pestov
e8503021fd RequirementMachine: Add a check to RewriteSystem::verifyMinimizedRules() 2022-02-17 19:40:58 -05:00
Slava Pestov
ef1292e1ce RequirementMachine: Don't filter out redundant superclass conformances when building archetypes
This was a hack to maintain exact compatibility with the GSB's
results when constructing archetypes, but it no longer appears
to be necessary.
2022-02-17 19:40:58 -05:00
Slava Pestov
6509eff0f2 Merge pull request #41412 from slavapestov/rqm-preserve-redundant-rule-paths
RequirementMachine: Preserve replacement paths for redundant rules
2022-02-16 22:26:50 -05:00
Slava Pestov
991fe1dbd8 RequirementMachine: Preserve replacement paths for redundant rules 2022-02-16 16:28:08 -05:00
Slava Pestov
5404754cb7 RequirementMachine: More concise printed output for concrete type and superclass symbols
Types cannot contain Terms, so the Symbol representation uses
GenericTypeParameterTypes whose index refers to an array of
"substitution" Terms.

We can (ab)use the PrintOptions.AlternateTypeNames mechanism
to print those GenericTypeParamTypes as if they were Terms.
2022-02-16 16:27:40 -05:00
Holly Borla
1dddd9c047 [RequirementMachine] Move gating emitting diagnostics on the requirement
macine flags to callers.

Callers gate this decision on different flags; InferredGenericSignatureRequestRQM
needs RequirementMachineInferredSignatures to be enabled, and the other
callsites need RequirementMachineProtocolSignatures to be enabled.
2022-02-16 13:11:57 -08:00
Holly Borla
04a91fe61d [RequirementMachine] Suppress requirement diagnostics when one of the
types already has an error.
2022-02-16 13:11:57 -08:00
Holly Borla
d5814ae3e5 [RequirementMachine] Only emit a redundancy warning for layout constraints
if the type is constrained to a class layout, and add a few more test cases
for type requirement conflicts.
2022-02-16 13:11:57 -08:00
Holly Borla
a992917f1c [NFC][RequirementMachine] Rename some of the RequirementError::Kind cases,
add documentation for each case, and move RequirementError to
lib/AST/RequirementMachine/Diagnostics.h.
2022-02-16 13:11:57 -08:00
Holly Borla
1f1125098b [NFC][RequirementMachine] Add documentation for diagnoseRequirementErrors. 2022-02-16 13:11:57 -08:00
Holly Borla
e6dc7e69c1 [RequirementMachine] Emit diagnostics for structural requirements in protocol
and typealias requirement signatures.
2022-02-16 13:11:57 -08:00
Holly Borla
ec45d960d8 [RequirementMachine] Always store an invalid requirement in RequirementError. 2022-02-16 13:11:57 -08:00
Holly Borla
3837b4def4 [RequirementMachine] Diagnose early redundant requirements. 2022-02-16 13:11:57 -08:00
Holly Borla
203734bea4 [RequirementMachine] Diagnose conformance, layout, and superclass constraints
on non-type-parameters in the requirement machine.
2022-02-16 13:11:57 -08:00
Holly Borla
e4cba889c2 [RequirementMachine] Diagnose concrete same-type mismatches, such as
`where String == Int`.
2022-02-16 13:11:57 -08:00
Holly Borla
3c2b588037 [RequirementMachine] Plumb the requirement errors vector through desugarRequirement. 2022-02-16 13:11:57 -08:00