Commit Graph

859 Commits

Author SHA1 Message Date
Holly Borla
a74717ab69 [RequirementMachine] Diagnose non-protocol, non-class conformance/subtype
constraints in the requirement machine.
2022-02-16 13:11:56 -08:00
Slava Pestov
426df360b0 RequirementMachine: More elimination order tweaking
- Prefer rewrite loops not involving Decompose
- Prefer more deeply nested concrete types
2022-02-15 04:02:46 -05:00
Slava Pestov
57d89a6f11 RequirementMachine: Use processTypeDifference() in simplifyLeftHandSideSubstitutions() 2022-02-15 04:02:46 -05:00
Slava Pestov
f32b545283 RequirementMachine: Move RewriteSystem::processTypeDifference() and friends from PropertyUnification.cpp to RewriteSystem.cpp 2022-02-15 04:02:46 -05:00
Slava Pestov
12ba9bf1ab RequirementMachine: Move processTypeDifference() method and friends from PropertyMap to RewriteSystem 2022-02-15 04:02:46 -05:00
Slava Pestov
c6119acb8f RequirementMachine: Generalize concrete unification a bit to allow sharing code with concrete simplification 2022-02-15 04:02:46 -05:00
Slava Pestov
eab371d8c5 RequirementMachine: Simplify RewriteStep::Decompose
The inverted form no longer has to replace substitutions; we use
the inverted form of DecomposeConcrete for that. So just assert
that the substitutions are equal.
2022-02-15 04:02:46 -05:00
Slava Pestov
41a6f9e118 RequirementMachine: Remove old implementation of simplifySubstitutions() 2022-02-15 04:02:46 -05:00
Slava Pestov
5e54a52c0e RequirementMachine: Remove old implementation of simplifyLeftHandSideSubstitutions()
Now that the PropertyMap to the concrete simplification version is optional,
we can just pass nullptr here to get the old behavior where type terms are
simplified to canonical anchors and no concrete simplification is performed.
2022-02-15 04:02:46 -05:00
Slava Pestov
2e3cb17666 RequirementMachine: Refactor concrete substitution pass 2022-02-15 04:02:46 -05:00
Slava Pestov
e478ced1ce RequirementMachine: Move some code into a new SimplifySubstitutions.cpp 2022-02-15 04:02:46 -05:00
Slava Pestov
0deacdc7b1 RequirementMachine: Fix silly typo in RewriteSystem::findRuleToDelete()
This was regressing a couple of validation tests after the
seemingly-unrelated introduction of protocol typealias support.
2022-02-13 09:50:16 -05:00
Slava Pestov
530bee5235 RequirementMachine: Loosen check in RewriteSystem::verifyRewriteRules() to allow protocol typealias rules 2022-02-13 00:24:23 -05:00
Slava Pestov
4a06e21bf9 RequirementMachine: Introduce rules for protocol typealiases from requirement signatures
When building a rewrite system for a generic signature or protocol,
we would add requirements from the requirement signature of every
protocol dependency. Now, also add protocol typealias rules as well.
2022-02-13 00:24:23 -05:00
Slava Pestov
a1c03db381 AST: Generalize ProtocolDecl::getRequirementSignature() to a new RequirementSignature type
The RequirementSignature generalizes the old ArrayRef<Requirement>
which stores the minimal requirements that a conforming type's
witnesses must satisfy, to also record the protocol typealiases
defined in the protocol.
2022-02-13 00:24:23 -05:00
Slava Pestov
c58d9d8974 Sema: StructuralRequirementsRequest collects protocol typealiases
Introduces rewrite rules for typealiases defined inside the protocol
itself.

Typealiases defined in protocol extensions do not participate
in the rewrite system, except for when they have the same name as
an associated type, as per the weird rule codified in the
TypeAliasRequirementsRequest for GSB compatibility.
2022-02-13 00:24:23 -05:00
Slava Pestov
fa65fd0e05 RequirementMachine: Plumb protocol typealiases through minimization
Now that we can detect protocol typealias rules, collect and keep
track of them so that they can be recorded in protocol requirement
signatures.

