Commit Graph

153 Commits

Author SHA1 Message Date
Holly Borla
e6eb5dab79 [Requirement Machine] Stricter verification conditions for element symbols. 2024-07-15 10:19:32 -07:00
Holly Borla
eabaed8a62 [Requirement Machine] Use element symbols for concrete same-element requirements. 2024-07-15 10:19:32 -07:00
Holly Borla
2ea4586580 [Requirement Machine] Implement same-element requirements. 2024-07-15 10:19:32 -07:00
Slava Pestov
273c4b2b1a RequirementMachine: Convert to new assertions 2024-06-22 08:53:22 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Slava Pestov
10a2ddb95e RequirementMachine: Fix MaxConcreteNesting check to take initial rules into account
Fixes rdar://123357717.
2024-03-06 21:42:49 -05:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Slava Pestov
dcca5ced0f RequirementMachine: Remove -warn-redundant-requirements flag 2024-02-02 14:57:19 -05:00
Slava Pestov
6bb74ab701 RequirementMachine: Fix stray } in RewriteSystem::dump() 2023-11-14 15:46:58 -05:00
Slava Pestov
0c6f6db980 RequirementMachine: Add missing closing brace in debug output 2023-07-03 15:41:09 -04:00
Slava Pestov
dca00debec RequirementMachine: Same-type requirements imply same-shape requirements
We want `T.A == U.B` to imply `shape(T) == shape(U)` if T (and thus U)
is a parameter pack.

To do this, we introduce some new rewrite rules:

1) For each associated type symbol `[P:A]`, a rule `([P:A].[shape] => [P:A])`.
2) For each non-pack generic parameter `τ_d_i`, a rule `τ_d_i.[shape] => [shape]`.

Now consider a rewrite rule `(τ_d_i.[P:A] => τ_D_I.[Q:B])`. The left-hand
side overlaps with the rule `([P:A].[shape] => [shape])` on the term
`τ_d_i.[P:A].[shape]`. Resolving the overlap gives us a new rule

    t_d_i.[shape] => T_D_I.[shape]

If T is a term corresponding to some type parameter, we say that `T.[shape]` is
a shape term. If `T'.[shape]` is a reduced term, we say that T' is the reduced
shape of T.

Recall that shape requirements are represented as rules of the form:

    τ_d_i.[shape] => τ_D_I.[shape]

Now, the rules of the first kind reduce our shape term `T.[shape]` to
`τ_d_i.[shape]`, where `τ_d_i` is the root generic parameter of T.

If `τ_d_i` is not a pack, a rule of the second kind reduces it to `[shape]`,
so the reduced shape of a non-pack parameter T is the empty term.

Otherwise, if `τ_d_i` is a pack, `τ_d_i.[shape]` might reduce to `τ_D_I.[shape]`
via a shape requirement. In this case, `τ_D_I` is the reduced shape of T.

Fixes rdar://problem/101813873.
2023-07-03 15:41:09 -04:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Holly Borla
abb501db74 [RequirementMachine] Add verification for rules involving shape symbols. 2022-10-06 20:48:40 -07:00
Slava Pestov
901cdaf85e RequirementMachine: Relax assertions in verifyRewriteSystem()
We don't care as much about rules with weird shapes as long
as they're LHS- or RHS-simplified, since the new minimal conformances
algorithm no longer relies on certain invariants.

Fixes rdar://problem/94746399.
2022-07-06 14:34:26 -04:00
Slava Pestov
e805339de4 RequirementMachine: Relax assertion in verifyRewriteSystem()
We can end up with a rule that has a protocol symbol on the right hand side:

    X.Y.Z => X.W.[P]

This will occur if X.W.[P] was obtained by simplifying a term X.W.U.V via
a rule (U.V => [P]), and before completion discovers a rule
(X.W.[P] => X.W).

Fixes rdar://problem/94854326, rdar://problem/94980084.
2022-06-17 16:13:42 -04:00
Slava Pestov
f901cc72b4 RequirementMachine: Move diagnostics code into a new Diagnostics.cpp 2022-05-10 01:49:56 -04:00
Slava Pestov
094130b9c7 RequirementMachine: Diagnose redundant concrete conformance requirements
A conformance requirement on a concrete type parameter is redundant if the
concrete type conforms to the protocol.

The replacement path for the conformance rule is based on a concrete
conformance rule introduced by the property map. Since the concrete
conformance rule is not associated with a requirement ID, this would
normally muffle the redundancy warning, because we don't want to
suggest removing a rule that depends on a non-redundant, non-explicit
rule.

However, concrete conformance rules don't actually appear in the
minimal signature, so skip them when computing the set of non-redundant,
non-explicit rules to ensure that the original conformance requirement
is still diagnosed as redundant.
2022-04-05 21:12:51 -04:00
Slava Pestov
4d15fa0087 RequirementMachine: Don't diagnose trivial circularity 'protocol P : P' as redundant
Every protocol gets an 'identity conformance' rule [P].[P] => [P].
A trivially-stated circularity is always redundant because of this
rule, and we diagnose circular inheritance elsewhere as a hard
error, so just add a special case to skip adding such a rule here
to avoid the useless warning on top of the existing error.
2022-04-05 21:12:51 -04:00
Slava Pestov
c0c109d3f5 RequirementMachine: Tighten verifyRewriteRules() further
We should never have a rule with a protocol symbol on the *right* hand
side, like (T => U.[P]).
2022-04-04 23:52:58 -04:00
Slava Pestov
7d51fa9f62 RequirementMachine: RewriteSystem::dump() prints out written requirements 2022-04-04 12:41:57 -04:00
Slava Pestov
22f0f40a30 RequirementMachine: Copy over GSB logic where inheritance of JSExport protocol can be re-stated redundantly 2022-04-01 01:04:54 -04:00
Holly Borla
85be43f18f [RequirementMachine] Omit duplicate conflict diagnostics. 2022-03-30 22:50:13 -07:00
Holly Borla
7e5c48382d [RequirementMachine] When computing requirements for conflict diagnostics,
adjust the concrete type symbol for the suffix rule by applying the prefix
from the subject rule.
2022-03-30 22:30:45 -07:00
Holly Borla
78b07565b6 [RequirementMachine] Use PropertyMap::getTypeFromSubstitutionSchema when
computing a concrete same-type or superclass for conflict diagnostics.

Otherwise, diagnostics will show fresh type parameters when the concrete
type is generic.
2022-03-30 14:09:06 -07:00
Holly Borla
5296fbdf45 [NFC][RequirementMachine] Stylistic changes for computing confict
diagnostics.

Move computing requirements from rules into a static function, make
a few variables const, etc.
2022-03-30 14:09:06 -07:00
Holly Borla
a4230a6061 [RequirementMachine] Simplify computing and emitting conflict diagnostics.
Instead of computing different combinations of conflicting requirement kinds,
emit the same error message for all of them.
2022-03-29 18:27:45 -07:00
Holly Borla
31028e5a9f [RequirementMachine] When computing conflict diagnostics, only compute the
type for the longer subject term between the two conflicting rules.
2022-03-29 18:27:45 -07:00
Holly Borla
964c11a4d0 [RequirementMachine] Consolidate the various RequirementError::Kind cases
that represent conflict diagnostics.
2022-03-29 18:27:45 -07:00
Holly Borla
caee699560 [RequirementMachine] Mention the type parameter that is the subject of
two conflicting requirements in diagnostics where possible.
2022-03-29 18:27:45 -07:00
Holly Borla
99f5e365cc [RequirementMachine] Compute and diagnose conflicting rules in the minimal
rewrite system of a generic/requirement signature.
2022-03-29 18:26:26 -07: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
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
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
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
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
9704168351 RequirementMachine: Skip imported rules in various places when iterating over all rules 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
Holly Borla
6b64ac26f4 [RequirementMachine] Suppress redundant requirement warnings for inferred
requirements.
2022-03-15 21:14:43 -07:00
Slava Pestov
755941c21f RequirementMachine: Split off Rule.cpp from RewriteSystem.cpp 2022-03-15 13:35:34 -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
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
Holly Borla
7ce6504b93 [RequirementMachine] Avoid passing the requirement error vector through
initialization of the rewrite system.

Instead, the rewrite system can determine trivially redundant requirements
by finding structural requirements with no associated rewrite rules.
2022-03-10 13:13:50 -08:00
Holly Borla
0085eb0dfe [RequirementMachine] Separate setting the explicit bit and the requirement
ID for a rewrite rule.
2022-03-09 21:22:53 -08:00
Holly Borla
4b55c30fad [RequirementMachine] Instead of recording redundant requirements and other
diagnostics in the rewrite system, pass down the 'errors' vector from the
top-level requests.
2022-03-09 18:18:43 -08:00