For now, this is all NFC since nothing introduces such rules into
the rewrite system, except for invalid requirements which are
diagnosed anyway.
2022-02-13 00:24:23 -05:00
Slava Pestov
f9c2d9fd7c RequirementMachine: Introduce Rule::isProtocolTypeAliasRule()
There are two kinds of protocol typealiases:

1) The underlying type is a type parameter. These rules look like
   [P].A => X where X is the underlying type.

2) The underlying type is a concrete type. These rules look like
   [P].A.[concrete: C] => [P].A.

The isProtocolTypeAliasRule() predicate detects both cases and
returns the type alias name ('A', in the above examples). For now
it's not used anywhere, since we don't actually introduce these
rules for any reason.
2022-02-13 00:24:23 -05:00
Slava Pestov
cc3d02b975 RequirementMachine: Reduction order on terms compares number of name symbols first
We want a term with more name symbols to order after a term with
fewer name symbols, even if the term with more name symbols is
shorter. That is,

    [P].A > [P:X].[Q:Y].[R:Z]

This is in support of handling protocol typealiases.
2022-02-13 00:24:23 -05:00
Slava Pestov
8406d44db8 RequirementMachine: Allow MutableTerm::rewriteSubTerm() to make the term longer
I'm about to change the reduction order so that terms with more
name symbols always order after terms with fewer name symbols,
so for example

    [P].A > [P:T].[Q:U].[R:V]

This will be used to implement protocol typealiases.
2022-02-13 00:24:23 -05:00
Slava Pestov
65fda8092f RequirementMachine: Small fix for debug output 2022-02-13 00:24:23 -05:00
Slava Pestov
d4a4540494 RequirementMachine: Fix name lookups for the internal 'where' clause of a GenericParamList
These come up in SIL and in the experimental named opaque result types
implementation. Pass the correct DeclContext here, instead of using
the ModuleDecl.
2022-02-10 17:23:54 -05:00
Slava Pestov
2d066dd7ed RequirementMachine: Do a better job of filtering out invalid superclass and concrete type requirements
In the long run it doesn't really matter if we leave them in there, but for now
matching the GenericSignatureBuilder's behavior makes the tests pass with
-requirement-machine-inferred-signatures=verify.
2022-02-10 17:23:54 -05:00
Slava Pestov
d64fb39514 RequirementMachine: Introduce conditional requirements in desugaring 2022-02-10 17:23:54 -05:00
Slava Pestov
48ca87ddeb RequirementMachine: Handle type parameters in @_differentiable requirement inference 2022-02-10 17:23:54 -05:00
Slava Pestov
915ae67974 RequirementMachine: Factor out lookupMemberType() utility in RequirementLowering.cpp 2022-02-10 17:23:54 -05:00
Slava Pestov
e445335eb3 RequirementMachine: Record rewrite loops during superclass unification
This is the second half of rdar://problem/88134910.

Also addresses rdar://problem/25065503.
2022-02-09 22:44:06 -05:00
Slava Pestov
94b55bd408 RequirementMachine: Factor out PropertyMap::unifyConcreteTypes() from ::addConcreteTypeProperty() 2022-02-09 16:49:24 -05:00
Slava Pestov
d39c667e26 RequirementMachine: New elimination order based on projection count
We want to prefer to eliminate rules that are not concrete type rules,
unless non-concrete type rule in question is a projection. For example,
suppose that this rule comes from a protocol:

    T.T == G<T.U, T.V>

And these three rules are in our minimization domain:

    a) T.T == G<Int, W>
    b) T.U == Int
    c) T.V == W

Then a) implies both b) and c), and vice versa.

In this case, we want to keep T.U == Int and T.V == W, and eliminate
T.T == G<Int, W>.

T.T == G<Int, W> is concrete and T.V == W is not, but because T.V == W
is defined by a projection, we still prefer to eliminate T.T == G<Int, W>
over T.V == W.
2022-02-09 11:31:35 -05:00
Slava Pestov
aeceda3cfa RequirementMachine: Introduce RewriteLoop::getProjectionCount() 2022-02-09 10:45:09 -05:00
Slava Pestov
6685e55135 RequirementMachine: Print loop numbers in RewriteSystem::dump() 2022-02-09 10:45:09 -05:00
Slava Pestov
fcd41a01af RequirementMachine: Fix introduction of same-type requirement for recursive concrete witness 2022-02-09 01:09:00 -05:00
Slava Pestov
0f3ec0f2f0 RequirementMachine: Simpler handling of concrete conformances in minimal conformances algorithm
This logic was tricky and unsound.

Correct handling of concrete conformances requires rebuilding the
signature after dropping protocol conformance requiremnts made
redundant by concrete type requirements.

The old support for "derived-via-concrete" requirements in the
minimal conformances algorithm was causing other problems, and it
is the wrong approach anyway, so just remove it.
2022-02-09 01:05:12 -05:00
Slava Pestov
4d1a8dfe23 RequirementMachine: Don't record no-op PrefixSubstitutions rewrite steps 2022-02-09 00:26:04 -05:00
Slava Pestov
50f84cb4bf RequirementMachine: Tweak debug output 2022-02-09 00:26:04 -05:00
Slava Pestov
0cf2881edf RequirementMachine: Simplify Symbol internal storage 2022-02-07 18:57:45 -05:00
Slava Pestov
0d0bcb2ff1 RequirementMachine: Simplify the Symbol API for removal of merged associated types 2022-02-07 18:57:45 -05:00
Slava Pestov
e2e088e082 RequirementMachine: Remove merged associated types from completion 2022-02-07 18:57:45 -05:00
Slava Pestov
868e48cb7a RequirementMachine: Mark rules as simplified in PropertyMap::addConcreteTypeProperty() 2022-02-07 08:20:59 -05:00
Slava Pestov
9e234a09c6 RequirementMachine: Record rewrite loop relating concrete type rules in processTypeDifference()
We can't actually rely on concretelySimplifyLeftHandSideSubstitutions()
to do this for us, because the less-simplified rule (the LHS rule)
might only apply to a suffix of the base term.
2022-02-07 08:20:59 -05:00
Slava Pestov
5bb80286e9 RequirementMachine: Factor out a utility for building a rewrite path unifying to concrete type rules 2022-02-07 08:20:59 -05:00
Slava Pestov
70876171e6 RequirementMachine: Build rewrite paths for concrete unification induced rules 2022-02-07 08:20:59 -05:00
Slava Pestov
9ee702026f RequirementMachine: Factor out TypeDifference::getOriginalSubstitution() 2022-02-07 08:20:59 -05:00
Slava Pestov
60db9174e6 RequirementMachine: Factor out PropertyMap::processTypeDifference() 2022-02-07 08:20:59 -05:00
Slava Pestov
fcd467ad4c RequirementMachine: Factor out TypeDifference::getReplacementSubstitution() 2022-02-07 08:20:59 -05:00
Slava Pestov
2c355de71b RequirementMachine: Introduce RewriteStep::{Left,Right}ConcreteProjection 2022-02-07 08:20:59 -05:00
Slava Pestov
00d226fd2f RequirementMachine: Store the base term in the TypeDifference 2022-02-07 08:20:59 -05:00
Slava Pestov
06d4770adb RequirementMachine: Teach homotopy reduction to better deal with incomparable rules 2022-02-07 08:20:59 -05:00
Slava Pestov
037dc98845 RequirementMachine: Generalize compare() methods to return None instead of asserting on incomparable symbols 2022-02-07 08:20:59 -05:00
Slava Pestov
37be2d5dd7 RequirementMachine: Emit a diagnostic note with the offending rewrite rule if completion failed
This surfaces an implementation detail, but it might be better
than nothing.
2022-02-07 08:20:59 -05:00